329 lines
13 KiB
TypeScript
329 lines
13 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
import sitePages from '../src/sitePages.ts';
|
|
import { eventually, openPage, waitForHydration } from './support/browser.ts';
|
|
import type { Page } from 'playwright-core';
|
|
import { startServer } from './support/server.ts';
|
|
|
|
// BR-TST-05, BR-HYD-20: elke pagina hydrateert onder de productie-CSP zonder
|
|
// één console-melding. De build is de productiebuild, dus React meldt een
|
|
// mismatch als minified fout (#418); `messages` laat hem door.
|
|
let server: Awaited<ReturnType<typeof startServer>>;
|
|
beforeAll(async () => {
|
|
server = await startServer();
|
|
});
|
|
afterAll(() => server.close());
|
|
|
|
const pages = Object.keys(sitePages);
|
|
// Smal is het telefoonscherm waar Lighthouse op meet; breed is desktop. De
|
|
// hydratatie verloopt niet gelijk: onder lg staat het mobiele menu in de HTML.
|
|
const widths = [390, 1280];
|
|
|
|
const notFoundPaths = [
|
|
'/404',
|
|
'/404.html',
|
|
'/onbekend',
|
|
'/soorten-fotoshoots/onbekend',
|
|
];
|
|
|
|
// Alleen een document dat de server bewust met 404 beantwoordt (BR-SRV-05)
|
|
const expectedStatus = (path: string) =>
|
|
path === '/404.html' || pages.includes(path) ? 200 : 404;
|
|
|
|
describe.each(widths)('hydratatie op %i px', (width) => {
|
|
it.each([...pages, ...notFoundPaths])(
|
|
'%s hydrateert zonder meldingen of CSP-overtredingen',
|
|
async (path) => {
|
|
const status = expectedStatus(path);
|
|
const { page, ...opened } = await openPage(server.origin + path, {
|
|
width,
|
|
expectNotFound: status === 404,
|
|
});
|
|
expect(opened.status).toBe(status);
|
|
await waitForHydration(page);
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
|
|
// Na de hydratatie nog steeds één titel en hoogstens één robots (V11)
|
|
const counts = await page.evaluate(() => ({
|
|
titles: document.querySelectorAll('title').length,
|
|
robots: document.querySelectorAll('meta[name="robots"]').length,
|
|
h1: document.querySelectorAll('h1').length,
|
|
}));
|
|
const noindex =
|
|
notFoundPaths.includes(path) || path === '/privacyverklaring';
|
|
expect(counts).toEqual({
|
|
titles: 1,
|
|
robots: noindex ? 1 : 0,
|
|
h1: 1,
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
describe('de 404-pagina, gemaakt voor /404 en getoond op een andere URL', () => {
|
|
// De bron van fout #418 op 404.html: Start herkent de match van
|
|
// de catch-all voor een ander pad dan /404 niet en laat die in de browser
|
|
// renderen. React hydrateert die grens los van de rest; komt het laden
|
|
// van de route-chunk later dan die hydratatie, dan was de eerste render
|
|
// leeg tegenover de HTML. Op een snelle pc is het een race, dus hier wordt
|
|
// de chunk vertraagd tot de race altijd verloren wordt.
|
|
it.each(widths)(
|
|
'hydrateert ook als de routechunk laat komt (%i px)',
|
|
async (width) => {
|
|
const { page, ...opened } = await openPage(
|
|
server.origin + '/404.html',
|
|
{
|
|
width,
|
|
prepare: async (page) => {
|
|
await page.route(
|
|
/\/assets\/_-[^/]+\.js$/,
|
|
async (route) => {
|
|
await new Promise((done) =>
|
|
setTimeout(done, 800),
|
|
);
|
|
await route.continue();
|
|
},
|
|
);
|
|
},
|
|
},
|
|
);
|
|
await waitForHydration(page);
|
|
// Wacht tot de vertraagde chunk binnen is en de route zich herstelt
|
|
await page.waitForTimeout(1500);
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
await eventually(() =>
|
|
page
|
|
.getByRole('heading', { name: 'Pagina niet gevonden' })
|
|
.count(),
|
|
).toBe(1);
|
|
},
|
|
);
|
|
|
|
it('toont de twee extra knoppen onder /soorten-fotoshoots/ pas na de hydratatie', async () => {
|
|
const url = server.origin + '/soorten-fotoshoots/onbekend';
|
|
// Zonder JavaScript: precies de HTML van /404, met één knop
|
|
const plain = await openPage(url, {
|
|
javaScript: false,
|
|
expectNotFound: true,
|
|
});
|
|
await eventually(() =>
|
|
plain.page.getByTestId('status-page-home-link').count(),
|
|
).toBe(1);
|
|
await eventually(() =>
|
|
plain.page.getByTestId('status-page-section-link').count(),
|
|
).toBe(0);
|
|
|
|
const { page, ...opened } = await openPage(url, {
|
|
expectNotFound: true,
|
|
});
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.getByTestId('status-page-section-link').count(),
|
|
).toBe(1);
|
|
await eventually(() =>
|
|
page.getByTestId('status-page-home-link').count(),
|
|
).toBe(1);
|
|
expect(opened.messages).toEqual([]);
|
|
});
|
|
|
|
it('toont op een gewone onbekende URL alleen de knop naar home', async () => {
|
|
const { page, ...opened } = await openPage(
|
|
server.origin + '/onbekend',
|
|
{
|
|
expectNotFound: true,
|
|
},
|
|
);
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.getByTestId('status-page-home-link').count(),
|
|
).toBe(1);
|
|
await eventually(() =>
|
|
page.getByTestId('status-page-section-link').count(),
|
|
).toBe(0);
|
|
expect(opened.messages).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// De zoekwaarde (?pakket=, ?shoot=) staat niet in de voorgerenderde HTML en
|
|
// wordt pas na de hydratatie toegepast (BR-HYD-10 tot 12)
|
|
describe('zoekwaarden na de hydratatie', () => {
|
|
const shootField = (page: Page) => page.getByTestId('contact-form-shoot');
|
|
|
|
it('/?pakket=gezin#contact kiest het pakket en ruimt het adres op', async () => {
|
|
const { page, ...opened } = await openPage(
|
|
server.origin + '/?pakket=gezin#contact',
|
|
);
|
|
await waitForHydration(page);
|
|
await eventually(() => shootField(page).inputValue()).toBe('gezin');
|
|
// De tidy-navigatie haalt de zoekwaarde weg, de hash blijft
|
|
await page.waitForFunction(() => location.search === '');
|
|
expect(new URL(page.url()).hash).toBe('#contact');
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
|
|
it('/contact?pakket=gezin kiest het pakket en eindigt op /contact', async () => {
|
|
const { page, ...opened } = await openPage(
|
|
server.origin + '/contact?pakket=gezin',
|
|
);
|
|
await waitForHydration(page);
|
|
await eventually(() => shootField(page).inputValue()).toBe('gezin');
|
|
await page.waitForFunction(() => location.search === '');
|
|
expect(new URL(page.url()).pathname).toBe('/contact');
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
|
|
it('/portfolio?shoot=gezin past het filter toe en houdt het adres', async () => {
|
|
const { page, ...opened } = await openPage(
|
|
server.origin + '/portfolio?shoot=gezin',
|
|
);
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.getByTestId('filter-gezin').getAttribute('aria-pressed'),
|
|
).toBe('true');
|
|
expect(new URL(page.url()).search).toBe('?shoot=gezin');
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// V16: de prijzen komen uit Intl.NumberFormat('nl-NL'). Node maakt de HTML,
|
|
// Chrome hydrateert; verschillen in ICU geven andere tekst (BR-HYD-17).
|
|
describe('prijstekst (V16)', () => {
|
|
const pricePaths = [
|
|
'/',
|
|
...pages.filter((p) => p.startsWith('/soorten-fotoshoots/')),
|
|
];
|
|
|
|
async function priceTexts(page: Page) {
|
|
return page.evaluate(() => {
|
|
const texts: string[] = [];
|
|
const walker = document.createTreeWalker(
|
|
document.body,
|
|
NodeFilter.SHOW_TEXT,
|
|
);
|
|
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
if (node.textContent?.includes('€')) {
|
|
texts.push(node.textContent);
|
|
}
|
|
}
|
|
return texts;
|
|
});
|
|
}
|
|
|
|
it.each(pricePaths)(
|
|
'%s toont voor en na de hydratatie dezelfde prijzen',
|
|
async (path) => {
|
|
const url = server.origin + path;
|
|
const server_ = await openPage(url, { javaScript: false });
|
|
const before = await priceTexts(server_.page);
|
|
const { page, ...opened } = await openPage(url);
|
|
await waitForHydration(page);
|
|
const after = await priceTexts(page);
|
|
expect(before.length).toBeGreaterThan(0);
|
|
expect(after).toEqual(before);
|
|
expect(opened.messages).toEqual([]);
|
|
},
|
|
);
|
|
});
|
|
|
|
// V8, BR-LZY-01 en 05: het contactformulier is een luie chunk
|
|
describe('contactformulier op /contact (V8)', () => {
|
|
it('staat in de HTML, maar verborgen tot het script het op zijn plek zet', async () => {
|
|
const html = await (await fetch(server.origin + '/contact')).text();
|
|
// Het formulier met zijn velden zit in de bytes van de pagina...
|
|
expect(html).toContain('data-testid="contact-form"');
|
|
expect(html).toContain('data-testid="contact-form-shoot"');
|
|
// ...maar als gestreamd segment: een lege plek in main, het formulier
|
|
// in een verborgen div aan het eind van de body
|
|
expect(html).toMatch(/<template id="B:0"><\/template>/);
|
|
expect(html).toMatch(/<div hidden id="S:0">/);
|
|
|
|
// Zonder JavaScript is het formulier niet zichtbaar (FR-PR-13)
|
|
const plain = await openPage(server.origin + '/contact', {
|
|
javaScript: false,
|
|
});
|
|
expect(await plain.page.getByTestId('contact-form').isVisible()).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it('verschijnt in main onder de CSP en geeft geen hydratatiefout', async () => {
|
|
const { page, ...opened } = await openPage(server.origin + '/contact');
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.locator('main').getByTestId('contact-form').isVisible(),
|
|
).toBe(true);
|
|
expect(opened.messages).toEqual([]);
|
|
// Het verplaatste completion-script mag niet geblokkeerd worden
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
|
|
it('hydrateert zonder fout als de chunk van het formulier laat komt (BR-LZY-05)', async () => {
|
|
const { page, ...opened } = await openPage(server.origin + '/contact', {
|
|
prepare: async (page) => {
|
|
await page.route(
|
|
/\/assets\/ContactForm-[^/]+\.js$/,
|
|
async (route) => {
|
|
await new Promise((done) => setTimeout(done, 1500));
|
|
await route.continue();
|
|
},
|
|
);
|
|
},
|
|
});
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.locator('main').getByTestId('contact-form').isVisible(),
|
|
).toBe(true);
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// BR-404-07: een URL die de server niet kent maar de router wel. Dit is een
|
|
// waarneming, geen controle die de suite laat falen. De
|
|
// preview-server draait op Windows en Linux anders (hoofdletters in
|
|
// bestandsnamen), dus de Linux-situatie wordt nagespeeld: 404.html met 404.
|
|
describe('URL die de server niet kent en de router wel (BR-404-07)', () => {
|
|
it.each(['/Over-Mij', '/over-mij/'])('observeert %s', async (path) => {
|
|
const real = await fetch(server.origin + path, { redirect: 'manual' });
|
|
const observe = async (
|
|
served: string,
|
|
prepare?: (page: Page) => Promise<void>,
|
|
) => {
|
|
const { page, ...opened } = await openPage(server.origin + path, {
|
|
prepare,
|
|
});
|
|
await page.waitForTimeout(1500);
|
|
const state = await page.evaluate(() => ({
|
|
url: location.pathname,
|
|
h1: document.querySelector('h1')?.textContent,
|
|
titles: [...document.querySelectorAll('title')].map(
|
|
(t) => t.textContent,
|
|
),
|
|
}));
|
|
console.info(
|
|
`BR-404-07 ${path} (${served}): ${JSON.stringify({ ...state, status: opened.status, messages: opened.messages })}`,
|
|
);
|
|
};
|
|
console.info(
|
|
`BR-404-07 ${path}: preview-server geeft ${real.status} ${real.headers.get('content-type')}`,
|
|
);
|
|
await observe('preview-server van deze machine');
|
|
await observe(
|
|
'404.html met 404, zoals een hoofdlettergevoelige host',
|
|
async (page) => {
|
|
await page.route(server.origin + path, async (route) => {
|
|
const notFound = await route.fetch({
|
|
url: server.origin + '/404.html',
|
|
});
|
|
await route.fulfill({ response: notFound, status: 404 });
|
|
});
|
|
},
|
|
);
|
|
expect(real.status).toBeGreaterThan(0);
|
|
});
|
|
});
|