Adds the Contact section to the homepage
Adds the contact form with the live fields, validation and browser-side anti-spam, behind a coming-soon notice with a mail button until the API exists. A "Boek" link on a price card selects its package, tidies the URL and moves focus to the form. The form loads only when it comes near the screen, so the hero does not wait for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
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}
|
||||
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('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}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user