Hero-admin: meerdere foto's tegelijk, slepen, niet-opgeslagen waarschuwing, begrijpelijke teksten, rustige wachtanimatie
- Meerdere bestanden kiezen of slepen (2 tegelijk, per bestand voortgang en reden bij falen, volgorde van kiezen), uploadlimiet standaard 100 per uur - Volgorde ook met slepen (greepje, toetsenbord), pijltjes blijven - Waarschuwing bij niet-opgeslagen wijzigingen (badge, beforeunload, bevestiging bij navigeren, vergelijking met laatst opgeslagen stand) - Teksten zonder vakjargon, uitleg per onderdeel, formaten en limieten van de host (GET /hero/images/limits) - Shimmer (CSS, reduced-motion) op foto's die nog verwerkt worden Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { fireEvent, screen, waitFor } from '@testing-library/react';
|
||||
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';
|
||||
@@ -142,9 +142,9 @@ describe('Hero page', () => {
|
||||
});
|
||||
|
||||
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/],
|
||||
['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');
|
||||
@@ -407,3 +407,261 @@ describe('Upload problems', () => {
|
||||
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: no alt text, decoration with an explanation, and a short guide per part', async () => {
|
||||
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
|
||||
await openPage();
|
||||
|
||||
expect(screen.getByLabelText(/Beschrijving afbeelding 1$|Description of image 1$/)).toBeInTheDocument();
|
||||
const row = within(screen.getByTestId('hero-slide-0'));
|
||||
expect(row.getByText(/Alleen versiering|Decoration only/)).toBeInTheDocument();
|
||||
expect(row.getByText(/niets toevoegt aan de inhoud|adds nothing to the content/)).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).toHaveTextContent(/Planning|Schedule/);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user