Files
slp-modular-cms/frontend/src/features/media/pages/MediaLibraryPage.test.tsx
T
SluijsensandClaude Sonnet 5.5 e4f2569d20
Continuous Integration / config-sls (pull_request) Successful in 10s
Continuous Integration / config-asf (pull_request) Successful in 20s
Continuous Integration / changes (pull_request) Successful in 21s
Continuous Integration / compile-sites (pull_request) Successful in 1m46s
Continuous Integration / backend-build (pull_request) Successful in 1m51s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m52s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m37s
Continuous Integration / backend-test (pull_request) Successful in 3m22s
Continuous Integration / frontend-build (pull_request) Successful in 38s
Continuous Integration / frontend-lint (pull_request) Successful in 44s
Continuous Integration / frontend-test (pull_request) Successful in 2m48s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Mediabibliotheek: gelijke ruimte rond het selectievakje op de tegel
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 20:34:54 +01:00

333 lines
18 KiB
TypeScript

import { beforeEach, describe, expect, it } from 'vitest';
import { http, HttpResponse } from 'msw';
import { screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import i18n from '@/i18n/config';
import { server } from '@/mocks/server';
import { renderWithProviders } from '@/test/utils';
import { createMemoryHistory, createRootRoute, createRoute, createRouter, RouterProvider } from '@tanstack/react-router';
import { seedPhotos, seedStorage } from '../mocks/handlers';
import { MediaLibraryPage } from './MediaLibraryPage';
/** The page inside a router with the same base path as the real admin app (/admin), and a stub hero route to land on. */
function renderPage() {
const root = createRootRoute();
const media = createRoute({ getParentRoute: () => root, path: '/media', component: MediaLibraryPage });
const hero = createRoute({ getParentRoute: () => root, path: '/hero', component: () => <p data-testid="hero-stub">hero</p> });
const router = createRouter({
routeTree: root.addChildren([media, hero]),
basepath: '/admin',
history: createMemoryHistory({ initialEntries: ['/admin/media'] }),
});
return renderWithProviders(<RouterProvider router={router} />);
}
beforeEach(async () => {
window.localStorage.removeItem('slp.media.layout');
await i18n.changeLanguage('nl');
});
describe('MediaLibraryPage', () => {
it('lists the photos newest first with where each is used, and the storage', async () => {
renderPage();
const list = await screen.findByTestId('photo-selection-list');
const rows = within(list).getAllByRole('listitem');
expect(rows).toHaveLength(3);
expect(within(rows[0]!).getByText('Hero, slide 1')).toBeInTheDocument();
expect(within(rows[1]!).getByText('Niet gebruikt')).toBeInTheDocument();
expect(within(rows[2]!).getByText('Hero, slide 2 (uit)')).toBeInTheDocument();
expect(await screen.findByTestId('storage-text')).toHaveTextContent('van');
expect(screen.queryByTestId('storage-warning')).not.toBeInTheDocument();
});
it('warns when the storage is nearly full and says so when it is full', async () => {
server.use(http.get('*/media-library/storage', () => HttpResponse.json({ ...seedStorage(), usedBytes: 1_800_000_000, level: 'warning' })));
const { unmount } = renderPage();
expect(await screen.findByTestId('storage-warning')).toHaveTextContent('80%');
unmount();
server.use(http.get('*/media-library/storage', () => HttpResponse.json({ ...seedStorage(), usedBytes: 2_147_483_648, level: 'full' })));
renderPage();
expect(await screen.findByTestId('storage-warning')).toHaveTextContent('vol');
});
it('selects photos and shows the bulk bar with the delete action', async () => {
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByRole('checkbox', { name: 'zomer.jpg' }));
expect(await screen.findByTestId('bulk-count')).toHaveTextContent('1 foto geselecteerd');
expect(screen.getByTestId('bulk-action-delete')).toHaveTextContent('Naar prullenbak');
});
it('opens the clean-up: older than 7 days, only unused, all selected, newest first, and nothing is deleted', async () => {
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('cleanup-open'));
const dialog = await screen.findByTestId('cleanup-dialog');
expect(within(dialog).getByTestId('cleanup-only-unused')).toBeChecked();
expect(await within(dialog).findByTestId('cleanup-summary')).toHaveTextContent('1 foto');
expect(within(dialog).getByTestId('cleanup-preview-note')).toHaveTextContent('prullenbak');
const boxes = within(dialog).getAllByRole('checkbox', { name: /Foto/ });
expect(boxes).toHaveLength(1);
await waitFor(() => expect(boxes[0]).toBeChecked());
});
it('includes photos in use when "only unused" is switched off', async () => {
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('cleanup-open'));
const dialog = await screen.findByTestId('cleanup-dialog');
await within(dialog).findByTestId('cleanup-summary');
await userEvent.click(within(dialog).getByTestId('cleanup-only-unused'));
await waitFor(() => expect(within(dialog).getByTestId('cleanup-summary')).toHaveTextContent('2 foto'));
expect(within(dialog).getAllByRole('checkbox', { name: /Foto/ })).toHaveLength(2);
});
it('moves an unused photo to the trash after one confirmation', async () => {
let body: { ids: string[]; confirmedInUse: boolean } | null = null;
server.use(
http.post('*/media-library/delete', async ({ request }) => {
body = (await request.json()) as { ids: string[]; confirmedInUse: boolean };
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'trashed', uses: [] })) });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('photo-select-hero:22222222-2222-2222-2222-222222222222'));
await userEvent.click(screen.getByTestId('bulk-action-delete'));
expect(await screen.findByTestId('delete-text')).toHaveTextContent('prullenbak');
await userEvent.click(screen.getByTestId('delete-confirm'));
await waitFor(() => expect(screen.queryByTestId('delete-dialog')).not.toBeInTheDocument());
expect(body).toEqual({ ids: ['hero:22222222-2222-2222-2222-222222222222'], confirmedInUse: false });
});
it('asks a second confirmation that names the places for a photo in use', async () => {
let body: { ids: string[]; confirmedInUse: boolean } | null = null;
server.use(
http.post('*/media-library/delete', async ({ request }) => {
body = (await request.json()) as { ids: string[]; confirmedInUse: boolean };
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'trashed', uses: [] })) });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('photo-select-hero:11111111-1111-1111-1111-111111111111'));
await userEvent.click(screen.getByTestId('bulk-action-delete'));
expect(await screen.findByTestId('delete-inuse-count')).toHaveTextContent('1 foto wordt nog gebruikt');
await userEvent.click(screen.getByTestId('delete-confirm'));
expect(await screen.findByTestId('delete-places')).toHaveTextContent('Hero, slide 1');
expect(body).toBeNull();
await userEvent.click(screen.getByTestId('delete-confirm'));
await waitFor(() => expect(body).toEqual({ ids: ['hero:11111111-1111-1111-1111-111111111111'], confirmedInUse: true }));
});
it('reports photos that could not be deleted', async () => {
server.use(
http.post('*/media-library/delete', async ({ request }) => {
const { ids } = (await request.json()) as { ids: string[] };
return HttpResponse.json({ results: ids.map((id, i) => ({ id, status: i === 0 ? 'failed' : 'trashed', uses: [] })) });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('photo-select-hero:22222222-2222-2222-2222-222222222222'));
await userEvent.click(screen.getByTestId('photo-select-hero:33333333-3333-3333-3333-333333333333'));
await userEvent.click(screen.getByTestId('bulk-action-delete'));
await userEvent.click(screen.getByTestId('delete-confirm'));
await userEvent.click(await screen.findByTestId('delete-confirm'));
expect(await screen.findByTestId('delete-partial')).toHaveTextContent('1 foto kon niet worden verwijderd');
});
it('shows the trash and restores a photo', async () => {
let restored: string[] = [];
server.use(
http.get('*/media-library/trash', () => HttpResponse.json({ photos: [{ ...seedPhotos()[1]!, deletedAt: new Date().toISOString() }], modules: ['hero'] })),
http.post('*/media-library/restore', async ({ request }) => {
restored = ((await request.json()) as { ids: string[] }).ids;
return HttpResponse.json({ results: restored.map((id) => ({ id, status: 'restored', uses: [] })) });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('view-trash'));
expect(await screen.findByTestId('trash-note')).toBeInTheDocument();
await userEvent.click(await screen.findByTestId('photo-select-hero:22222222-2222-2222-2222-222222222222'));
await userEvent.click(screen.getByTestId('bulk-action-restore'));
await waitFor(() => expect(restored).toEqual(['hero:22222222-2222-2222-2222-222222222222']));
});
it('confirming the clean-up moves exactly the selected photos to the trash', async () => {
let body: { ids: string[]; confirmedInUse: boolean } | null = null;
server.use(
http.post('*/media-library/delete', async ({ request }) => {
body = (await request.json()) as { ids: string[]; confirmedInUse: boolean };
return HttpResponse.json({ results: body.ids.map((id) => ({ id, status: 'trashed', uses: [] })) });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('cleanup-open'));
const dialog = await screen.findByTestId('cleanup-dialog');
await within(dialog).findByTestId('cleanup-summary');
await userEvent.click(within(dialog).getByTestId('cleanup-confirm'));
await userEvent.click(await screen.findByTestId('delete-confirm'));
await waitFor(() => expect(body).toEqual({ ids: ['hero:22222222-2222-2222-2222-222222222222'], confirmedInUse: false }));
});
it('filters the library on used and unused photos', async () => {
renderPage();
const list = await screen.findByTestId('photo-selection-list');
expect(within(list).getAllByRole('listitem')).toHaveLength(3);
await userEvent.click(screen.getByTestId('filter-unused'));
expect(within(screen.getByTestId('photo-selection-list')).getAllByRole('listitem')).toHaveLength(1);
await userEvent.click(screen.getByTestId('filter-used'));
expect(within(screen.getByTestId('photo-selection-list')).getAllByRole('listitem')).toHaveLength(2);
});
it('links a use to the hero under the admin base path (/admin/hero, not /hero) and opens its images tab', async () => {
window.sessionStorage.removeItem('cms-hero-tab');
renderPage();
const list = await screen.findByTestId('photo-selection-list');
const link = within(within(list).getAllByRole('listitem')[0]!).getByRole('link', { name: 'Hero, slide 1' });
expect(link).toHaveAttribute('href', '/admin/hero');
await userEvent.click(link);
expect(await screen.findByTestId('hero-stub')).toBeInTheDocument();
expect(window.sessionStorage.getItem('cms-hero-tab')).toBe('images');
});
const trashed = (n: number) => ({ ...seedPhotos()[1]!, id: `hero:2222222${n}-2222-2222-2222-222222222222`, bytes: 1_000_000, deletedAt: new Date().toISOString() });
it('removes selected photos from the trash for good after a clear, irreversible confirmation that names the number', async () => {
let body: { ids?: string[]; all?: boolean } | null = null;
server.use(
http.get('*/media-library/trash', () => HttpResponse.json({ photos: [trashed(1), trashed(2)], modules: ['hero'] })),
http.post('*/media-library/purge', async ({ request }) => {
body = (await request.json()) as { ids?: string[]; all?: boolean };
return HttpResponse.json({ results: (body.ids ?? []).map((id) => ({ id, status: 'purged', uses: [] })) });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('view-trash'));
await userEvent.click(await screen.findByTestId('photo-select-hero:22222221-2222-2222-2222-222222222222'));
await userEvent.click(await screen.findByTestId('photo-select-hero:22222222-2222-2222-2222-222222222222'));
expect(screen.getByTestId('bulk-action-restore')).toBeInTheDocument();
await userEvent.click(screen.getByTestId('bulk-action-purge'));
expect(await screen.findByTestId('purge-text')).toHaveTextContent('2 foto');
expect(screen.getByTestId('purge-warning')).toHaveTextContent('niet ongedaan');
await userEvent.click(screen.getByTestId('purge-confirm'));
await waitFor(() => expect(body).toEqual({ ids: ['hero:22222221-2222-2222-2222-222222222222', 'hero:22222222-2222-2222-2222-222222222222'] }));
});
it('empties the whole trash with one button: the confirmation names the total number and size, and the server is asked for all', async () => {
let body: { ids?: string[]; all?: boolean } | null = null;
server.use(
http.get('*/media-library/trash', () => HttpResponse.json({ photos: [trashed(1), trashed(2), trashed(3)], modules: ['hero'] })),
http.post('*/media-library/purge', async ({ request }) => {
body = (await request.json()) as { ids?: string[]; all?: boolean };
return HttpResponse.json({ results: [{ id: 'a', status: 'purged', uses: [] }] });
}),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('view-trash'));
await waitFor(() => expect(screen.getByTestId('trash-empty')).toBeEnabled());
await userEvent.click(screen.getByTestId('trash-empty'));
expect(await screen.findByTestId('purge-text')).toHaveTextContent(/3 foto's.*2,9 MB|3 foto's \(2,9 MB\)/);
expect(screen.getByTestId('purge-warning')).toHaveTextContent('niet ongedaan');
await userEvent.click(screen.getByTestId('purge-confirm'));
await waitFor(() => expect(body).toEqual({ all: true }));
});
it('disables Empty the trash when the trash is empty', async () => {
server.use(http.get('*/media-library/trash', () => HttpResponse.json({ photos: [], modules: ['hero'] })));
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('view-trash'));
await waitFor(() => expect(screen.getByTestId('trash-empty')).toBeDisabled());
});
it('reports photos that could not be removed for good', async () => {
server.use(
http.get('*/media-library/trash', () => HttpResponse.json({ photos: [trashed(1)], modules: ['hero'] })),
http.post('*/media-library/purge', () => HttpResponse.json({ results: [{ id: 'x', status: 'inUse', uses: [] }] })),
);
renderPage();
await screen.findByTestId('photo-selection-list');
await userEvent.click(screen.getByTestId('view-trash'));
await userEvent.click(await screen.findByTestId('photo-select-hero:22222221-2222-2222-2222-222222222222'));
await userEvent.click(screen.getByTestId('bulk-action-purge'));
await userEvent.click(await screen.findByTestId('purge-confirm'));
expect(await screen.findByTestId('purge-partial')).toHaveTextContent('1 foto kon niet worden verwijderd');
});
it('shows tiles by default, switches to a list, remembers the choice and keeps every function in both', async () => {
const first = renderPage();
await screen.findByTestId('photo-selection-list');
expect(screen.getByTestId('photo-items')).toHaveAttribute('data-layout', 'tiles');
expect(screen.getByTestId('layout-tiles')).toHaveAttribute('aria-pressed', 'true');
// The checkbox chip is a flex box with equal padding: no inline line box that would add space under the box.
const chip = screen.getAllByTestId(/^photo-chip-/)[0]!;
expect(chip).toHaveClass('flex', 'p-1');
// Selecting and the bulk bar work on tiles.
await userEvent.click(screen.getByRole('checkbox', { name: 'zomer.jpg' }));
expect(await screen.findByTestId('bulk-count')).toHaveTextContent('1 foto geselecteerd');
// The filter works on tiles.
await userEvent.click(screen.getByTestId('filter-unused'));
expect(within(screen.getByTestId('photo-items')).getAllByRole('listitem')).toHaveLength(1);
await userEvent.click(screen.getByTestId('filter-all'));
await userEvent.click(screen.getByTestId('layout-list'));
expect(screen.getByTestId('photo-items')).toHaveAttribute('data-layout', 'list');
expect(screen.getByTestId('layout-list')).toHaveAttribute('aria-pressed', 'true');
expect(window.localStorage.getItem('slp.media.layout')).toBe('list');
first.unmount();
// A later visit starts with the remembered layout, also in the trash view.
renderPage();
await screen.findByTestId('photo-selection-list');
expect(screen.getByTestId('photo-items')).toHaveAttribute('data-layout', 'list');
await userEvent.click(screen.getByTestId('view-trash'));
await screen.findByTestId('trash-note');
expect(screen.getByTestId('layout-list')).toBeInTheDocument();
});
it('shows an error when the library cannot be loaded', async () => {
server.use(http.get('*/media-library/photos', () => new HttpResponse(null, { status: 500 })));
renderPage();
expect(await screen.findByTestId('media-error')).toBeInTheDocument();
});
});