frame-src gets the three exact Friendly Captcha global hosts (no wildcard). When the widget errors, is blocked or shows no sign of life, the form says so and offers the mail button instead of blocking silently. Adds an e2e for the form with a mocked API, a draft privacy text for approval, and label tests. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
136 lines
4.9 KiB
TypeScript
136 lines
4.9 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;
|
|
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);
|
|
});
|
|
});
|