268 lines
10 KiB
TypeScript
268 lines
10 KiB
TypeScript
import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import LandingE from '../LandingE';
|
|
import { ThemeProvider } from '../../../theme/ThemeProvider';
|
|
import { ContactPreselectProvider } from '../../../shared/contact/ContactPreselectProvider';
|
|
import { clearMatchMedia, mockMatchMedia } from '../../../test/matchMedia';
|
|
import { REDUCED_MOTION_QUERY } from '../../../shared/hooks/usePrefersReducedMotion';
|
|
import {
|
|
aboutContent,
|
|
contactInfo,
|
|
packages,
|
|
processSteps,
|
|
} from '../../../features/landing/data/content';
|
|
|
|
class FakeIntersectionObserver {
|
|
static instances: FakeIntersectionObserver[] = [];
|
|
constructor(private readonly callback: IntersectionObserverCallback) {
|
|
FakeIntersectionObserver.instances.push(this);
|
|
}
|
|
observe() {}
|
|
unobserve() {}
|
|
disconnect() {}
|
|
takeRecords() {
|
|
return [];
|
|
}
|
|
trigger() {
|
|
this.callback(
|
|
[{ isIntersecting: true } as IntersectionObserverEntry],
|
|
this as unknown as IntersectionObserver,
|
|
);
|
|
}
|
|
}
|
|
|
|
const jsonResponse = (body: unknown, status = 200) =>
|
|
new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
|
|
interface SetupOptions {
|
|
tokenStatus?: number;
|
|
offerings?: unknown;
|
|
}
|
|
|
|
function setup({ tokenStatus = 404, offerings = packages }: SetupOptions = {}) {
|
|
const submits: RequestInit[] = [];
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (url: string, init?: RequestInit) => {
|
|
if (url.endsWith('/api/v1/offerings')) return jsonResponse(offerings);
|
|
if (url.endsWith('/contact-messages/token')) {
|
|
return tokenStatus === 200
|
|
? jsonResponse({ token: 'tok-1', minSeconds: 3, captcha: null })
|
|
: new Response(null, { status: tokenStatus });
|
|
}
|
|
if (url.endsWith('/contact-messages') && init?.method === 'POST') {
|
|
submits.push(init);
|
|
return jsonResponse({ status: 'received' }, 202);
|
|
}
|
|
return new Response(null, { status: 404 });
|
|
}),
|
|
);
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
render(
|
|
<QueryClientProvider client={queryClient}>
|
|
<ContactPreselectProvider>
|
|
<ThemeProvider loadVariant={() => Promise.resolve()}>
|
|
<LandingE />
|
|
</ThemeProvider>
|
|
</ContactPreselectProvider>
|
|
</QueryClientProvider>,
|
|
);
|
|
return { submits };
|
|
}
|
|
|
|
async function loaded() {
|
|
await waitFor(() =>
|
|
expect(screen.queryByTestId('package-card-skeleton')).not.toBeInTheDocument(),
|
|
);
|
|
}
|
|
|
|
describe('LandingE (variant E, five eras)', () => {
|
|
beforeEach(() => {
|
|
FakeIntersectionObserver.instances = [];
|
|
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver);
|
|
vi.stubEnv('VITE_API_BASE_URL', '');
|
|
window.localStorage.clear();
|
|
Element.prototype.scrollIntoView = vi.fn();
|
|
});
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.unstubAllEnvs();
|
|
clearMatchMedia();
|
|
});
|
|
|
|
it('has the landmarks, one h1, a skip link and a way back to B', async () => {
|
|
setup();
|
|
expect(screen.getAllByRole('heading', { level: 1 })).toHaveLength(1);
|
|
expect(screen.getByRole('heading', { level: 1, name: 'Websites, apps & meer.' })).toBeVisible();
|
|
expect(screen.getByRole('banner')).toBeInTheDocument();
|
|
expect(screen.getByRole('main')).toHaveAttribute('id', 'main');
|
|
expect(screen.getByRole('contentinfo')).toBeInTheDocument();
|
|
expect(screen.getByTestId('skip-link')).toBeInTheDocument();
|
|
expect(screen.getByTestId('variant-toggle-button')).toBeInTheDocument();
|
|
expect(screen.getByTestId('mode-toggle-button')).toBeInTheDocument();
|
|
expect(screen.getByRole('navigation', { name: 'Hoofdnavigatie' })).toBeInTheDocument();
|
|
await loaded();
|
|
});
|
|
|
|
it('names every era section and orders headings logically', async () => {
|
|
setup();
|
|
await loaded();
|
|
for (const name of [
|
|
'Eén ontwikkelaar, korte lijnen',
|
|
'Drie manieren om te starten',
|
|
'Van idee naar live in drie stappen',
|
|
contactInfo.heading,
|
|
]) {
|
|
expect(screen.getByRole('heading', { level: 2, name })).toBeInTheDocument();
|
|
}
|
|
expect(screen.getAllByRole('region')).toHaveLength(5);
|
|
});
|
|
|
|
it('shows the about text from the content with the photo', () => {
|
|
setup();
|
|
aboutContent.paragraphs.forEach((paragraph) =>
|
|
expect(screen.getByText(paragraph)).toBeInTheDocument(),
|
|
);
|
|
expect(
|
|
screen.getByRole('img', { name: 'Bryan Slop, oprichter van SLP Software' }),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it('shows the 2006 package cards from the shared logic with a star on the featured one', async () => {
|
|
setup();
|
|
await loaded();
|
|
packages.forEach((pkg) => {
|
|
const card = screen.getByTestId(`package-card-${pkg.id}`);
|
|
expect(within(card).getByRole('heading', { level: 3, name: pkg.title })).toBeInTheDocument();
|
|
expect(within(card).getByText(pkg.price)).toBeInTheDocument();
|
|
});
|
|
const featured = packages.find((pkg) => pkg.featured)!;
|
|
expect(
|
|
within(screen.getByTestId(`package-card-${featured.id}`)).getByText('Meest gekozen!'),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it('preselects the package for the contact form when a card button is used', async () => {
|
|
setup({ tokenStatus: 200 });
|
|
await loaded();
|
|
await act(async () => {
|
|
FakeIntersectionObserver.instances.forEach((o) => o.trigger());
|
|
});
|
|
await screen.findByTestId('contact-form');
|
|
fireEvent.click(screen.getByTestId('package-cta-pakket_02'));
|
|
const select = (await screen.findByLabelText(/^Onderwerp/)) as HTMLSelectElement;
|
|
await waitFor(() => expect(select.value).toBe('website'));
|
|
});
|
|
|
|
it('shows filter tabs for 4+ card packages and filters', async () => {
|
|
const category = (slug: string, c: string | null) => ({
|
|
id: `id_${slug}`,
|
|
slug,
|
|
title: `Titel ${slug}`,
|
|
description: `Omschrijving ${slug}`,
|
|
price: '€ 100',
|
|
priceNote: 'eenmalig',
|
|
features: ['een'],
|
|
ctaLabel: `Kies ${slug}`,
|
|
category: c,
|
|
});
|
|
setup({
|
|
offerings: [
|
|
category('a', 'Websites'),
|
|
category('b', 'Websites'),
|
|
category('c', 'Onderhoud'),
|
|
category('d', 'Onderhoud'),
|
|
],
|
|
});
|
|
await loaded();
|
|
const group = screen.getByRole('group', { name: 'Filter pakketten' });
|
|
fireEvent.click(within(group).getByTestId('package-chip-cat:Onderhoud'));
|
|
expect(screen.queryByTestId('package-card-id_a')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('package-card-id_c')).toBeInTheDocument();
|
|
expect(screen.getByTestId('packages-status')).toHaveTextContent('2');
|
|
});
|
|
|
|
it('shows the 1998 steps with progress and the done card; decorations are aria-hidden', async () => {
|
|
setup();
|
|
await loaded();
|
|
processSteps.forEach((step, index) => {
|
|
const card = screen.getByTestId(`e-step-${index + 1}`);
|
|
expect(
|
|
within(card).getByText(`Stap ${index + 1} van ${processSteps.length}`),
|
|
).toBeInTheDocument();
|
|
expect(within(card).getByRole('heading', { level: 3, name: step.title })).toBeInTheDocument();
|
|
expect(within(card).getByText(step.description)).toBeInTheDocument();
|
|
});
|
|
expect(
|
|
within(screen.getByTestId('e-step-done')).getByRole('heading', { name: 'Je site is live!' }),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByTestId('e-marquee')).toHaveAttribute('aria-hidden', 'true');
|
|
expect(screen.getByTestId('e-scanlines')).toHaveAttribute('aria-hidden', 'true');
|
|
});
|
|
|
|
it('keeps the mailto prompt in the terminal while the API is closed', async () => {
|
|
setup({ tokenStatus: 404 });
|
|
await act(async () => {
|
|
FakeIntersectionObserver.instances.forEach((o) => o.trigger());
|
|
});
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId('contact-panel')).toHaveAttribute('data-availability', 'closed'),
|
|
);
|
|
expect(screen.getByTestId('mailto-panel')).toBeInTheDocument();
|
|
expect(screen.getByTestId('contact-email-link')).toHaveAttribute(
|
|
'href',
|
|
`mailto:${contactInfo.email}`,
|
|
);
|
|
expect(screen.queryByTestId('contact-form')).not.toBeInTheDocument();
|
|
await loaded();
|
|
});
|
|
|
|
it('sends a message through the terminal form and shows the confirmation', async () => {
|
|
const { submits } = setup({ tokenStatus: 200 });
|
|
await act(async () => {
|
|
FakeIntersectionObserver.instances.forEach((o) => o.trigger());
|
|
});
|
|
await screen.findByTestId('contact-form');
|
|
await waitFor(() =>
|
|
expect(screen.getByRole('option', { name: 'WEBSITE' })).toBeInTheDocument(),
|
|
);
|
|
fireEvent.change(screen.getByLabelText(/^Naam/), { target: { value: 'Jan Jansen' } });
|
|
fireEvent.change(screen.getByLabelText(/^E-mail/), { target: { value: 'jan@example.nl' } });
|
|
fireEvent.change(screen.getByLabelText(/^Onderwerp/), { target: { value: 'website' } });
|
|
fireEvent.change(screen.getByLabelText(/^Bericht/), {
|
|
target: { value: 'Ik wil graag een offerte.' },
|
|
});
|
|
fireEvent.submit(screen.getByTestId('contact-form'));
|
|
const success = await screen.findByTestId('contact-success');
|
|
expect(success).toHaveAttribute('role', 'status');
|
|
expect(submits).toHaveLength(1);
|
|
fireEvent.click(screen.getByTestId('contact-reset'));
|
|
expect(await screen.findByTestId('contact-form')).toBeInTheDocument();
|
|
});
|
|
|
|
it('shows field errors in the terminal form and does not send an invalid form', async () => {
|
|
const { submits } = setup({ tokenStatus: 200 });
|
|
await act(async () => {
|
|
FakeIntersectionObserver.instances.forEach((o) => o.trigger());
|
|
});
|
|
await screen.findByTestId('contact-form');
|
|
fireEvent.submit(screen.getByTestId('contact-form'));
|
|
expect((await screen.findAllByTestId('field-error')).length).toBeGreaterThan(0);
|
|
expect(submits).toHaveLength(0);
|
|
});
|
|
|
|
it('stops the decorative motion under prefers-reduced-motion via CSS only (nothing auto-moves)', async () => {
|
|
mockMatchMedia({ [REDUCED_MOTION_QUERY]: true });
|
|
setup();
|
|
await loaded();
|
|
// Motion lives in era.css behind the reduced-motion media query; the markup stays the same and
|
|
// all moving parts are decorative.
|
|
expect(screen.getByTestId('e-marquee')).toHaveAttribute('aria-hidden', 'true');
|
|
expect(screen.getByTestId('e-codeline').querySelector('[aria-hidden="true"]')).not.toBeNull();
|
|
});
|
|
});
|