import { readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import { screen, within } from '@testing-library/react'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import packages from './content/packages'; import shootPageShared from './content/shootPageShared'; import shootTypes from './content/shootTypes'; import { formatPrice } from './lib/formatPrice'; import { readMoreLabel } from './lib/readMoreLabel'; import { shootPagePath } from './lib/shootPages'; import sitePages from './sitePages'; import { FakeIntersectionObserver } from './test/fakeIntersectionObserver'; import { axeResults } from './test/axe'; import { cssRulesFor } from './test/cssRules'; import { expectShell, renderApp } from './test/renderApp'; // The pages use IntersectionObserver (fade-in, deferred photos) and the // homepage-style shell may use Element.scrollTo, which jsdom lacks beforeEach(() => { vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver); Element.prototype.scrollTo = () => {}; }); afterEach(() => { vi.unstubAllGlobals(); FakeIntersectionObserver.reset(); // @ts-expect-error -- jsdom has no Element.scrollTo of its own delete Element.prototype.scrollTo; }); const withPackage = ['newborn', 'zwangerschap', 'gezin']; const mosaicTiles: Record = { newborn: 3 }; const renderShoot = async (id: string) => { const path = shootPagePath(id as (typeof shootTypes)[number]['id']); const result = await renderApp(path); await screen.findByRole('heading', { level: 1 }); return result; }; const isAlt = (element: Element) => cssRulesFor(element).join().includes('var(--slp-colors-paper-alt)'); describe('shoot pages', () => { it.each(shootTypes.map((shoot) => shoot.id))( '%s has a clean heading outline and the sections its data says', async (id) => { const { container } = await renderShoot(id); expectShell(); const main = screen.getByRole('main'); expect( within(main).getAllByRole('heading', { level: 1 }), ).toHaveLength(1); // Six process steps; FAQ questions are summaries, not headings expect( within(main).getAllByRole('heading', { level: 3 }), ).toHaveLength(6); const levels = [ ...main.querySelectorAll('h1, h2, h3, h4, h5, h6'), ].map((heading) => Number(heading.tagName[1])); levels.slice(1).forEach((level, index) => { expect(level).toBeLessThanOrEqual(levels[index] + 1); }); // Every section is named by an existing heading for (const section of main.querySelectorAll('section')) { const target = section.getAttribute('aria-labelledby')!; expect(main.querySelector(`#${target}`)).not.toBeNull(); } expect(main.querySelector('#zo-werkt-het')).not.toBeNull(); expect(main.querySelector('#vragen')).not.toBeNull(); expect(main.querySelector('#pakket')).not.toBeNull(); const tiles = mosaicTiles[id] ?? 0; if (tiles > 0) { expect(main.querySelector('#impressie')).not.toBeNull(); expect( within(screen.getByTestId('photo-mosaic')).getAllByRole( 'listitem', ), ).toHaveLength(tiles); } else { expect(main.querySelector('#impressie')).toBeNull(); expect(screen.queryByTestId('photo-mosaic')).toBeNull(); } expect(await axeResults(container)).toHaveNoViolations(); }, ); it.each(shootTypes)( '$id has a breadcrumb that ends at the page itself, and nothing else in main is current', async ({ id, name }) => { await renderShoot(id); const crumb = screen.getByRole('navigation', { name: 'Kruimelpad', }); expect(crumb).toBe(screen.getByTestId('shoot-breadcrumb')); const items = within(crumb).getAllByRole('listitem'); // Three items and two separators expect( items.filter((item) => !item.hasAttribute('aria-hidden')), ).toHaveLength(3); expect( within(crumb).getByRole('link', { name: 'Home' }), ).toHaveAttribute('href', '/'); expect( within(crumb).getByRole('link', { name: 'Soorten fotoshoots' }), ).toHaveAttribute('href', '/soorten-fotoshoots'); const current = within(crumb).getByText(name); expect(current).toHaveAttribute('aria-current', 'page'); // The header and footer are checked on the whole page below const marked = screen .getByRole('main') .querySelectorAll('[aria-current]'); expect([...marked]).toEqual([current]); }, ); // Whole-page facet (BR-SP-03, application design D4). The header submenu // child and the breadcrumb's last item both name the page itself, so the // rule is "at most one in main and at most one in each menu", not one // overall. The two menus (the header's, and the mobile menu which is a // sibling of the header and only one of them shows at a time) each mark // the shoot's own link; the parent link and the footer link are not // current. it.each(shootTypes)( '$id marks only the page itself as current: once in main (breadcrumb) and once in each menu (submenu link)', async ({ id, name }) => { await renderShoot(id); const header = screen.getByRole('banner'); const main = screen.getByRole('main'); const footer = screen.getByRole('contentinfo'); const crumbCurrent = within( screen.getByTestId('shoot-breadcrumb'), ).getByText(name); const inMain = [...main.querySelectorAll('[aria-current]')]; expect(inMain).toEqual([crumbCurrent]); const inHeader = [...header.querySelectorAll('[aria-current]')]; expect(inHeader).toHaveLength(1); expect(inHeader[0]).toHaveAttribute('aria-current', 'page'); expect(inHeader[0]).toHaveAttribute('href', shootPagePath(id)); // The parent stays unmarked for assistive technology const parentLinks = [ ...document.querySelectorAll( 'header a[href="/soorten-fotoshoots"], #mobile-nav a[href="/soorten-fotoshoots"]', ), ]; expect(parentLinks.length).toBeGreaterThan(0); expect( parentLinks.some((link) => link.hasAttribute('aria-current')), ).toBe(false); expect(footer.querySelectorAll('[aria-current]')).toHaveLength(0); const mobile = [ ...document.querySelectorAll('#mobile-nav [aria-current]'), ]; expect(mobile).toHaveLength(1); expect(mobile[0]).toHaveAttribute('aria-current', 'page'); expect(mobile[0]).toHaveAttribute('href', shootPagePath(id)); // Nothing outside breadcrumb, header menu and mobile menu expect( document.querySelectorAll('[aria-current="page"]'), ).toHaveLength(3); }, ); it.each(shootTypes)( '$id shows the right offer, with the deposit note and a closing band of another tone', async ({ id }) => { await renderShoot(id); const offer = screen.getByTestId('package-block'); const note = shootPageShared.deposit.note.replace( '{aanbetaling}', formatPrice(shootPageShared.deposit.amount), ); const noteElement = within(offer).getByText( (_, element) => element?.textContent === note && element.tagName === 'P', ); if (withPackage.includes(id)) { const pkg = packages.find((item) => item.id === id)!; expect( within(offer).getByRole('link', { name: 'Boek dit pakket', }), ).toHaveAttribute('href', `/contact?pakket=${id}`); expect( within(offer).getByRole('link', { name: shootPageShared.offer.askLabel, }), ).toHaveAttribute('href', '/contact'); expect( within(offer).getByText( (_, element) => element?.tagName === 'P' && element.textContent === formatPrice(pkg.price), ), ).toBeInTheDocument(); } else { expect( within(offer).getByText('Prijs op aanvraag'), ).toBeInTheDocument(); expect(within(offer).queryByRole('list')).toBeNull(); const links = within(offer).getAllByRole('link'); expect(links).toHaveLength(1); expect(links[0]).toHaveAttribute('href', '/contact'); } // The note comes after the actions const lastLink = within(offer).getAllByRole('link').at(-1)!; expect( lastLink.compareDocumentPosition(noteElement) & Node.DOCUMENT_POSITION_FOLLOWING, ).toBeTruthy(); const packageBand = screen .getByRole('main') .querySelector('#pakket')!; const closingBand = screen.getByTestId('shoot-closing'); expect(isAlt(packageBand)).not.toBe(isAlt(closingBand)); }, ); it.each(shootTypes.map((shoot) => shoot.id))( '%s loads only the first story photo with priority', async (id) => { await renderShoot(id); const images = within(screen.getByRole('main')).getAllByRole('img'); const first = within(screen.getByTestId('shoot-page')) .getAllByRole('img') .at(0)!; expect(images[0]).toBe(first); expect(first).not.toHaveAttribute('loading', 'lazy'); expect(first).toHaveAttribute('fetchpriority', 'high'); for (const image of images.slice(1)) { expect(image).not.toHaveAttribute('loading', 'eager'); expect(image).not.toHaveAttribute('fetchpriority'); } }, ); }); describe('overview page', () => { it('gives every shoot a "Lees meer" link to its page, first in its row, with a unique name', async () => { await renderApp('/soorten-fotoshoots'); await screen.findByRole('heading', { level: 1 }); const names: string[] = []; for (const shoot of shootTypes) { const link = screen.getByRole('link', { name: readMoreLabel(shoot), }); expect(link).toHaveAttribute('href', shootPagePath(shoot.id)); const article = link.closest('article')!; expect(article.id).toBe(shoot.id); expect(within(article).getAllByRole('link')[0]).toBe(link); names.push(link.textContent!); } expect(new Set(names).size).toBe(shootTypes.length); }); it('keeps the anchors and the old links', async () => { await renderApp('/soorten-fotoshoots'); await screen.findByRole('heading', { level: 1 }); for (const shoot of shootTypes) { expect( screen.getByRole('article', { name: shoot.name }), ).toHaveAttribute('id', shoot.id); expect( screen.getByTestId(`shoot-${shoot.id}-contact`), ).toBeInTheDocument(); } }); it('links to each shoot page exactly once and to no other shoot path', async () => { await renderApp('/soorten-fotoshoots'); await screen.findByRole('heading', { level: 1 }); const main = screen.getByRole('main'); const hrefs = within(main) .getAllByRole('link') .map((link) => link.getAttribute('href')!) .filter((href) => href.startsWith('/soorten-fotoshoots/')); expect([...hrefs].sort()).toEqual( shootTypes.map((shoot) => shootPagePath(shoot.id)).sort(), ); }); }); // The size of a JPEG, read from its start-of-frame marker (baseline, // extended, progressive or lossless), without an image library function jpegSize(bytes: Uint8Array) { expect([bytes[0], bytes[1]]).toEqual([0xff, 0xd8]); let offset = 2; while (offset + 9 < bytes.length) { expect(bytes[offset]).toBe(0xff); const marker = bytes[offset + 1]; const length = (bytes[offset + 2] << 8) | bytes[offset + 3]; const isFrame = marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker); if (isFrame) { return { height: (bytes[offset + 5] << 8) | bytes[offset + 6], width: (bytes[offset + 7] << 8) | bytes[offset + 8], }; } offset += 2 + length; } throw new Error('no frame marker in the JPEG'); } describe('preview images of the shoot pages', () => { it.each(shootTypes.map((shoot) => shoot.id))( '%s has a registered 1200 x 630 JPEG of at most 150 KB', (id) => { const og = sitePages[shootPagePath(id)].ogImage; expect(og).toBeDefined(); expect(og!.path).toBe(`/og-${id}.jpg`); expect(og!.width).toBe(1200); expect(og!.height).toBe(630); expect(og!.alt.trim()).not.toBe(''); const file = resolve( import.meta.dirname, '../public', `og-${id}.jpg`, ); const bytes = readFileSync(file); expect(bytes.length).toBeLessThanOrEqual(150 * 1024); expect(jpegSize(bytes)).toEqual({ width: 1200, height: 630 }); }, ); it('uses no preview image twice in the registry', () => { const paths = Object.values(sitePages).flatMap((page) => page.ogImage ? [page.ogImage.path] : [], ); expect(new Set(paths).size).toBe(paths.length); }); });