Files
AndreaSlopFotografie/web/src/components/ContactForm/ContactForm.test.tsx
T
SluijsensandClaude Opus 5.5 26a8bd9469 Shares the contact form, contact card and privacy line
ContactPanel holds the coming-soon gate, the shoot options, the package
chosen via ?pakket= and the form, which loads as its own chunk either
when it comes near the screen (homepage) or right away (contact page).
ContactDetails shows the ways to reach Andrea as a card with icons. A
privacy line now sits under the form and under the coming-soon notice,
and the reply time in the thank-you comes from the site content.
Instagram is listed before Facebook, so the footer and hero follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:26:48 +01:00

237 lines
8.9 KiB
TypeScript

import { useState } from 'react';
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import packages from '@/content/packages';
import siteContent from '@/content/siteContent';
import { shootOptions } from '@/lib/shootOptions';
import { axeResults } from '@/test/axe';
import { renderWithProviders } from '@/test/renderWithProviders';
import ContactForm from './ContactForm';
const options = shootOptions(packages);
// The time check needs over 3 s between showing and sending (BR-SPAM-01)
let now = 0;
beforeEach(() => {
now = 0;
vi.spyOn(Date, 'now').mockImplementation(() => now);
});
const renderForm = (
props: Partial<React.ComponentProps<typeof ContactForm>> = {},
) =>
renderWithProviders(
<ContactForm
idPrefix="test"
options={options}
mailSubject="Vraag via andreaslop.nl"
email={siteContent.email}
replyTime="1-2 werkdagen"
submit={vi.fn().mockResolvedValue(undefined)}
{...props}
/>,
);
const field = (name: string) => screen.getByTestId(`contact-form-${name}`);
const type = (name: string, value: string) =>
fireEvent.change(field(name), { target: { value } });
const fillIn = () => {
type('naam', 'Demi');
type('email', 'demi@example.nl');
type('shoot', 'newborn');
type('bericht', 'Wanneer heb je plek?');
};
const send = () => {
now = 10_000;
fireEvent.click(screen.getByTestId('contact-form-submit'));
};
describe('ContactForm', () => {
it('shows the four labelled fields and the button', async () => {
const { container } = await renderForm();
expect(screen.getByLabelText(/Naam/)).toBe(field('naam'));
expect(screen.getByLabelText(/E-mail/)).toBe(field('email'));
expect(screen.getByLabelText(/In welke shoot/)).toBe(field('shoot'));
expect(screen.getByLabelText(/Stel hier je vraag/)).toBe(
field('bericht'),
);
expect(
screen.getByRole('option', { name: 'Kies een optie...' }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: '(Aan)vraag versturen' }),
).toBeInTheDocument();
expect(await axeResults(container)).toHaveNoViolations();
});
it('shows nothing when tabbing through an untouched field', async () => {
await renderForm();
fireEvent.blur(field('naam'));
expect(screen.queryByText('Vul je naam in.')).toBeNull();
});
it('checks a field on leaving once typed in, and clears it once fine', async () => {
await renderForm();
type('email', 'demi@');
fireEvent.blur(field('email'));
expect(
screen.getByText('Dit e-mailadres is ongeldig.'),
).toBeInTheDocument();
expect(field('email')).toHaveAttribute('aria-invalid', 'true');
type('email', 'demi@example.nl');
expect(screen.queryByText('Dit e-mailadres is ongeldig.')).toBeNull();
});
it('stops an incomplete submit and focuses the first invalid field', async () => {
const submit = vi.fn();
await renderForm({ submit });
type('naam', 'Demi');
send();
expect(screen.getByText('Vul je e-mailadres in.')).toBeInTheDocument();
expect(screen.getByText('Kies een soort shoot.')).toBeInTheDocument();
expect(screen.getByText('Schrijf hier je vraag.')).toBeInTheDocument();
expect(field('email')).toHaveFocus();
expect(submit).not.toHaveBeenCalled();
});
it('sends the trimmed form and shows the thank-you with focus', async () => {
const submit = vi.fn().mockResolvedValue(undefined);
await renderForm({ submit });
fillIn();
type('naam', ' Demi ');
send();
expect(screen.getByTestId('contact-form-submit')).toHaveTextContent(
'Versturen…',
);
expect(submit).toHaveBeenCalledWith({
naam: 'Demi',
email: 'demi@example.nl',
shoot: 'newborn',
bericht: 'Wanneer heb je plek?',
});
const thanks = await screen.findByTestId('contact-form-sent');
expect(thanks).toHaveTextContent(
'Bedankt voor je bericht! Ik reageer binnen 1-2 werkdagen.',
);
await waitFor(() => expect(thanks).toHaveFocus());
expect(screen.queryByTestId('contact-form')).toBeNull();
});
it('takes the reply time for the thank-you from the content', async () => {
await renderForm({ replyTime: '3 werkdagen' });
fillIn();
send();
expect(
await screen.findByTestId('contact-form-sent'),
).toHaveTextContent('Ik reageer binnen 3 werkdagen.');
});
it('shows the privacy line under the submit button', async () => {
await renderForm();
const form = screen.getByTestId('contact-form');
expect(form).toContainElement(screen.getByTestId('privacy-note'));
expect(
screen.getByRole('link', { name: 'privacyverklaring' }),
).toHaveAttribute('href', '/privacyverklaring');
});
it('keeps the input and offers the mail link when sending fails', async () => {
await renderForm({ submit: vi.fn().mockRejectedValue(new Error('x')) });
fillIn();
send();
const status = await screen.findByRole('alert');
expect(status).toHaveTextContent(
'Versturen lukte niet. Probeer het later nog eens of mail naar info@andreaslop.nl',
);
expect(
screen.getByRole('link', { name: 'info@andreaslop.nl' }),
).toHaveAttribute(
'href',
'mailto:info@andreaslop.nl?subject=Vraag%20via%20andreaslop.nl',
);
expect(field('naam')).toHaveValue('Demi');
expect(screen.getByTestId('contact-form-submit')).not.toBeDisabled();
});
it('treats a filled honeypot as spam: thank-you, nothing sent', async () => {
const submit = vi.fn();
const { container } = await renderForm({ submit });
fillIn();
fireEvent.change(
container.querySelector('input[name="website"]') as Element,
{ target: { value: 'https://spam.example' } },
);
send();
expect(screen.getByTestId('contact-form-sent')).toBeInTheDocument();
expect(submit).not.toHaveBeenCalled();
});
it('treats a submit within 3 seconds as spam', async () => {
const submit = vi.fn();
await renderForm({ submit });
fillIn();
now = 1_000;
fireEvent.click(screen.getByTestId('contact-form-submit'));
expect(screen.getByTestId('contact-form-sent')).toBeInTheDocument();
expect(submit).not.toHaveBeenCalled();
});
it('ignores a second submit while sending', async () => {
let finish = () => {};
const submit = vi.fn(
() => new Promise<void>((resolve) => (finish = resolve)),
);
await renderForm({ submit });
fillIn();
send();
fireEvent.click(screen.getByTestId('contact-form-submit'));
expect(submit).toHaveBeenCalledTimes(1);
await act(async () => finish());
});
it('takes over a preselected package, also again after a change', async () => {
// A second "Boek" click for the same package raises the key
function Harness() {
const [preselect, setPreselect] = useState({
shoot: 'newborn',
key: 1,
});
return (
<>
<button
type="button"
onClick={() =>
setPreselect((p) => ({ ...p, key: p.key + 1 }))
}
>
nog eens
</button>
<ContactForm
idPrefix="test"
options={options}
preselect={preselect}
mailSubject="Vraag via andreaslop.nl"
email={siteContent.email}
replyTime="1-2 werkdagen"
/>
</>
);
}
await renderWithProviders(<Harness />);
expect(field('shoot')).toHaveValue('newborn');
type('shoot', 'gezin');
expect(field('shoot')).toHaveValue('gezin');
fireEvent.click(screen.getByRole('button', { name: 'nog eens' }));
expect(field('shoot')).toHaveValue('newborn');
});
it('keeps the honeypot away from people and assistive technology', async () => {
const { container } = await renderForm();
const honeypot = container.querySelector('input[name="website"]');
expect(honeypot).toHaveAttribute('tabindex', '-1');
expect(honeypot?.closest('[aria-hidden="true"]')).not.toBeNull();
expect(container.querySelector('[data-robot-check]')).not.toBeNull();
});
});