Copies the headless cms-client package (SlpSoftware 93fa729) and builds the form on useContactForm: availability check when the form is in view (no request while switched off), the readable shoot label as subject, the Friendly Captcha widget as its own lazy chunk, trackEvent for contact-sent and contact-failed. Removes the old reducer, validation and submit stub. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
113 lines
4.1 KiB
TypeScript
113 lines
4.1 KiB
TypeScript
import fc from 'fast-check';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { CONTACT_LIMITS, EMPTY_FIELDS, type ContactFields } from '../types';
|
|
import { hasErrors, trimFields, validateContact } from '../validateContact';
|
|
|
|
const valid: ContactFields = {
|
|
name: 'Jan Jansen',
|
|
email: 'jan@example.nl',
|
|
subject: 'website',
|
|
message: 'Ik wil graag een offerte.',
|
|
};
|
|
|
|
describe('validateContact (examples)', () => {
|
|
it('accepts valid values', () => {
|
|
expect(validateContact(valid)).toEqual({});
|
|
expect(hasErrors(validateContact(valid))).toBe(false);
|
|
});
|
|
|
|
it('requires every field (after trimming)', () => {
|
|
expect(validateContact(EMPTY_FIELDS)).toEqual({
|
|
name: 'required',
|
|
email: 'required',
|
|
subject: 'required',
|
|
message: 'required',
|
|
});
|
|
expect(validateContact({ ...valid, name: ' ', message: '\n\t ' })).toEqual({
|
|
name: 'required',
|
|
message: 'required',
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
['plain', false],
|
|
['a@b', false],
|
|
['a@b.c', false],
|
|
['a b@c.nl', false],
|
|
['a@b.nl', true],
|
|
['first.last+tag@sub.example.nl', true],
|
|
])('email %s valid=%s', (email, ok) => {
|
|
const errors = validateContact({ ...valid, email });
|
|
expect(errors.email).toBe(ok ? undefined : 'invalid');
|
|
});
|
|
|
|
it('reports the first failing rule per field: required, then length, then format', () => {
|
|
const tooLongInvalid = 'x'.repeat(CONTACT_LIMITS.email + 1);
|
|
expect(validateContact({ ...valid, email: tooLongInvalid }).email).toBe('tooLong');
|
|
expect(validateContact({ ...valid, email: 'nope' }).email).toBe('invalid');
|
|
});
|
|
|
|
it('rejects control characters in name, email and subject, but allows line breaks in the message', () => {
|
|
expect(validateContact({ ...valid, name: 'Jan\nJansen' }).name).toBe('invalid');
|
|
expect(validateContact({ ...valid, subject: 'a\u0007b' }).subject).toBe('invalid');
|
|
expect(validateContact({ ...valid, message: 'regel 1\nregel 2\tmet tab' })).toEqual({});
|
|
expect(validateContact({ ...valid, message: 'bel\u0007' }).message).toBe('invalid');
|
|
});
|
|
|
|
it('trims before measuring', () => {
|
|
expect(validateContact({ ...valid, name: ` ${'a'.repeat(100)} ` })).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe('validateContact (properties)', () => {
|
|
const text = fc.string({ maxLength: 300 });
|
|
const fields = fc.record({ name: text, email: text, subject: text, message: text });
|
|
|
|
it('is invariant under trimming', () => {
|
|
fc.assert(
|
|
fc.property(fields, (values) => {
|
|
expect(validateContact(values)).toEqual(validateContact(trimFields(values)));
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('treats the length limits as inclusive for every field', () => {
|
|
fc.assert(
|
|
fc.property(fc.constantFrom('name', 'subject', 'message' as const), (field) => {
|
|
const limit = CONTACT_LIMITS[field];
|
|
const at = { ...valid, [field]: 'a'.repeat(limit) };
|
|
const over = { ...valid, [field]: 'a'.repeat(limit + 1) };
|
|
expect(validateContact(at)[field]).toBeUndefined();
|
|
expect(validateContact(over)[field]).toBe('tooLong');
|
|
}),
|
|
);
|
|
const localPart = 'a'.repeat(CONTACT_LIMITS.email - '@b.nl'.length);
|
|
expect(validateContact({ ...valid, email: `${localPart}@b.nl` }).email).toBeUndefined();
|
|
expect(validateContact({ ...valid, email: `${localPart}a@b.nl` }).email).toBe('tooLong');
|
|
});
|
|
|
|
it('rejects any control character inside name', () => {
|
|
fc.assert(
|
|
fc.property(
|
|
fc.string({ minLength: 1, maxLength: 20 }).filter((s) => s.trim().length > 0),
|
|
fc.integer({ min: 0, max: 0x1f }),
|
|
(base, code) => {
|
|
const name = `${base.trim()}${String.fromCharCode(code)}x`;
|
|
if (name.trim() !== name) return; // trimming would remove edge whitespace controls
|
|
expect(validateContact({ ...valid, name }).name).toBe('invalid');
|
|
},
|
|
),
|
|
);
|
|
});
|
|
|
|
it('only ever yields known codes', () => {
|
|
fc.assert(
|
|
fc.property(fields, (values) => {
|
|
for (const code of Object.values(validateContact(values))) {
|
|
expect(['required', 'tooLong', 'invalid']).toContain(code);
|
|
}
|
|
}),
|
|
);
|
|
});
|
|
});
|