1174 lines
57 KiB
TypeScript
1174 lines
57 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { fireEvent, screen, waitFor, within } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { server } from '@/mocks/server';
|
|
import { API_BASE } from '@/features/auth/mocks/fixtures';
|
|
import { renderApp, mockAuthenticated } from '@/test/utils';
|
|
import { emptyHero, failNextUpload, finishMockRecut, getRetriedImages, getSavedHeroRequests, resetMockHero, setMockHero, slide } from '@/features/hero/mocks/handlers';
|
|
import { _resetSetupStatusCache } from '@/router';
|
|
|
|
const T = { timeout: 10000 };
|
|
|
|
beforeEach(() => {
|
|
_resetSetupStatusCache();
|
|
resetMockHero();
|
|
window.sessionStorage.clear();
|
|
});
|
|
|
|
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('shows a placeholder, and no focus button, for an image whose variants are still being cut', async () => {
|
|
const waiting = slide(1);
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...waiting, image: { ...waiting.image, variants: [], src: '', processing: true } }] });
|
|
|
|
await openPage();
|
|
|
|
expect(await screen.findByTestId('hero-slide-0-processing', {}, T)).toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-slide-0-focus')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('shows how many photos are ready, with an accessible progress bar, and hides it when all are ready', async () => {
|
|
const waiting = (n: number) => ({ ...slide(n), image: { ...slide(n).image, variants: [], src: '', processing: true } });
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), waiting(2), waiting(3)] });
|
|
|
|
await openPage();
|
|
|
|
expect(await screen.findByTestId('hero-processing-count', {}, T)).toHaveTextContent(/1 van 3|1 of 3/);
|
|
const bar = screen.getByRole('progressbar');
|
|
expect(bar).toHaveAttribute('aria-valuenow', '1');
|
|
expect(bar).toHaveAttribute('aria-valuemax', '3');
|
|
});
|
|
|
|
it('keeps unsaved edits when the background refresh brings a finished photo', async () => {
|
|
const waiting = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'Oude titel', slides: [waiting] });
|
|
await openPage();
|
|
expect(await screen.findByTestId('hero-slide-0-processing', {}, T)).toBeInTheDocument();
|
|
|
|
const title = screen.getByTestId('hero-title-input');
|
|
await userEvent.clear(title);
|
|
await userEvent.type(title, 'Nieuwe titel');
|
|
const alt = screen.getByTestId('hero-slide-0-alt');
|
|
await userEvent.clear(alt);
|
|
await userEvent.type(alt, 'Mijn eigen alt');
|
|
|
|
// The server finishes the photo; the page asks again by itself (every 5 seconds).
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'Oude titel', slides: [slide(1)] });
|
|
|
|
await waitFor(() => expect(screen.queryByTestId('hero-slide-0-processing')).not.toBeInTheDocument(), { timeout: 12000 });
|
|
expect(screen.getByTestId('hero-title-input')).toHaveValue('Nieuwe titel');
|
|
expect(screen.getByTestId('hero-slide-0-alt')).toHaveValue('Mijn eigen alt');
|
|
expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled();
|
|
}, 20000);
|
|
|
|
it('does not show the progress panel when every photo is ready', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
|
|
await openPage();
|
|
|
|
expect(screen.queryByTestId('hero-processing')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('shows a failed photo with a retry that asks the server to try again', async () => {
|
|
const failed = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true, failed: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [failed] });
|
|
|
|
await openPage();
|
|
await userEvent.click(await screen.findByTestId('hero-processing-retry', {}, T));
|
|
|
|
await waitFor(() => expect(getRetriedImages()).toEqual([failed.imageId]), T);
|
|
expect(screen.getByTestId('hero-processing-failed')).toBeInTheDocument();
|
|
});
|
|
|
|
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 and the buttons, 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' },
|
|
],
|
|
slides: [],
|
|
});
|
|
});
|
|
|
|
it('does not save without a title and says why', async () => {
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-lead'), 'Alleen een lead');
|
|
|
|
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 onderdeel van de pagina|Choose a part of the page/],
|
|
['//evil.example', 'hero-button-0-target-error', /Kies een onderdeel van de pagina|Choose a part of the page/],
|
|
['http://example.com', 'hero-button-0-target-error', /Kies een onderdeel van de pagina|Choose a part of the page/],
|
|
])('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('has no switch and no schedule: the hero is always on', async () => {
|
|
await openPage();
|
|
|
|
expect(screen.queryByTestId('hero-active')).not.toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-starts')).not.toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-ends')).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
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, shows both previews and says the change needs a save', 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();
|
|
expect(screen.getByTestId('focus-apply-hint')).toHaveTextContent(/pas nadat je de hero opslaat|only takes effect when you save the hero/);
|
|
});
|
|
|
|
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('keeps a change locally until the hero is saved, then sends it with the save', 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());
|
|
expect(getSavedHeroRequests()).toHaveLength(0);
|
|
expect(screen.getByTestId('hero-slide-0-focus-pending')).toBeInTheDocument();
|
|
|
|
// Reopening shows the pending 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');
|
|
await userEvent.click(screen.getByTestId('focus-cancel'));
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
|
expect(getSavedHeroRequests()[0].imageFocus).toEqual([
|
|
{
|
|
imageId: '10000000-0000-0000-0000-000000000001',
|
|
focus: { x: 0.3, y: 0.5, zoom: 1 },
|
|
wide: null,
|
|
tall: { x: 0.8, y: 0.5, zoom: 2 },
|
|
},
|
|
]);
|
|
await waitFor(() => expect(screen.queryByTestId('hero-slide-0-focus-pending')).not.toBeInTheDocument());
|
|
});
|
|
|
|
it('sends no focus edits when nothing was changed', async () => {
|
|
await open();
|
|
await userEvent.click(screen.getByTestId('focus-cancel'));
|
|
// Something else changed, so there is something to save; the cancelled focus is not part of it.
|
|
await userEvent.type(screen.getByTestId('hero-tagline'), 'x');
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
|
expect(getSavedHeroRequests()[0].imageFocus).toEqual([]);
|
|
});
|
|
|
|
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('drops a change when the dialog is cancelled', async () => {
|
|
await open();
|
|
fireEvent.change(screen.getByTestId('focus-x'), { target: { value: '10' } });
|
|
|
|
await userEvent.click(screen.getByTestId('focus-cancel'));
|
|
|
|
await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument());
|
|
expect(screen.queryByTestId('hero-slide-0-focus-pending')).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe('Upload problems', () => {
|
|
it('refuses a file over 10 MB without sending it', async () => {
|
|
await openPage();
|
|
const big = new File(['x'], 'groot.jpg', { type: 'image/jpeg' });
|
|
Object.defineProperty(big, 'size', { value: 11 * 1024 * 1024 });
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), big);
|
|
|
|
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/maximaal 10 MB|at most 10 MB/);
|
|
expect(screen.getByTestId('hero-slides-empty')).toBeInTheDocument();
|
|
});
|
|
|
|
it('explains a 413 of the proxy (no JSON body)', async () => {
|
|
await openPage();
|
|
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => new HttpResponse('<html>413 Request Entity Too Large</html>', { status: 413 })));
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.jpg', { type: 'image/jpeg' }));
|
|
|
|
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/maximaal 10 MB|at most 10 MB/);
|
|
});
|
|
|
|
it('asks to try again when the connection breaks off', async () => {
|
|
await openPage();
|
|
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => HttpResponse.error()));
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.jpg', { type: 'image/jpeg' }));
|
|
|
|
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/afgebroken|interrupted/);
|
|
});
|
|
|
|
it('says when there are too many uploads in a row', async () => {
|
|
await openPage();
|
|
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => new HttpResponse(null, { status: 429 })));
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), new File(['x'], 'x.jpg', { type: 'image/jpeg' }));
|
|
|
|
expect(await screen.findByTestId('hero-upload-error')).toHaveTextContent(/te veel uploads|Too many uploads/);
|
|
});
|
|
});
|
|
|
|
|
|
describe('Unsaved changes', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
const withSlides = () => setMockHero({ ...emptyHero(), id: 'h1', title: 'Titel', isLive: true, updatedAt: '2026-11-16T12:00:00Z', slides: [slide(1), slide(2)] });
|
|
|
|
it('shows a notice as soon as something changed, and removes it when the change is undone', async () => {
|
|
withSlides();
|
|
await openPage();
|
|
expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument();
|
|
|
|
const title = screen.getByTestId('hero-title-input');
|
|
await userEvent.type(title, 'X');
|
|
expect(await screen.findByTestId('hero-unsaved')).toHaveTextContent(/Niet-opgeslagen wijzigingen|Unsaved changes/);
|
|
|
|
await userEvent.type(title, '{backspace}');
|
|
await waitFor(() => expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument());
|
|
});
|
|
|
|
it('counts a reordered, removed, decorative or switched-off slide as a change', async () => {
|
|
withSlides();
|
|
await openPage();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-1-up'));
|
|
expect(await screen.findByTestId('hero-unsaved')).toBeInTheDocument();
|
|
await userEvent.click(screen.getByTestId('hero-slide-1-up'));
|
|
await waitFor(() => expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument());
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-0-active'));
|
|
expect(await screen.findByTestId('hero-unsaved')).toBeInTheDocument();
|
|
});
|
|
|
|
it('is gone after a successful save, and comes back for the next change', async () => {
|
|
withSlides();
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), '!');
|
|
expect(await screen.findByTestId('hero-unsaved')).toBeInTheDocument();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
|
await waitFor(() => expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument());
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), '?');
|
|
expect(await screen.findByTestId('hero-unsaved')).toBeInTheDocument();
|
|
});
|
|
|
|
it('asks the browser to warn when the tab is closed, only while there are unsaved changes', async () => {
|
|
withSlides();
|
|
await openPage();
|
|
const clean = new Event('beforeunload', { cancelable: true });
|
|
window.dispatchEvent(clean);
|
|
expect(clean.defaultPrevented).toBe(false);
|
|
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'X');
|
|
await screen.findByTestId('hero-unsaved');
|
|
const dirty = new Event('beforeunload', { cancelable: true });
|
|
window.dispatchEvent(dirty);
|
|
|
|
expect(dirty.defaultPrevented).toBe(true);
|
|
});
|
|
|
|
it('asks before leaving through the menu: stay keeps the page and the changes, discard leaves', async () => {
|
|
withSlides();
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'X');
|
|
await screen.findByTestId('hero-unsaved');
|
|
|
|
await userEvent.click(screen.getByTestId('nav-dashboard'));
|
|
expect(await screen.findByTestId('hero-leave-dialog', {}, T)).toBeInTheDocument();
|
|
await userEvent.click(screen.getByTestId('hero-leave-stay'));
|
|
await waitFor(() => expect(screen.queryByTestId('hero-leave-dialog')).not.toBeInTheDocument());
|
|
expect(screen.getByTestId('hero-title-input')).toHaveValue('TitelX');
|
|
|
|
await userEvent.click(screen.getByTestId('nav-dashboard'));
|
|
await userEvent.click(await screen.findByTestId('hero-leave-discard', {}, T));
|
|
await waitFor(() => expect(screen.queryByTestId('hero-form')).not.toBeInTheDocument(), T);
|
|
});
|
|
|
|
it('leaves without a question when nothing changed', async () => {
|
|
withSlides();
|
|
await openPage();
|
|
|
|
await userEvent.click(screen.getByTestId('nav-dashboard'));
|
|
|
|
await waitFor(() => expect(screen.queryByTestId('hero-form')).not.toBeInTheDocument(), T);
|
|
expect(screen.queryByTestId('hero-leave-dialog')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('does not count photos that are still being prepared, and keeps the notice through the background refresh', async () => {
|
|
const waiting = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'Titel', slides: [waiting] });
|
|
await openPage();
|
|
await screen.findByTestId('hero-slide-0-processing', {}, T);
|
|
expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument();
|
|
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'X');
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'Titel', slides: [slide(1)] });
|
|
|
|
await waitFor(() => expect(screen.queryByTestId('hero-slide-0-processing')).not.toBeInTheDocument(), { timeout: 12000 });
|
|
expect(screen.getByTestId('hero-unsaved')).toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-title-input')).toHaveValue('TitelX');
|
|
}, 20000);
|
|
});
|
|
|
|
describe('Several photos at once', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
const jpg = (name: string) => new File(['x'], name, { type: 'image/jpeg' });
|
|
|
|
it('adds every chosen photo at the end of the list, in the order they were chosen', async () => {
|
|
await openPage();
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), [jpg('a.jpg'), jpg('b.jpg'), jpg('c.jpg')]);
|
|
|
|
await waitFor(() => expect(screen.getAllByTestId(/^hero-slide-\d+$/)).toHaveLength(3), T);
|
|
expect(screen.getByTestId('hero-upload-summary')).toHaveTextContent(/3 van 3|3 of 3/);
|
|
expect(screen.getByTestId('hero-upload-item-0') ?? screen.getByTestId('hero-upload-list')).toBeInTheDocument();
|
|
});
|
|
|
|
it('goes on with the other photos when one is refused, and says why for that one', async () => {
|
|
await openPage();
|
|
const big = jpg('groot.jpg');
|
|
Object.defineProperty(big, 'size', { value: 11 * 1024 * 1024 });
|
|
const pdf = new File(['x'], 'brief.pdf', { type: 'application/pdf' });
|
|
|
|
// The file chooser would filter on type; here the files arrive as they do on a drop.
|
|
fireEvent.change(screen.getByTestId('hero-upload'), { target: { files: [jpg('a.jpg'), big, pdf, jpg('d.jpg')] } });
|
|
|
|
await waitFor(() => expect(screen.getAllByTestId(/^hero-slide-\d+$/)).toHaveLength(2), T);
|
|
const errors = screen.getAllByTestId('hero-upload-error');
|
|
expect(errors).toHaveLength(2);
|
|
expect(errors[0]).toHaveTextContent(/maximaal 10 MB|at most 10 MB/);
|
|
expect(errors[1]).toHaveTextContent(/geen afbeelding|not an image/);
|
|
expect(screen.getByTestId('hero-upload-summary')).toHaveTextContent(/2 van 4|2 of 4/);
|
|
});
|
|
|
|
it('never has more than two uploads running at the same time', async () => {
|
|
await openPage();
|
|
let running = 0;
|
|
let most = 0;
|
|
server.use(
|
|
http.post(`${API_BASE}/api/v1/hero/images`, async () => {
|
|
running += 1;
|
|
most = Math.max(most, running);
|
|
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
running -= 1;
|
|
return HttpResponse.json(
|
|
{ id: crypto.randomUUID(), width: 1600, height: 1067, focus: { x: 0.5, y: 0.5, zoom: 1 }, wideFocus: null, tallFocus: null, src: '', variants: [], processing: true, failed: false },
|
|
{ status: 201 },
|
|
);
|
|
}),
|
|
);
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), [jpg('1.jpg'), jpg('2.jpg'), jpg('3.jpg'), jpg('4.jpg'), jpg('5.jpg')]);
|
|
|
|
await waitFor(() => expect(screen.getAllByTestId(/^hero-slide-\d+$/)).toHaveLength(5), T);
|
|
expect(most).toBe(2);
|
|
});
|
|
|
|
it('accepts photos that are dropped on the list', async () => {
|
|
await openPage();
|
|
const section = screen.getByTestId('hero-slides-section');
|
|
|
|
fireEvent.drop(section, { dataTransfer: { types: ['Files'], files: [jpg('a.jpg'), jpg('b.jpg')] } });
|
|
|
|
await waitFor(() => expect(screen.getAllByTestId(/^hero-slide-\d+$/)).toHaveLength(2), T);
|
|
});
|
|
|
|
it('lists the formats and limits the host gives, in plain words', async () => {
|
|
await openPage();
|
|
|
|
const limits = await screen.findByTestId('hero-upload-limits', {}, T);
|
|
|
|
expect(limits).toHaveTextContent('JPEG, PNG, GIF, BMP, TIFF, WebP, AVIF');
|
|
expect(limits).toHaveTextContent(/10 MB/);
|
|
expect(limits).toHaveTextContent(/64 megapixel/);
|
|
});
|
|
});
|
|
|
|
describe('Order by dragging, wording and calm waiting', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('gives every row a handle with an understandable name, next to the arrows', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
|
|
const handle = screen.getByTestId('hero-slide-1-drag');
|
|
|
|
expect(handle).toHaveAccessibleName(/Verplaats afbeelding 2|Move image 2/);
|
|
expect(screen.getByTestId('hero-slide-1-up')).toBeInTheDocument();
|
|
});
|
|
|
|
it('moves a row with the keyboard on the handle, and the new order goes live only on save', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
// jsdom has no layout: give the rows a position so the sorting can see where they are.
|
|
const rects = new Map<Element, DOMRect>();
|
|
['hero-slide-0', 'hero-slide-1'].forEach((id, i) => {
|
|
const rect = { x: 0, y: i * 100, top: i * 100, bottom: i * 100 + 90, left: 0, right: 500, width: 500, height: 90, toJSON: () => ({}) } as DOMRect;
|
|
rects.set(screen.getByTestId(id), rect);
|
|
});
|
|
const original = Element.prototype.getBoundingClientRect;
|
|
Element.prototype.getBoundingClientRect = function (this: Element) {
|
|
return rects.get(this) ?? original.call(this);
|
|
};
|
|
try {
|
|
const handle = screen.getByTestId('hero-slide-1-drag');
|
|
handle.focus();
|
|
await userEvent.keyboard(' ');
|
|
await userEvent.keyboard('{ArrowUp}');
|
|
await userEvent.keyboard(' ');
|
|
|
|
await waitFor(() => expect(screen.getByTestId('hero-slide-0-alt')).toHaveValue('Foto 2'));
|
|
expect(getSavedHeroRequests()).toHaveLength(0);
|
|
expect(await screen.findByTestId('hero-unsaved')).toBeInTheDocument();
|
|
} finally {
|
|
Element.prototype.getBoundingClientRect = original;
|
|
}
|
|
});
|
|
|
|
it('speaks plain language: a plain Description field inside a titled slide, no alt text, and a short guide per part', async () => {
|
|
window.sessionStorage.setItem('cms-hero-tab', 'images');
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
|
|
const first = screen.getByRole('group', { name: /^Slide 1$/ });
|
|
expect(within(first).getByLabelText(/^(Beschrijving|Description)$/)).toBeInTheDocument();
|
|
expect(within(first).getByText(/Verberg voor schermlezers|Hide from screen readers/)).toBeInTheDocument();
|
|
expect(within(first).queryByText(/niets toevoegt aan de inhoud|adds nothing to the content/)).not.toBeInTheDocument();
|
|
expect(screen.getByRole('group', { name: /^Slide 2$/ })).toBeInTheDocument();
|
|
expect(screen.queryByText(/alt-tekst|alt text/i)).not.toBeInTheDocument();
|
|
const help = screen.getByTestId('hero-help');
|
|
expect(help).toHaveTextContent(/#contact/);
|
|
expect(help).toHaveTextContent(/\/privacy/);
|
|
expect(help).not.toHaveTextContent(/Planning|Schedule/);
|
|
});
|
|
|
|
it('numbers the slides by their current order, also after the order changed', async () => {
|
|
window.sessionStorage.setItem('cms-hero-tab', 'images');
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-1-up'));
|
|
|
|
expect(within(screen.getByRole('group', { name: /^Slide 1$/ })).getByTestId('hero-slide-0-alt')).toHaveValue('Foto 2');
|
|
});
|
|
|
|
it('has a switch per slide: role switch, Active or Not active, keyboard, and a dimmed row when off', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
const toggle = screen.getByTestId('hero-slide-0-active');
|
|
expect(toggle).toHaveAttribute('role', 'switch');
|
|
expect(toggle).toBeChecked();
|
|
expect(toggle).toHaveTextContent(/^(Actief|Active)/);
|
|
expect(screen.getByTestId('hero-slide-0')).not.toHaveAttribute('data-inactive');
|
|
|
|
toggle.focus();
|
|
await userEvent.keyboard('{Enter}');
|
|
|
|
expect(toggle).not.toBeChecked();
|
|
expect(toggle).toHaveTextContent(/^(Niet actief|Not active)/);
|
|
expect(screen.getByTestId('hero-slide-0')).toHaveAttribute('data-inactive', 'true');
|
|
expect(screen.getByTestId('hero-slide-1')).not.toHaveAttribute('data-inactive');
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
|
expect(getSavedHeroRequests()[0].slides.map((x) => x.isActive)).toEqual([false, true]);
|
|
});
|
|
|
|
it('marks every photo without a complete set as being processed, the one being cut with its step and the rest as queued', async () => {
|
|
const waiting = (n: number, step?: [number, number]) => ({
|
|
...slide(n),
|
|
image: { ...slide(n).image, variants: [], src: '', processing: true, renderDone: step?.[0] ?? null, renderTotal: step?.[1] ?? null },
|
|
});
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [waiting(1, [3, 16]), waiting(2), waiting(3)] });
|
|
await openPage();
|
|
|
|
for (const i of [0, 1, 2]) {
|
|
const box = await screen.findByTestId(`hero-slide-${i}-processing`, {}, T);
|
|
expect(box).toHaveClass('hero-shimmer');
|
|
expect(box).toHaveTextContent(/Wordt verwerkt|Processing/);
|
|
}
|
|
expect(screen.getByTestId('hero-slide-0-processing-step')).toHaveTextContent('3/16');
|
|
expect(screen.getByTestId('hero-slide-1-processing-queued')).toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-slide-2-processing-queued')).toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/0 van 3|0 of 3/);
|
|
});
|
|
|
|
it('keeps the waiting placeholder mounted through the background refresh, so its animation keeps running', async () => {
|
|
const waiting = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [waiting, slide(2)] });
|
|
await openPage();
|
|
const before = await screen.findByTestId('hero-slide-0-processing', {}, T);
|
|
expect(before).toHaveClass('hero-shimmer');
|
|
expect(before).toHaveAttribute('role', 'status');
|
|
expect(before).toHaveAttribute('aria-busy', 'true');
|
|
|
|
// The server still has the photo in the works when the page asks again after 5 seconds.
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', updatedAt: '2026-11-16T12:05:00Z', slides: [{ ...waiting }, slide(2)] });
|
|
await new Promise((resolve) => setTimeout(resolve, 6000));
|
|
|
|
expect(screen.getByTestId('hero-slide-0-processing')).toBe(before);
|
|
}, 20000);
|
|
});
|
|
|
|
|
|
describe('Progress of a photo that is being prepared', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('shows which photo is being worked on and at which step', async () => {
|
|
const waiting = (n: number, step?: [number, number]) => ({
|
|
...slide(n),
|
|
image: { ...slide(n).image, variants: [], src: '', processing: true, renderDone: step?.[0] ?? null, renderTotal: step?.[1] ?? null },
|
|
});
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), waiting(2, [7, 16]), waiting(3)] });
|
|
|
|
await openPage();
|
|
|
|
expect(await screen.findByTestId('hero-processing-current', {}, T)).toHaveTextContent(/foto 2 van 3.*stap 7 van 16|photo 2 of 3.*step 7 of 16/);
|
|
expect(screen.getByTestId('hero-slide-1-processing-step')).toHaveTextContent('7/16');
|
|
expect(screen.queryByTestId('hero-slide-2-processing-step')).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
|
|
describe('A changed focus point makes a new crop in the background', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('shows that the new crop is not live yet (old picture underneath), counts it in the total, and clears it when done', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
|
fireEvent.change(await screen.findByTestId('focus-x', {}, T), { target: { value: '25' } });
|
|
await userEvent.click(screen.getByTestId('focus-save'));
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
const recut = await screen.findByTestId('hero-slide-0-recut', {}, T);
|
|
expect(recut).toHaveClass('hero-shimmer');
|
|
expect(recut.querySelector('img')).not.toBeNull();
|
|
expect(recut).toHaveTextContent(/Nieuwe uitsnede|new crop/);
|
|
expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/1 van 2|1 of 2/);
|
|
expect(screen.queryByTestId('hero-slide-1-recut')).not.toBeInTheDocument();
|
|
|
|
// The server finishes the new crop; the page asks again by itself.
|
|
finishMockRecut();
|
|
await waitFor(() => expect(screen.queryByTestId('hero-slide-0-recut')).not.toBeInTheDocument(), { timeout: 12000 });
|
|
expect(screen.queryByTestId('hero-processing')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled();
|
|
}, 25000);
|
|
|
|
it('shows a failed new crop with the retry', async () => {
|
|
const failed = { ...slide(1), image: { ...slide(1).image, processing: true, failed: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [failed] });
|
|
|
|
await openPage();
|
|
|
|
expect(await screen.findByTestId('hero-slide-0-recut', {}, T)).toHaveTextContent(/Mislukt|Failed/);
|
|
await userEvent.click(screen.getByTestId('hero-processing-retry'));
|
|
await waitFor(() => expect(getRetriedImages()).toEqual([failed.imageId]), T);
|
|
});
|
|
|
|
it('offers to try again for a photo that takes long', async () => {
|
|
const waiting = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [waiting] });
|
|
await openPage();
|
|
|
|
await userEvent.click(await screen.findByTestId('hero-processing-retry-all', {}, T));
|
|
|
|
await waitFor(() => expect(getRetriedImages()).toEqual([waiting.imageId]), T);
|
|
});
|
|
});
|
|
|
|
|
|
describe('Focus dialog shows the whole picture', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
const portraitSlide = () => {
|
|
const base = slide(1);
|
|
// A portrait photo of 2000x3000: the published crops are wide/tall, but the dialog needs the whole picture.
|
|
return { ...base, image: { ...base.image, width: 2000, height: 3000, src: '/media/hero/0123456789abcdef/wide-1600.webp', previewSrc: '/media/hero/0123456789abcdef/full-1280.webp' } };
|
|
};
|
|
|
|
it('uses the full-frame preview at the shape of the original, not the wide crop', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [portraitSlide()] });
|
|
await openPage();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
|
|
|
const area = await screen.findByTestId('focus-area', {}, T);
|
|
expect(area.style.aspectRatio.replace(/\s/g, '')).toBe('2000/3000');
|
|
const picture = area.querySelector('img')!;
|
|
expect(picture.getAttribute('src')).toContain('full-1280.webp');
|
|
expect(picture.getAttribute('src')).not.toContain('wide-1600');
|
|
expect(picture).toHaveClass('object-cover');
|
|
for (const ratio of ['wide', 'tall']) {
|
|
expect(screen.getByTestId(`focus-preview-${ratio}`).querySelector('img')!.getAttribute('src')).toContain('full-1280.webp');
|
|
}
|
|
});
|
|
|
|
it('says the preview is being prepared when the server has none yet, instead of showing a wrong picture', async () => {
|
|
const base = portraitSlide();
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...base, image: { ...base.image, previewSrc: null } }] });
|
|
await openPage();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
|
|
|
expect(await screen.findByTestId('focus-preview-pending', {}, T)).toBeInTheDocument();
|
|
expect(screen.getByTestId('focus-area').querySelector('img')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('Destructive actions are red', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('shows the remove button of every slide in the destructive style, with a name', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
|
|
for (const i of [0, 1]) {
|
|
const remove = screen.getByTestId(`hero-slide-${i}-remove`);
|
|
expect(remove).toHaveClass('bg-destructive');
|
|
expect(remove).toHaveAccessibleName(/Slide verwijderen|Remove slide/);
|
|
}
|
|
});
|
|
|
|
it('shows Discard changes in the destructive style', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'X');
|
|
await screen.findByTestId('hero-unsaved');
|
|
|
|
await userEvent.click(screen.getByTestId('nav-dashboard'));
|
|
|
|
expect(await screen.findByTestId('hero-leave-discard', {}, T)).toHaveClass('bg-destructive');
|
|
expect(screen.getByTestId('hero-leave-stay')).not.toHaveClass('bg-destructive');
|
|
});
|
|
});
|
|
|
|
|
|
describe('Each photo shows by itself when its processing is finished', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
const jpg = (name: string) => new File(['x'], name, { type: 'image/jpeg' });
|
|
const cutting = (id: string) => ({
|
|
id, width: 5757, height: 3838, focus: { x: 0.5, y: 0.5, zoom: 1 }, wideFocus: null, tallFocus: null,
|
|
src: '', variants: [], processing: true, failed: false, previewSrc: null,
|
|
});
|
|
const ready = (id: string) => ({
|
|
...cutting(id), src: '/media/hero/0123456789abcdef/wide-1600.webp', processing: false,
|
|
variants: [{ ratio: 'wide', width: 960, height: 640, type: 'image/webp', url: '/media/hero/0123456789abcdef/wide-960.webp' }],
|
|
previewSrc: '/media/hero/0123456789abcdef/full-1280.webp',
|
|
});
|
|
|
|
it('turns the placeholder of a photo that was not saved yet into a finished note on that photo, by itself, and the photo can be worked on at once', async () => {
|
|
const id = '70000000-0000-0000-0000-000000000001';
|
|
let finished = false;
|
|
server.use(
|
|
http.post(`${API_BASE}/api/v1/hero/images`, () => HttpResponse.json(cutting(id), { status: 201 })),
|
|
http.get(`${API_BASE}/api/v1/hero/images/${id}`, () => HttpResponse.json(finished ? ready(id) : cutting(id))),
|
|
);
|
|
await openPage();
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), jpg('groot.jpg'));
|
|
expect(await screen.findByTestId('hero-slide-0-processing', {}, T)).toHaveClass('hero-shimmer');
|
|
expect(screen.queryByTestId('hero-slide-0-focus')).not.toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-slide-0-done-text')).not.toBeInTheDocument();
|
|
|
|
// The server is done; nobody saves or reloads: the page asks again by itself.
|
|
finished = true;
|
|
const note = await screen.findByTestId('hero-slide-0-done-text', {}, { timeout: 12000 });
|
|
expect(note).toHaveTextContent(/Verwerking afgerond|Processing finished/);
|
|
expect(screen.getByTestId('hero-slide-0-done')).toHaveAttribute('role', 'status');
|
|
expect(screen.getByTestId('hero-slide-0-done')).toHaveAttribute('aria-live', 'polite');
|
|
expect(screen.queryByTestId('hero-slide-0-processing')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled();
|
|
// The note names the slide for a screen reader, and goes away by itself after a few seconds.
|
|
expect(note.textContent).toMatch(/Slide 1: (verwerking afgerond|processing finished)/);
|
|
await waitFor(() => expect(screen.queryByTestId('hero-slide-0-done-text')).not.toBeInTheDocument(), { timeout: 8000 });
|
|
}, 30000);
|
|
|
|
it('shows the same finished note, at once and without a placeholder, for a photo that was already in the CMS', async () => {
|
|
const id = '70000000-0000-0000-0000-000000000002';
|
|
server.use(http.post(`${API_BASE}/api/v1/hero/images`, () => HttpResponse.json({ ...ready(id), reused: true }, { status: 201 })));
|
|
await openPage();
|
|
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), jpg('zelfde.jpg'));
|
|
|
|
expect(await screen.findByTestId('hero-slide-0-done-text', {}, T)).toHaveTextContent(/Verwerking afgerond|Processing finished/);
|
|
expect(screen.queryByTestId('hero-slide-0-processing')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled();
|
|
expect(screen.queryByText(/stond al|already/i)).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('finishes photo by photo: the first one can be worked on while the second is still busy, and the total at the top follows', async () => {
|
|
const ids = ['70000000-0000-0000-0000-000000000011', '70000000-0000-0000-0000-000000000012'];
|
|
let n = 0;
|
|
const finished = new Set<string>();
|
|
server.use(
|
|
http.post(`${API_BASE}/api/v1/hero/images`, () => HttpResponse.json(cutting(ids[n++]), { status: 201 })),
|
|
http.get(`${API_BASE}/api/v1/hero/images/${ids[0]}`, () => HttpResponse.json(finished.has(ids[0]) ? ready(ids[0]) : cutting(ids[0]))),
|
|
http.get(`${API_BASE}/api/v1/hero/images/${ids[1]}`, () => HttpResponse.json(finished.has(ids[1]) ? ready(ids[1]) : cutting(ids[1]))),
|
|
);
|
|
await openPage();
|
|
await userEvent.upload(screen.getByTestId('hero-upload'), [jpg('a.jpg'), jpg('b.jpg')]);
|
|
await screen.findByTestId('hero-slide-1-processing', {}, T);
|
|
expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/0 van 2|0 of 2/);
|
|
|
|
finished.add(ids[0]);
|
|
await screen.findByTestId('hero-slide-0-done-text', {}, { timeout: 12000 });
|
|
|
|
expect(screen.getByTestId('hero-slide-0-focus')).toBeEnabled();
|
|
expect(screen.getByTestId('hero-slide-1-processing')).toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-slide-1-focus')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-processing-count')).toHaveTextContent(/1 van 2|1 of 2/);
|
|
}, 30000);
|
|
});
|
|
|
|
|
|
describe('The picture of a slide is the way to adjust its crop', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('is a real button named Adjust crop, with a hint that is there without hovering, and it opens the dialog', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
await openPage();
|
|
|
|
const thumb = screen.getByTestId('hero-slide-0-focus');
|
|
|
|
expect(thumb.tagName).toBe('BUTTON');
|
|
expect(thumb).toHaveAccessibleName(/Uitsnede aanpassen|Adjust crop/);
|
|
expect(thumb.querySelector('svg')).not.toBeNull();
|
|
expect(within(thumb).getAllByText(/Uitsnede aanpassen|Adjust crop/).length).toBeGreaterThan(0);
|
|
expect(screen.queryByRole('button', { name: /Focus instellen|Set focus/ })).not.toBeInTheDocument();
|
|
thumb.focus();
|
|
await userEvent.keyboard('{Enter}');
|
|
expect(await screen.findByTestId('focus-dialog', {}, T)).toBeInTheDocument();
|
|
});
|
|
|
|
it('shows the new crop on the thumbnail right after Apply, with the not-saved note, and drops it when the change is undone', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
await openPage();
|
|
expect(screen.queryByTestId('hero-slide-0-focus-local')).not.toBeInTheDocument();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
|
fireEvent.change(await screen.findByTestId('focus-x', {}, T), { target: { value: '10' } });
|
|
await userEvent.click(screen.getByTestId('focus-save'));
|
|
|
|
await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument());
|
|
const local = await screen.findByTestId('hero-slide-0-focus-local', {}, T);
|
|
expect(local.getAttribute('src')).toContain('full-1280.webp');
|
|
expect(local.style.left).toMatch(/^-?\d/);
|
|
expect(screen.getByTestId('hero-slide-0-focus-pending')).toHaveTextContent(/Focus gewijzigd, nog niet opgeslagen|Focus changed, not saved yet/);
|
|
});
|
|
|
|
it('follows the whole chain: Apply, Save sends the focus, the new crop is made (animation), then it is finished', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1), slide(2)] });
|
|
await openPage();
|
|
await userEvent.click(screen.getByTestId('hero-slide-1-focus'));
|
|
fireEvent.change(await screen.findByTestId('focus-y', {}, T), { target: { value: '20' } });
|
|
await userEvent.click(screen.getByTestId('focus-save'));
|
|
await screen.findByTestId('hero-slide-1-focus-pending', {}, T);
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
|
expect(getSavedHeroRequests()[0].imageFocus).toEqual([
|
|
expect.objectContaining({ imageId: '10000000-0000-0000-0000-000000000002', focus: { x: 0.5, y: 0.2, zoom: 1 } }),
|
|
]);
|
|
// Saved: the note of the unsaved change is gone and the new crop is being made, with the animation, on that photo only.
|
|
const recut = await screen.findByTestId('hero-slide-1-recut', {}, T);
|
|
expect(recut).toHaveClass('hero-shimmer');
|
|
expect(recut).toHaveTextContent(/Nieuwe uitsnede wordt gemaakt|Making the new crop/);
|
|
expect(screen.queryByTestId('hero-slide-1-focus-pending')).not.toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-slide-0-recut')).not.toBeInTheDocument();
|
|
|
|
finishMockRecut();
|
|
expect(await screen.findByTestId('hero-slide-1-done-text', {}, { timeout: 12000 })).toHaveTextContent(/Verwerking afgerond|Processing finished/);
|
|
expect(screen.queryByTestId('hero-slide-1-recut')).not.toBeInTheDocument();
|
|
expect(screen.getByTestId('hero-slide-1-focus').tagName).toBe('BUTTON');
|
|
}, 30000);
|
|
});
|
|
|
|
|
|
describe('The Save button', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('is greyed out while there is nothing to save, active after a change, busy while saving, and greyed out again after saving', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'Titel', slides: [slide(1)] });
|
|
let release: () => void = () => undefined;
|
|
const gate = new Promise<void>((resolve) => { release = resolve; });
|
|
server.use(
|
|
http.put(`${API_BASE}/api/v1/hero`, async ({ request }) => {
|
|
await gate;
|
|
const body = (await request.json()) as { title: string };
|
|
return HttpResponse.json({ ...emptyHero(), id: 'h1', title: body.title, slides: [slide(1)] });
|
|
}),
|
|
);
|
|
await openPage();
|
|
const save = screen.getByTestId('hero-save');
|
|
expect(save).toBeDisabled();
|
|
expect(save).toHaveAttribute('aria-disabled', 'true');
|
|
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), '!');
|
|
await waitFor(() => expect(save).toBeEnabled());
|
|
expect(save).toHaveAttribute('aria-disabled', 'false');
|
|
|
|
await userEvent.click(save);
|
|
await waitFor(() => expect(save).toHaveAttribute('aria-busy', 'true'));
|
|
expect(save).toBeDisabled();
|
|
expect(save).toHaveTextContent(/Bezig met opslaan|Saving/);
|
|
|
|
release();
|
|
await waitFor(() => expect(save).toHaveAttribute('aria-busy', 'false'));
|
|
await waitFor(() => expect(save).toBeDisabled());
|
|
expect(screen.queryByTestId('hero-unsaved')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('is greyed out again when a change is undone, and active for a focus change that is applied', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
await openPage();
|
|
const save = screen.getByTestId('hero-save');
|
|
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'X');
|
|
await waitFor(() => expect(save).toBeEnabled());
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), '{backspace}');
|
|
await waitFor(() => expect(save).toBeDisabled());
|
|
|
|
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
|
|
fireEvent.change(await screen.findByTestId('focus-x', {}, T), { target: { value: '15' } });
|
|
await userEvent.click(screen.getByTestId('focus-save'));
|
|
|
|
await waitFor(() => expect(save).toBeEnabled());
|
|
});
|
|
|
|
it('stays active when saving failed, so it can be tried again', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
server.use(http.put(`${API_BASE}/api/v1/hero`, () => HttpResponse.json({ error: 'x', message: 'Het lukt niet.' }, { status: 500 })));
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'X');
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(screen.getByTestId('hero-save')).toHaveAttribute('aria-busy', 'false'));
|
|
expect(screen.getByTestId('hero-save')).toBeEnabled();
|
|
});
|
|
});
|
|
|
|
describe('Tabs Content and Images', () => {
|
|
beforeEach(() => {
|
|
resetMockHero();
|
|
});
|
|
|
|
it('has accessible tabs: tablist, tab, tabpanel, the arrow keys, Home and End', async () => {
|
|
await openPage();
|
|
|
|
const tablist = screen.getByRole('tablist');
|
|
const [content, images] = within(tablist).getAllByRole('tab');
|
|
expect(content).toHaveAttribute('aria-selected', 'true');
|
|
expect(content).toHaveAttribute('tabindex', '0');
|
|
expect(images).toHaveAttribute('aria-selected', 'false');
|
|
expect(images).toHaveAttribute('tabindex', '-1');
|
|
expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-labelledby', content.id);
|
|
expect(screen.getByTestId('hero-title-input')).toBeVisible();
|
|
|
|
content.focus();
|
|
await userEvent.keyboard('{ArrowRight}');
|
|
expect(images).toHaveAttribute('aria-selected', 'true');
|
|
expect(images).toHaveFocus();
|
|
expect(screen.getByRole('tabpanel')).toHaveAttribute('aria-labelledby', images.id);
|
|
expect(screen.getByTestId('hero-slides-empty')).toBeVisible();
|
|
expect(screen.getByTestId('hero-title-input')).not.toBeVisible();
|
|
|
|
await userEvent.keyboard('{ArrowRight}');
|
|
expect(content).toHaveAttribute('aria-selected', 'true');
|
|
await userEvent.keyboard('{End}');
|
|
expect(images).toHaveAttribute('aria-selected', 'true');
|
|
await userEvent.keyboard('{Home}');
|
|
expect(content).toHaveAttribute('aria-selected', 'true');
|
|
});
|
|
|
|
it('keeps a change when switching tabs, shows a dot on the tab with changes, and saves both tabs with one button', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-title-input'), 'x');
|
|
expect(screen.getByTestId('hero-tab-content-changed')).toBeInTheDocument();
|
|
expect(screen.queryByTestId('hero-tab-images-changed')).not.toBeInTheDocument();
|
|
|
|
await userEvent.click(screen.getByTestId('hero-tab-images'));
|
|
expect(screen.getByTestId('hero-unsaved')).toBeInTheDocument();
|
|
await userEvent.type(screen.getByTestId('hero-slide-0-alt'), '!');
|
|
expect(screen.getByTestId('hero-tab-images-changed')).toBeInTheDocument();
|
|
await userEvent.click(screen.getByTestId('hero-tab-content'));
|
|
expect(screen.getByTestId('hero-title-input')).toHaveValue('Tx');
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(getSavedHeroRequests()).toHaveLength(1));
|
|
expect(getSavedHeroRequests()[0].title).toBe('Tx');
|
|
expect(getSavedHeroRequests()[0].slides[0].alt).toBe('Foto 1!');
|
|
});
|
|
|
|
it('remembers the chosen tab for the session', async () => {
|
|
const first = await openPage();
|
|
expect(first).toBeInTheDocument();
|
|
await userEvent.click(screen.getByTestId('hero-tab-images'));
|
|
|
|
expect(window.sessionStorage.getItem('cms-hero-tab')).toBe('images');
|
|
});
|
|
|
|
it('opens on the remembered tab', async () => {
|
|
window.sessionStorage.setItem('cms-hero-tab', 'images');
|
|
|
|
await openPage();
|
|
|
|
expect(screen.getByTestId('hero-tab-images')).toHaveAttribute('aria-selected', 'true');
|
|
});
|
|
|
|
it('shows how many photos are busy on the Images tab', async () => {
|
|
const waiting = { ...slide(1), image: { ...slide(1).image, variants: [], src: '', processing: true } };
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [waiting, slide(2)] });
|
|
|
|
await openPage();
|
|
|
|
expect(await screen.findByTestId('hero-tab-images', {}, T)).toHaveTextContent(/1 bezig|1 busy/);
|
|
});
|
|
|
|
it('jumps to the tab of the first error, marks it, and puts the cursor in that field', async () => {
|
|
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1, { alt: '' })] });
|
|
await openPage();
|
|
await userEvent.type(screen.getByTestId('hero-lead'), 'x');
|
|
|
|
await userEvent.click(screen.getByTestId('hero-save'));
|
|
|
|
await waitFor(() => expect(screen.getByTestId('hero-tab-images')).toHaveAttribute('aria-selected', 'true'));
|
|
expect(screen.getByTestId('hero-tab-images-invalid')).toBeInTheDocument();
|
|
await waitFor(() => expect(screen.getByTestId('hero-slide-0-alt')).toHaveFocus());
|
|
});
|
|
|
|
it('has a full-width, opaque save bar', async () => {
|
|
await openPage();
|
|
|
|
const bar = screen.getByTestId('hero-save-bar');
|
|
expect(bar).toHaveClass('-mx-6', 'bg-background', 'border-t');
|
|
expect(bar.className).not.toMatch(/bg-background\//);
|
|
});
|
|
});
|