Files
AndreaSlopFotografie/web/e2e/rawHtml.e2e.ts
T
SluijsensandClaude Opus 5.5 8bf0000531 Adds browser tests and fixes hydration of the 404 page
The prerendered 404 page is served for other URLs, so its dehydrated match id
differs and the router renders the catch-all's pending component first. Without
one that render was empty, which gave React error 418 and duplicate head tags.
The tests run the built output in a real browser under the production CSP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 19:03:07 +01:00

158 lines
6.5 KiB
TypeScript

// @vitest-environment jsdom
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import siteContent from '../src/content/siteContent.ts';
import testimonials from '../src/content/testimonials.ts';
import { emailAddress } from '../src/lib/emailAddress.ts';
import { visibleTestimonials } from '../src/lib/visibleTestimonials.ts';
import { footerNav, mainNav, type NavItem } from '../src/navigation.ts';
import { canonicalUrl, pageHead, siteName } from '../src/seo.ts';
import sitePages from '../src/sitePages.ts';
import type { RoutePath } from '../src/navigation.ts';
import { startServer } from './support/server.ts';
// BR-TST-06: wat een bezoeker of zoekmachine zonder JavaScript krijgt. Alleen
// `fetch` van de preview-server en een parser die geen script uitvoert (de
// jsdom-omgeving is hier alleen voor DOMParser; er is geen browser).
let server: Awaited<ReturnType<typeof startServer>>;
beforeAll(async () => {
server = await startServer();
});
afterAll(() => server.close());
async function fetchDocument(path: string, status = 200) {
const response = await fetch(server.origin + path);
expect(response.status).toBe(status);
const html = await response.text();
// DOMParser voert geen script uit: er wordt alleen gelezen
return {
html,
document: new DOMParser().parseFromString(html, 'text/html'),
};
}
const targets = (items: readonly NavItem[]): string[] =>
items.flatMap((item) => [item.to, ...targets(item.children ?? [])]);
const navTargets = [...new Set([...targets(mainNav), ...targets(footerNav)])];
// Zichtbare tekst van main buiten de h1: geen scripts, stijlen of templates
function bodyText(document: Document) {
const main = document.querySelector('main')!.cloneNode(true) as Element;
for (const node of main.querySelectorAll(
'h1, script, style, template, noscript, [hidden]',
)) {
node.remove();
}
return (main.textContent ?? '').replace(/\s+/g, ' ').trim();
}
// V17: inline event-handler-attributen en javascript:-URL's zijn geen script
// voor de runner maar de CSP blokkeert ze; ze mogen er niet in staan
function inlineScriptVectors(document: Document) {
return [...document.querySelectorAll('*')].flatMap((element) =>
[...element.attributes]
.filter(
({ name, value }) =>
name.startsWith('on') ||
value.trim().toLowerCase().startsWith('javascript:'),
)
.map(({ name }) => `${element.tagName.toLowerCase()}[${name}]`),
);
}
// De samengestelde mailadres-vormen die nergens in de HTML mogen staan; de
// runner (BR-MAIL-10) controleert ze ook in de scripts en stijlen
function addressForms(address: string) {
const [user, domain] = address.split('@');
return [address, `${user}%40${domain}`, `${user}&#64;${domain}`].map(
(form) => form.toLowerCase(),
);
}
const paths = Object.keys(sitePages) as RoutePath[];
describe.each(paths)('rauwe HTML van %s', (path) => {
it('heeft titel, kop, tekst, links, canonical en gegevens', async () => {
const { html, document } = await fetchDocument(path);
const head = pageHead(path);
// Precies één titel en één beschrijving (V11), de titel uit het register
const titles = document.querySelectorAll('title');
expect(titles).toHaveLength(1);
expect(titles[0].textContent).toBe(head.meta[0].title);
expect(
document.querySelectorAll('meta[name="description"]'),
).toHaveLength(1);
// Eén h1, en genoeg tekst eromheen om te lezen zonder JavaScript
expect(document.querySelectorAll('h1')).toHaveLength(1);
expect(bodyText(document).length).toBeGreaterThanOrEqual(40);
// Een link naar elk doel van het hoofdmenu en de footer
const hrefs = new Set(
[...document.querySelectorAll('a[href]')].map((a) =>
a.getAttribute('href'),
),
);
for (const target of navTargets) expect(hrefs).toContain(target);
const canonical = document.querySelectorAll('link[rel="canonical"]');
expect(canonical).toHaveLength(1);
expect(canonical[0].getAttribute('href')).toBe(canonicalUrl(path));
// JSON-LD: aanwezig en leesbaar
const blocks = [
...document.querySelectorAll('script[type="application/ld+json"]'),
];
expect(blocks.length).toBeGreaterThan(0);
for (const block of blocks) {
expect(() => JSON.parse(block.textContent ?? '')).not.toThrow();
}
expect(html).not.toContain('data-fallback');
expect(inlineScriptVectors(document)).toEqual([]);
const address = emailAddress(siteContent.email);
expect(address).toBeTruthy();
for (const form of addressForms(address!)) {
expect(html.toLowerCase()).not.toContain(form);
}
});
});
describe('rauwe HTML van 404.html', () => {
it('heeft één titel, één noindex en geen beschrijving of canonical', async () => {
// 404.html als bestand: status 200 (BR-SRV-04)
const { html, document } = await fetchDocument('/404.html');
const titles = document.querySelectorAll('title');
expect(titles).toHaveLength(1);
expect(titles[0].textContent).toBe(
`Pagina niet gevonden | ${siteName}`,
);
const robots = document.querySelectorAll('meta[name="robots"]');
expect(robots).toHaveLength(1);
expect(robots[0].getAttribute('content')).toBe('noindex');
expect(document.querySelectorAll('h1')).toHaveLength(1);
expect(bodyText(document).length).toBeGreaterThanOrEqual(40);
expect(html).not.toContain('data-fallback');
expect(inlineScriptVectors(document)).toEqual([]);
});
it('is wat de server voor een onbekende URL stuurt, met status 404', async () => {
const { html } = await fetchDocument('/onbekend', 404);
const file = await (await fetch(server.origin + '/404.html')).text();
expect(html).toBe(file);
});
});
describe('reviews op de homepage (V9)', () => {
it('heeft de tekst van elke zichtbare review in de HTML', async () => {
const { document } = await fetchDocument('/');
const text = (document.body.textContent ?? '').replace(/\s+/g, ' ');
const shown = visibleTestimonials(testimonials);
expect(shown.length).toBeGreaterThan(0);
for (const { quote, author } of shown) {
expect(text).toContain(quote);
expect(text).toContain(author);
}
});
});