import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import type { LiveData } from '../config/hostInjector.ts'; import { eventually, openPage, waitForHydration, type OpenedPage, } from './support/browser.ts'; import { startServer } from './support/server.ts'; // De CMS-host vult markers in de geprerenderde HTML (plan: aidlc-docs/features/ // asf-cms-host-injection). Hier doet de previewserver alsof hij die host is // (config/hostInjector.ts), met data die van de gebakken data afwijkt. Wat // getest wordt: de HTML die de host schrijft is leesbaar zonder JavaScript, // de pagina hydrateert zonder fout, toont daarna de actuele data zonder zelf // de API te vragen, en valt terug op wat gebakken is als de data niet bruikbaar // is. const AUTHORED = 'https://andreaslop.nl'; let live: { data: LiveData; publicBaseUrl?: string } | null = null; let server: Awaited>; beforeAll(async () => { server = await startServer({ inject: () => live && { data: live.data, authoredBaseUrl: AUTHORED, publicBaseUrl: live.publicBaseUrl, }, }); }); afterAll(() => server.close()); const pakket = ( slug: string, title: string, price: string, order: number, extra: Record = {}, ) => ({ slug, title, price, features: [`Kenmerk van ${title}`], displayOrder: order, ...extra, }); const FIVE = [ pakket('zwangerschap', 'Zwangerschap', '€ 200', 0), pakket('newborn', 'Newborn', '€ 210', 1, { featured: true }), pakket('gezin', 'Gezin', '€ 220', 2), pakket('loveshoot', 'Loveshoot', '€ 230', 3), pakket('portret', 'Portret', '€ 240', 4), ]; const NO_BANNERS = { top: null, bottom: null }; const html = async (path: string) => (await fetch(server.origin + path)).text(); const cards = (page: OpenedPage['page']) => page.locator('[data-testid^="price-card-"]:not([data-testid$="-book"])'); // Elke test begint zonder injectie en zet live zelf const inject = (data: LiveData, publicBaseUrl?: string) => { live = { data, publicBaseUrl }; }; describe('markers in de gebouwde pagina', () => { it('heeft ze op de plekken van het contract, en niet op andere', async () => { live = null; const home = await html('/'); for (const marker of [ 'OFFERINGS-HTML', 'BANNER-TOP-HTML', 'BANNER-BOTTOM-HTML', ]) { expect(home.split(``)).toHaveLength(2); expect(home.split(``)).toHaveLength(2); } for (const path of [ '/', '/over-mij', '/contact', '/soorten-fotoshoots', ]) { const page = await html(path); expect(page.split('')).toHaveLength(2); expect(page).toMatch( /'], }, ), ], banners: { top: null, bottom: { title: 'x', position: 'bottom' }, }, }); const raw = await html('/'); expect(raw).not.toContain('window.pwned'); const { page, messages } = await openPage(server.origin + '/'); await waitForHydration(page); await eventually(() => cards(page).count()).toBe(1); expect(await page.evaluate(() => 'pwned' in window)).toBe(false); expect(await cards(page).first().textContent()).toContain( '', ); expect(messages).toEqual([]); }); }); describe('zonder host die injecteert', () => { it('toont de gebakken pakketten en vraagt de API zelf, zoals voor de injectie', async () => { live = null; const requested: string[] = []; const { page, messages } = await openPage(server.origin + '/', { prepare: async (p) => { p.on('request', (request) => { if (request.url().includes('/api/v1/offerings')) { requested.push(new URL(request.url()).pathname); } }); }, }); await waitForHydration(page); await eventually(() => cards(page).count()).toBe(3); await eventually(async () => requested.length).toBe(3); // Het blok dat de build schreef telt niet als data van de host expect(requested.sort()).toEqual([ '/api/v1/offerings', '/api/v1/offerings/banner', '/api/v1/offerings/banner', ]); expect(messages).toEqual([]); }); });