Files
AndreaSlopFotografie/web/e2e/hydration.e2e.ts
SluijsensandClaude Sonnet 5.5 a1444b7b2a Adds the captcha frame hosts to the CSP and handles a widget that cannot load
frame-src gets the three exact Friendly Captcha global hosts (no wildcard).
When the widget errors, is blocked or shows no sign of life, the form says so
and offers the mail button instead of blocking silently. Adds an e2e for the
form with a mocked API, a draft privacy text for approval, and label tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 23:27:29 +01:00

321 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);
// /404 en /404.html zijn echte 404's (BR-HST-05)
const expectedStatus = (path: string) => (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)
// en één kop 1 voor wie de pagina ziet of hoort. Op een 404 staat
// er een tijdje (tot pendingMinMs, 0,5 s) naast de echte kop een
// kopie van de serverpagina in een display:none-sectie: die ziet
// en hoort niemand, maar querySelectorAll telt hem wel mee.
const counts = await page.evaluate(() => ({
titles: document.querySelectorAll('title').length,
robots: document.querySelectorAll('meta[name="robots"]').length,
h1: [...document.querySelectorAll('h1')].filter(
(heading) =>
heading.checkVisibility() &&
!heading.closest('[aria-hidden="true"]'),
).length,
}));
const noindex =
notFoundPaths.includes(path) || path === '/privacyverklaring';
expect(counts).toEqual({
titles: 1,
robots: noindex ? 1 : 0,
h1: 1,
});
// Is de wissel klaar, dan is ook de verborgen kopie weg
await eventually(() =>
page.evaluate(() => document.querySelectorAll('h1').length),
).toBe(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,
expectNotFound: true,
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, BR-HST-08: een URL die de router wel kent maar de server niet als
// pagina bedient kan geen verschil tussen HTML en eerste render meer geven,
// want de server verwijst hem eerst door naar het canonieke pad. Het volledige
// gedrag in Chrome staat in urls.e2e.ts.
describe('URL die alleen de router kent wordt doorverwezen (BR-HST-08)', () => {
it.each(['/Over-Mij', '/over-mij/'])(
'%s gaat met één 301 naar /over-mij',
async (path) => {
const real = await fetch(server.origin + path, {
redirect: 'manual',
});
expect(real.status).toBe(301);
expect(real.headers.get('location')).toBe('/over-mij');
const { page, ...opened } = await openPage(server.origin + path);
await waitForHydration(page);
expect(new URL(page.url()).pathname).toBe('/over-mij');
expect(opened.status).toBe(200);
expect(opened.messages).toEqual([]);
expect(opened.violations).toEqual([]);
},
);
});