// @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>; beforeAll(async () => { server = await startServer(); }); afterAll(() => server.close()); async function fetchDocument(path: string) { const response = await fetch(server.origin + path); expect(response.status).toBe(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}@${domain}`].map( (form) => form.toLowerCase(), ); } const preloads = (document: Document) => [ ...document.querySelectorAll( '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(' { 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 // 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]'), ].filter((image) => !image.closest('picture')); 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')); }); });