Files
AndreaSlopFotografie/web/e2e/displayHtml.e2e.ts
SluijsensandClaude Sonnet 5.5 ad0b114828 Hero from the CMS: markers HERO-HTML and HERO-PRELOAD, hero key in the data block, picture per ratio, baked fallback
The prerendered hero is plain HTML between the markers (first photo, h1, buttons), the slider takes over after hydration with the hero of the host block, the API or the build. The head preload is added to the finished HTML. Hero texts move into nl.json. The stand-in host and the e2e tests cover the hero.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 16:48:00 +01:00

219 lines
9.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment jsdom
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import siteContent from '../src/content/siteContent.ts';
import { emailAddress } from '../src/lib/emailAddress.ts';
import type { RoutePath } from '../src/navigation.ts';
import sitePages from '../src/sitePages.ts';
import { startServer } from './support/server.ts';
// BR-PRE-04, BR-NAV-02, V-D11: wat er in de rauwe HTML van elke pagina staat
// over de CSS zonder JavaScript, de preloads en de noscript-tweelingen. Alleen
// `fetch` van de preview-server; DOMParser is hier de enige parser (hij voert
// niets uit en leest noscript-inhoud als markup).
let server: Awaited<ReturnType<typeof startServer>>;
beforeAll(async () => {
server = await startServer();
});
afterAll(() => server.close());
async function fetchDocument(path: string) {
const response = await fetch(server.origin + path);
// /404.html is een echte 404 (BR-HST-05)
expect(response.status).toBe(path === '/404.html' ? 404 : 200);
const html = await response.text();
return {
html,
document: new DOMParser().parseFromString(html, 'text/html'),
};
}
// Zelfde vormen als in rawHtml.e2e.ts
function addressForms(address: string) {
const [user, domain] = address.split('@');
return [address, `${user}%40${domain}`, `${user}&#64;${domain}`].map(
(form) => form.toLowerCase(),
);
}
const preloads = (document: Document) => [
...document.querySelectorAll<HTMLLinkElement>(
'link[rel="preload"][as="image"]',
),
];
const isLogo = (link: Element) => /\/logo-/.test(link.getAttribute('href')!);
const photoPreloads = (document: Document) =>
preloads(document).filter((link) => !isLogo(link));
const priorityPages: RoutePath[] = [
'/',
'/over-mij',
'/soorten-fotoshoots',
'/soorten-fotoshoots/newborn',
'/soorten-fotoshoots/zwangerschap',
'/soorten-fotoshoots/gezin',
'/soorten-fotoshoots/loveshoot',
'/soorten-fotoshoots/portret',
];
const paths = [...(Object.keys(sitePages) as RoutePath[]), '/404.html'];
describe.each(paths)('display-HTML van %s', (path) => {
it('heeft één noscript met één stijl in de head, zonder adres of verwijzing', async () => {
const { html, document } = await fetchDocument(path);
const noscripts = document.head.querySelectorAll('noscript');
expect(noscripts).toHaveLength(1);
const styles = noscripts[0].querySelectorAll('style');
expect(styles).toHaveLength(1);
const css = styles[0].textContent ?? '';
expect(css.length).toBeGreaterThan(0);
expect(css).not.toContain('</');
expect(css).not.toContain('url(');
expect(css).not.toContain('@import');
const address = emailAddress(siteContent.email);
expect(address).toBeTruthy();
for (const form of addressForms(address!)) {
expect(html.toLowerCase()).not.toContain(form);
}
expect(html).not.toContain('data-reveal="hidden"');
});
it('noemt elke foto in hoogstens één preload', async () => {
const { document } = await fetchDocument(path);
const hrefs = preloads(document).map((link) =>
link.getAttribute('href'),
);
expect(new Set(hrefs).size).toBe(hrefs.length);
});
it('geeft elke uitgestelde foto een noscript-tweeling met dezelfde alt', async () => {
const { document } = await fetchDocument(path);
const deferred = [
...document.body.querySelectorAll('img[data-deferred]'),
];
for (const image of deferred) {
const twin = image.nextElementSibling;
expect(twin?.tagName).toBe('NOSCRIPT');
const twinImage = twin!.querySelector('img');
expect(twinImage).not.toBeNull();
expect(twinImage!.getAttribute('alt')).toBe(
image.getAttribute('alt'),
);
expect(twinImage!.getAttribute('src')).toBeTruthy();
expect(image.hasAttribute('src')).toBe(false);
}
});
it('preloadt niets dat lazy is of alleen een tweeling is', async () => {
const { document } = await fetchDocument(path);
const linkFor = (src: string | null) =>
preloads(document).filter(
(link) => link.getAttribute('href') === src,
);
// Een lazy foto wordt door React niet gepreload en mag ook geen
// expliciete link hebben, tenzij een niet-lazy img (het donkere logo
// in de header en in de footer) hetzelfde bestand gebruikt
const eager = new Set(
[...document.body.querySelectorAll('img[src]')]
.filter(
(image) =>
image.getAttribute('loading') !== 'lazy' &&
!image.closest('noscript'),
)
.map((image) => image.getAttribute('src')),
);
for (const lazy of document.body.querySelectorAll(
'img[loading="lazy"][src]',
)) {
const src = lazy.getAttribute('src');
if (eager.has(src)) continue;
expect(linkFor(src)).toHaveLength(0);
}
// Een tweeling krijgt van React geen preload. Alleen een expliciete
// link met media (de eerste rij van /portfolio, BR-PRE-11) noemt
// dezelfde foto, want die is aan dezelfde breedte gebonden als de
// <source> van de tegel.
for (const twin of document.body.querySelectorAll('noscript img')) {
for (const link of linkFor(twin.getAttribute('src'))) {
expect(path).toBe('/portfolio');
expect(link.getAttribute('media')).toBeTruthy();
}
}
});
it('preloadt het lichte logo alleen op de homepage', async () => {
const { document } = await fetchDocument(path);
const light = preloads(document).filter((link) =>
/\/logo-light-/.test(link.getAttribute('href')!),
);
expect(light).toHaveLength(path === '/' ? 1 : 0);
});
});
describe.each(priorityPages)('prioriteitsfoto van %s', (path) => {
it('heeft zijn preload met fetchpriority high, uit dezelfde src als de img', async () => {
const { document } = await fetchDocument(path);
const body = document.body.cloneNode(true) as HTMLElement;
for (const node of body.querySelectorAll('noscript')) node.remove();
const prioritized = [
...body.querySelectorAll('img[fetchpriority="high"][src]'),
// The hero photo sits in a <picture> but has no <source> for the
// baked page, so its preload is the plain one (config/heroPreload)
].filter(
(image) =>
!image.closest('picture') ||
image.classList.contains('cms-hero-image'),
);
expect(prioritized.length).toBeGreaterThan(0);
for (const image of prioritized) {
const links = preloads(document).filter(
(link) =>
link.getAttribute('href') === image.getAttribute('src'),
);
expect(links).toHaveLength(1);
expect(links[0].getAttribute('fetchpriority')).toBe('high');
expect(links[0].hasAttribute('media')).toBe(false);
}
});
});
describe('preloads van /contact', () => {
it('heeft één media-link voor de foto naast het formulier en geen andere voor dat bestand', async () => {
const { document } = await fetchDocument('/contact');
const source = document.body.querySelector('picture source[srcset]');
expect(source).not.toBeNull();
const aside = source!.getAttribute('srcset');
const links = preloads(document).filter(
(link) => link.getAttribute('href') === aside,
);
expect(links).toHaveLength(1);
expect(links[0].getAttribute('media')).toBe('(min-width: 64em)');
expect(links[0].getAttribute('fetchpriority')).toBe('high');
expect(photoPreloads(document)).toHaveLength(1);
});
});
describe('preloads van /portfolio', () => {
it('heeft de React-link voor tegel 1 en twee media-links voor twee andere foto’s', async () => {
const { document } = await fetchDocument('/portfolio');
const links = photoPreloads(document);
const plain = links.filter((link) => !link.hasAttribute('media'));
const bound = links.filter((link) => link.hasAttribute('media'));
expect(plain).toHaveLength(1);
expect(plain[0].getAttribute('fetchpriority')).toBe('high');
expect(bound.map((link) => link.getAttribute('media'))).toEqual([
'(min-width: 48em)',
'(min-width: 64em)',
]);
expect(
new Set(
[...plain, ...bound].map((link) => link.getAttribute('href')),
).size,
).toBe(3);
// De React-link is de src van de eerste tegel
const first = document.querySelector(
'[data-testid="portfolio-tile-1"] img',
);
expect(first?.getAttribute('src')).toBe(plain[0].getAttribute('href'));
});
});