87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
import { render, screen, act } from '@testing-library/react';
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import FocusAreas from '../src/pages/catalog/FocusAreas';
|
|
import { setAccessToken } from '../src/api/client';
|
|
import { useListSearch } from '../src/store/list-search';
|
|
import type { FocusArea } from '../src/api/types';
|
|
|
|
const A1: FocusArea = {
|
|
id: 'fa-1',
|
|
name: 'Klatka piersiowa',
|
|
muscle: 'chest',
|
|
description: 'Wyciskanie, rozpiętki.',
|
|
setsMin: 10,
|
|
setsMax: 20,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
updatedAt: '2026-01-01T00:00:00.000Z',
|
|
};
|
|
|
|
const A2: FocusArea = {
|
|
id: 'fa-2',
|
|
name: 'Nogi',
|
|
muscle: 'legs',
|
|
description: 'Przysiady, wykroki.',
|
|
setsMin: null,
|
|
setsMax: null,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
updatedAt: '2026-01-01T00:00:00.000Z',
|
|
};
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
function mockFetchSequence(...responses: Response[]) {
|
|
const fetchMock = vi.fn(async () => {
|
|
const next = responses.shift();
|
|
if (!next) throw new Error('No more mocked responses');
|
|
return next;
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
return fetchMock;
|
|
}
|
|
|
|
function renderFocusAreas() {
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
return render(
|
|
<MemoryRouter initialEntries={['/focus-areas']}>
|
|
<QueryClientProvider client={queryClient}>
|
|
<FocusAreas />
|
|
</QueryClientProvider>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setAccessToken('admin-tok');
|
|
useListSearch.getState().setQuery('focus-areas', '');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin FocusAreas page', () => {
|
|
it('lists focus areas', async () => {
|
|
mockFetchSequence(jsonResponse(200, { focusAreas: [A1, A2] }));
|
|
renderFocusAreas();
|
|
expect(await screen.findByText('Klatka piersiowa')).toBeInTheDocument();
|
|
expect(screen.getByText('Nogi')).toBeInTheDocument();
|
|
});
|
|
|
|
it('filters focus areas by the search box', async () => {
|
|
mockFetchSequence(jsonResponse(200, { focusAreas: [A1, A2] }));
|
|
renderFocusAreas();
|
|
await screen.findByText('Klatka piersiowa');
|
|
// wyszukiwarka jest w menu kontekstowym (Layout) — filtr ustawiamy przez store
|
|
act(() => useListSearch.getState().setQuery('focus-areas', 'nogi'));
|
|
expect(screen.queryByText('Klatka piersiowa')).not.toBeInTheDocument();
|
|
expect(screen.getByText('Nogi')).toBeInTheDocument();
|
|
});
|
|
});
|