H5: admin-scherm voor de hero (formulier, slides, upload, focus-editor met voorbeeld, TypeScript-tweeling van FocusMath)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 16:07:49 +01:00
co-authored by Claude Sonnet 5.5
parent 4369c905cd
commit 5559d459c3
21 changed files with 1635 additions and 15 deletions
@@ -0,0 +1,273 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderApp, mockAuthenticated } from '@/test/utils';
import { emptyHero, failNextUpload, getSavedHeroRequests, resetMockHero, setMockHero, slide } from '@/features/hero/mocks/handlers';
import { _resetSetupStatusCache } from '@/router';
const T = { timeout: 10000 };
beforeEach(() => {
_resetSetupStatusCache();
resetMockHero();
});
async function openPage() {
mockAuthenticated();
renderApp('/hero');
return screen.findByTestId('hero-form', {}, T);
}
describe('Hero page', () => {
it('shows an empty form and the not-live badge when there is no hero yet', async () => {
await openPage();
expect(screen.getByTestId('hero-live-badge')).toHaveTextContent(/Niet live|Not live/);
expect(screen.getByTestId('hero-slides-empty')).toBeInTheDocument();
expect(screen.getByTestId('hero-title-input')).toHaveValue('');
expect(screen.getByTestId('hero-display-single')).toBeChecked();
});
it('is reachable from the navigation for an administrator', async () => {
mockAuthenticated();
renderApp('/dashboard');
await userEvent.click(await screen.findByTestId('nav-hero', {}, T));
expect(await screen.findByTestId('hero-form', {}, T)).toBeInTheDocument();
});
it('saves the texts, the buttons and the schedule, and shows the hero as live', async () => {
await openPage();
await userEvent.type(screen.getByTestId('hero-title-input'), 'Websites,{enter}apps{enter}& meer.');
await userEvent.type(screen.getByTestId('hero-lead'), 'Een lead.');
await userEvent.click(screen.getByTestId('hero-display-slideshow'));
await userEvent.type(screen.getByTestId('hero-button-0-label'), 'Bekijk pakketten');
await userEvent.type(screen.getByTestId('hero-button-0-target'), '#pakketten');
await userEvent.type(screen.getByTestId('hero-button-1-label'), 'Plan een gesprek');
await userEvent.type(screen.getByTestId('hero-button-1-target'), '#contact');
await userEvent.click(screen.getByTestId('hero-save'));
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
expect(getSavedHeroRequests()[0]).toMatchObject({
title: 'Websites,\napps\n& meer.',
tagline: null,
lead: 'Een lead.',
display: 'slideshow',
buttons: [
{ label: 'Bekijk pakketten', target: '#pakketten' },
{ label: 'Plan een gesprek', target: '#contact' },
],
isActive: true,
startsAt: null,
endsAt: null,
slides: [],
});
});
it('does not save without a title and says why', async () => {
await openPage();
await userEvent.click(screen.getByTestId('hero-save'));
expect(await screen.findByTestId('hero-title-error')).toHaveTextContent(/Een titel is verplicht\.|A title is required\./);
expect(getSavedHeroRequests()).toHaveLength(0);
});
it.each([
['javascript:alert(1)', 'hero-button-0-target-error', /Kies een #anker|Choose an #anchor/],
['//evil.example', 'hero-button-0-target-error', /Kies een #anker|Choose an #anchor/],
['http://example.com', 'hero-button-0-target-error', /Kies een #anker|Choose an #anchor/],
])('rejects the button target %s', async (target, errorId, message) => {
await openPage();
await userEvent.type(screen.getByTestId('hero-title-input'), 'Titel');
await userEvent.type(screen.getByTestId('hero-button-0-label'), 'Knop');
await userEvent.type(screen.getByTestId('hero-button-0-target'), target);
await userEvent.click(screen.getByTestId('hero-save'));
expect(await screen.findByTestId(errorId)).toHaveTextContent(message);
expect(getSavedHeroRequests()).toHaveLength(0);
});
it('wants a button text and a target together', async () => {
await openPage();
await userEvent.type(screen.getByTestId('hero-title-input'), 'Titel');
await userEvent.type(screen.getByTestId('hero-button-1-label'), 'Alleen een tekst');
await userEvent.click(screen.getByTestId('hero-save'));
expect(await screen.findByTestId('hero-button-1-target-error')).toHaveTextContent(/Vul zowel de tekst als het doel|Fill in both the text and the target/);
});
it('wants the end after the start', async () => {
await openPage();
await userEvent.type(screen.getByTestId('hero-title-input'), 'Titel');
fireEvent.change(screen.getByTestId('hero-starts'), { target: { value: '2026-12-10T10:00' } });
fireEvent.change(screen.getByTestId('hero-ends'), { target: { value: '2026-12-01T10:00' } });
await userEvent.click(screen.getByTestId('hero-save'));
expect(await screen.findByTestId('hero-ends-error')).toHaveTextContent(/Het einde moet na het begin liggen\.|The end must be after the start\./);
});
});
describe('Slides', () => {
const withTwoSlides = () =>
setMockHero({ ...emptyHero(), id: 'h1', title: 'Andrea Slop', isLive: true, updatedAt: '2026-11-16T12:00:00Z', slides: [slide(1), slide(2)] });
it('lists the saved slides with their description, and the live badge', async () => {
withTwoSlides();
await openPage();
expect(screen.getByTestId('hero-live-badge')).toHaveTextContent(/^(Live)$/);
expect(screen.getByTestId('hero-slide-0-alt')).toHaveValue('Foto 1');
expect(screen.getByTestId('hero-slide-1-alt')).toHaveValue('Foto 2');
});
it('reorders slides and saves the new order with the slide ids', async () => {
withTwoSlides();
await openPage();
await userEvent.click(screen.getByTestId('hero-slide-1-up'));
await userEvent.click(screen.getByTestId('hero-save'));
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
const { slides } = getSavedHeroRequests()[0];
expect(slides.map((s) => s.alt)).toEqual(['Foto 2', 'Foto 1']);
expect(slides[0].id).toBe('00000000-0000-0000-0000-000000000002');
});
it('cannot move the first slide up or the last one down', async () => {
withTwoSlides();
await openPage();
expect(screen.getByTestId('hero-slide-0-up')).toBeDisabled();
expect(screen.getByTestId('hero-slide-1-down')).toBeDisabled();
});
it('removes a slide from the hero, switches one off and marks one decorative', async () => {
withTwoSlides();
await openPage();
await userEvent.click(screen.getByTestId('hero-slide-1-remove'));
await userEvent.click(screen.getByTestId('hero-slide-0-active'));
await userEvent.click(screen.getByTestId('hero-slide-0-decorative'));
expect(screen.getByTestId('hero-slide-0-alt')).toBeDisabled();
await userEvent.click(screen.getByTestId('hero-save'));
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
expect(getSavedHeroRequests()[0].slides).toEqual([
{ id: '00000000-0000-0000-0000-000000000001', imageId: '10000000-0000-0000-0000-000000000001', alt: null, decorative: true, isActive: false },
]);
});
it('adds an uploaded image as a slide that needs a description before it can be saved', async () => {
withTwoSlides();
await openPage();
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'foto.jpg', { type: 'image/jpeg' }));
expect(await screen.findByTestId('hero-slide-2')).toBeInTheDocument();
await userEvent.click(screen.getByTestId('hero-save'));
expect(await screen.findByTestId('hero-slide-2-alt-error')).toHaveTextContent(/Geef een beschrijving|Give a description/);
expect(getSavedHeroRequests()).toHaveLength(0);
await userEvent.type(screen.getByTestId('hero-slide-2-alt'), 'Nieuwe foto');
await userEvent.click(screen.getByTestId('hero-save'));
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
const slides = getSavedHeroRequests()[0].slides;
expect(slides).toHaveLength(3);
expect(slides[2]).toMatchObject({ id: null, alt: 'Nieuwe foto', decorative: false, isActive: true });
});
it('shows the message of the server when an upload is refused', async () => {
await openPage();
failNextUpload({ status: 400, error: 'unsupported_format', message: 'Dit bestandstype wordt niet ondersteund.' });
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.svg', { type: 'image/svg+xml' }));
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent('Dit bestandstype wordt niet ondersteund.');
expect(screen.getByTestId('hero-slides-empty')).toBeInTheDocument();
});
it('notes that only the first slides are published when there are more than the ceiling', async () => {
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', maxPublishedSlides: 1, slides: [slide(1), slide(2)] });
await openPage();
expect(screen.getByTestId('hero-ceiling-note')).toHaveTextContent(/Alleen de eerste 1 afbeeldingen|Only the first 1 images/);
});
});
describe('Focus dialog', () => {
const open = async () => {
setMockHero({ ...emptyHero(), id: 'h1', title: 'Andrea Slop', isLive: true, slides: [slide(1)] });
await openPage();
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
return screen.findByTestId('focus-dialog');
};
it('starts with the stored point and shows both previews', async () => {
await open();
expect(screen.getByTestId('focus-x')).toHaveValue(50);
expect(screen.getByTestId('focus-y')).toHaveValue(50);
expect(screen.getByTestId('focus-preview-wide')).toBeInTheDocument();
expect(screen.getByTestId('focus-preview-tall')).toBeInTheDocument();
});
it('moves the point with the keyboard', async () => {
await open();
const area = screen.getByTestId('focus-area');
area.focus();
await userEvent.keyboard('{ArrowRight}{ArrowRight}{Shift>}{ArrowDown}{/Shift}');
expect(screen.getByTestId('focus-x')).toHaveValue(52);
expect(screen.getByTestId('focus-y')).toHaveValue(55);
});
it('saves a general point and an own setting for the tall display', async () => {
await open();
fireEvent.change(screen.getByTestId('focus-x'), { target: { value: '30' } });
await userEvent.click(screen.getByTestId('focus-target-tall'));
expect(screen.getByTestId('focus-x')).toBeDisabled();
await userEvent.click(screen.getByTestId('focus-override-tall'));
fireEvent.change(screen.getByTestId('focus-x'), { target: { value: '80' } });
fireEvent.change(screen.getByTestId('focus-zoom'), { target: { value: '2' } });
await userEvent.click(screen.getByTestId('focus-save'));
await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument());
// The dialog closed after the server accepted it; reopening shows the stored values.
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
expect(screen.getByTestId('focus-x')).toHaveValue(30);
await userEvent.click(screen.getByTestId('focus-target-tall'));
expect(screen.getByTestId('focus-override-tall')).toBeChecked();
expect(screen.getByTestId('focus-x')).toHaveValue(80);
expect(screen.getByTestId('focus-zoom')).toHaveValue('2');
});
it('goes back to the general point when the own setting is switched off', async () => {
await open();
await userEvent.click(screen.getByTestId('focus-target-wide'));
await userEvent.click(screen.getByTestId('focus-override-wide'));
fireEvent.change(screen.getByTestId('focus-x'), { target: { value: '10' } });
await userEvent.click(screen.getByTestId('focus-override-wide'));
expect(screen.getByTestId('focus-x')).toHaveValue(50);
expect(screen.getByTestId('focus-x')).toBeDisabled();
});
it('closes without saving', async () => {
await open();
await userEvent.click(screen.getByTestId('focus-cancel'));
await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument());
});
});