The build still bakes the current packages and the active banner into the HTML. After hydration the page asks /api/v1/offerings and /api/v1/offerings/banner once and replaces what differs; errors and time-outs keep what was baked, and a 204 from the banner route hides the banner. The banner is now the OfferingBanner contract (title, text, optional link) instead of layout=banner. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
141 lines
5.2 KiB
TypeScript
141 lines
5.2 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
import siteContent from '../src/content/siteContent.ts';
|
|
import {
|
|
eventually,
|
|
openPage,
|
|
waitForHydration,
|
|
type OpenedPage,
|
|
} from './support/browser.ts';
|
|
import { startServer } from './support/server.ts';
|
|
|
|
// Het contactformulier tegen de gebouwde site, met een nagebootste API
|
|
// (page.route). Welke tests draaien hangt af van de schakelaar in de content:
|
|
// uit (nu) betekent dat de site de server niets vraagt; aan betekent dat het
|
|
// formulier opent als de server het toestaat.
|
|
const enabled = siteContent.contactForm.enabled;
|
|
|
|
let server: Awaited<ReturnType<typeof startServer>>;
|
|
beforeAll(async () => {
|
|
server = await startServer();
|
|
});
|
|
afterAll(() => server.close());
|
|
|
|
const TOKEN = {
|
|
token: 'signed-token',
|
|
minSeconds: 3,
|
|
captcha: null,
|
|
};
|
|
|
|
// Houdt bij welke API-verzoeken de pagina doet en beantwoordt ze
|
|
function mockApi(options: {
|
|
token?: { status: number; body?: unknown };
|
|
submit?: { status: number; body?: unknown };
|
|
}) {
|
|
const requests: string[] = [];
|
|
const bodies: unknown[] = [];
|
|
return {
|
|
requests,
|
|
bodies,
|
|
prepare: async (page: OpenedPage['page']) => {
|
|
await page.route('**/api/v1/**', async (route) => {
|
|
const request = route.request();
|
|
const path = new URL(request.url()).pathname;
|
|
// The live packages call of every page: no live data, and
|
|
// not part of what these tests count
|
|
if (path.startsWith('/api/v1/offerings')) {
|
|
return route.fulfill({ status: 204 });
|
|
}
|
|
requests.push(`${request.method()} ${path}`);
|
|
if (path.endsWith('/token')) {
|
|
const { status, body } = options.token ?? {
|
|
status: 200,
|
|
body: TOKEN,
|
|
};
|
|
return route.fulfill({
|
|
status,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify(body ?? {}),
|
|
});
|
|
}
|
|
bodies.push(request.postDataJSON());
|
|
const { status, body } = options.submit ?? {
|
|
status: 202,
|
|
body: { status: 'received' },
|
|
};
|
|
return route.fulfill({
|
|
status,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify(body ?? {}),
|
|
});
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
describe.skipIf(enabled)('contactformulier uitgeschakeld', () => {
|
|
it('vraagt de server niets en toont de poort op /contact', async () => {
|
|
const api = mockApi({});
|
|
const { page, ...opened } = await openPage(server.origin + '/contact', {
|
|
prepare: api.prepare,
|
|
});
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.getByTestId('contact-gate-notice').count(),
|
|
).toBe(1);
|
|
// Scrollen naar het formulier mag ook geen verzoek opleveren
|
|
await page.getByTestId('contact-gate-notice').scrollIntoViewIfNeeded();
|
|
await page.waitForTimeout(500);
|
|
expect(api.requests).toEqual([]);
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe.skipIf(!enabled)('contactformulier ingeschakeld', () => {
|
|
const fill = async (page: OpenedPage['page']) => {
|
|
await page.getByTestId('contact-form-naam').fill('Demi');
|
|
await page.getByTestId('contact-form-email').fill('demi@example.nl');
|
|
await page.getByTestId('contact-form-shoot').selectOption('Newborn');
|
|
await page
|
|
.getByTestId('contact-form-bericht')
|
|
.fill('Wanneer heb je plek?');
|
|
};
|
|
|
|
it('opent als de server het toestaat en verstuurt het bericht', async () => {
|
|
const api = mockApi({});
|
|
const { page, ...opened } = await openPage(server.origin + '/contact', {
|
|
prepare: api.prepare,
|
|
});
|
|
await waitForHydration(page);
|
|
await eventually(() =>
|
|
page.getByTestId('contact-gate-notice').count(),
|
|
).toBe(0);
|
|
await fill(page);
|
|
await page.getByTestId('contact-form-submit').click();
|
|
await eventually(() =>
|
|
page.getByTestId('contact-form-sent').count(),
|
|
).toBe(1);
|
|
expect(api.requests).toEqual([
|
|
'GET /api/v1/contact-messages/token',
|
|
'POST /api/v1/contact-messages',
|
|
]);
|
|
expect(api.bodies[0]).toMatchObject({
|
|
name: 'Demi',
|
|
subject: 'Newborn',
|
|
token: 'signed-token',
|
|
});
|
|
expect(opened.messages).toEqual([]);
|
|
expect(opened.violations).toEqual([]);
|
|
});
|
|
|
|
it('blijft bij de poort als het formulier dicht is (404)', async () => {
|
|
const api = mockApi({ token: { status: 404 } });
|
|
const { page } = await openPage(server.origin + '/contact', {
|
|
prepare: api.prepare,
|
|
});
|
|
await waitForHydration(page);
|
|
await eventually(async () => api.requests.length).toBe(1);
|
|
expect(await page.getByTestId('contact-gate-notice').count()).toBe(1);
|
|
});
|
|
});
|