101 lines
3.9 KiB
TypeScript
101 lines
3.9 KiB
TypeScript
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { SaveAsSmartCollectionDialog } from './SaveAsSmartCollectionDialog';
|
|
|
|
function jsonResponse(body: unknown, status = 200): Response {
|
|
return new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' }, status });
|
|
}
|
|
|
|
const smartCollections = [
|
|
{ id: 5, name: 'Recent Sci-Fi', query: 'genre:"Science Fiction"' },
|
|
{ id: 6, name: 'Kids', query: 'tag:kids' }
|
|
];
|
|
|
|
interface Recorded {
|
|
url: string;
|
|
method: string;
|
|
body: unknown;
|
|
}
|
|
|
|
function mockApi() {
|
|
const calls: Recorded[] = [];
|
|
vi.spyOn(window, 'fetch').mockImplementation((input: RequestInfo | URL, init?: RequestInit) => {
|
|
const url = input.toString();
|
|
const method = (init?.method ?? 'GET').toUpperCase();
|
|
const body = init?.body ? JSON.parse(String(init.body)) : undefined;
|
|
calls.push({ body, method, url });
|
|
|
|
if (url === '/api/smart-collections' && method === 'GET') {
|
|
return Promise.resolve(jsonResponse(smartCollections));
|
|
}
|
|
|
|
if (url === '/api/smart-collections' && method === 'POST') {
|
|
const name = (body as { name: string }).name;
|
|
return Promise.resolve(jsonResponse({ id: 42, name, query: (body as { query: string }).query }, 201));
|
|
}
|
|
|
|
return Promise.resolve(new Response(null, { status: 204 }));
|
|
});
|
|
|
|
return { calls };
|
|
}
|
|
|
|
describe('SaveAsSmartCollectionDialog', () => {
|
|
beforeEach(() => {
|
|
window.localStorage.clear();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
});
|
|
|
|
it('renders nothing when closed', () => {
|
|
mockApi();
|
|
const { container } = render(
|
|
<SaveAsSmartCollectionDialog onClose={() => {}} open={false} query="star trek" />
|
|
);
|
|
expect(container).toBeEmptyDOMElement();
|
|
});
|
|
|
|
it('loads existing smart collections plus the new option', async () => {
|
|
mockApi();
|
|
render(<SaveAsSmartCollectionDialog onClose={() => {}} open query="star trek" />);
|
|
|
|
await waitFor(() => expect(screen.getByRole('option', { name: 'Recent Sci-Fi' })).toBeTruthy());
|
|
expect(screen.getByRole('option', { name: 'Kids' })).toBeTruthy();
|
|
expect(screen.getByRole('option', { name: '(New smart collection)' })).toBeTruthy();
|
|
});
|
|
|
|
it('creates a new smart collection with the current query', async () => {
|
|
const onSaved = vi.fn();
|
|
const onClose = vi.fn();
|
|
const { calls } = mockApi();
|
|
render(<SaveAsSmartCollectionDialog onClose={onClose} onSaved={onSaved} open query="star trek" />);
|
|
|
|
await waitFor(() => expect(screen.getByRole('option', { name: '(New smart collection)' })).toBeTruthy());
|
|
fireEvent.change(screen.getByPlaceholderText('Smart collection name'), { target: { value: 'Trek' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /Save as smart collection/ }));
|
|
|
|
await waitFor(() => expect(onSaved).toHaveBeenCalledWith('Trek'));
|
|
const createCall = calls.find((call) => call.url === '/api/smart-collections' && call.method === 'POST');
|
|
expect(createCall?.body).toEqual({ name: 'Trek', query: 'star trek' });
|
|
expect(onClose).toHaveBeenCalled();
|
|
});
|
|
|
|
it('updates an existing smart collection query when one is chosen', async () => {
|
|
const onSaved = vi.fn();
|
|
const { calls } = mockApi();
|
|
render(<SaveAsSmartCollectionDialog onClose={() => {}} onSaved={onSaved} open query="star trek" />);
|
|
|
|
await waitFor(() => expect(screen.getByRole('option', { name: 'Kids' })).toBeTruthy());
|
|
fireEvent.change(screen.getByRole('combobox'), { target: { value: '6' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /Save as smart collection/ }));
|
|
|
|
await waitFor(() => expect(onSaved).toHaveBeenCalledWith('Kids'));
|
|
const updateCall = calls.find((call) => call.url === '/api/smart-collections/6');
|
|
expect(updateCall?.method).toBe('PUT');
|
|
expect(updateCall?.body).toEqual({ name: 'Kids', query: 'star trek' });
|
|
});
|
|
});
|