Continuous Integration / config-asf (pull_request) Successful in 7s
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 12s
Continuous Integration / compile-sites (pull_request) Successful in 1m53s
Continuous Integration / backend-build (pull_request) Successful in 1m54s
Continuous Integration / vulnerability-scan (pull_request) Successful in 2m22s
Continuous Integration / frontend-prepare (pull_request) Successful in 2m46s
Continuous Integration / frontend-build (pull_request) Successful in 1m8s
Continuous Integration / backend-test (pull_request) Successful in 3m8s
Continuous Integration / frontend-test (pull_request) Successful in 2m48s
Continuous Integration / frontend-lint (pull_request) Successful in 3m26s
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
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
226 lines
12 KiB
TypeScript
226 lines
12 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 () => {
|
|
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');
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|