131 lines
4.1 KiB
TypeScript
131 lines
4.1 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { MemoryRouter, Route, Routes, useParams } from 'react-router-dom';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import GymLogsPage from '../src/pages/gyms/GymLogsPage';
|
|
import { setAccessToken } from '../src/api/client';
|
|
import type { ActivityLog } from '../src/api/types';
|
|
|
|
const GYM = {
|
|
id: 'g1',
|
|
name: 'Gym A',
|
|
location: 'Warszawa',
|
|
availability: null,
|
|
capacity: null,
|
|
kind: 'REAL',
|
|
isPrivate: false,
|
|
equipment: [],
|
|
_count: { memberships: 3 },
|
|
};
|
|
|
|
const LOG1: ActivityLog = {
|
|
id: 'log-1',
|
|
action: 'gym.created',
|
|
entityType: 'gym',
|
|
entityId: 'g1',
|
|
details: { actorId: 'admin-1', name: 'Gym A' },
|
|
createdAt: '2026-08-20T10:00:00.000Z',
|
|
actor: { id: 'admin-1', email: 'admin@fitos.pl', firstName: 'Ada', lastName: 'Admin' },
|
|
};
|
|
|
|
const LOG2: ActivityLog = {
|
|
id: 'log-2',
|
|
action: 'membership.created',
|
|
entityType: 'membership',
|
|
entityId: 'user-9',
|
|
details: { actorId: 'admin-1', userId: 'user-9', gymId: 'g1', roles: ['MEMBER'] },
|
|
createdAt: '2026-08-21T11:00:00.000Z',
|
|
actor: { id: 'admin-1', email: 'admin@fitos.pl', firstName: 'Ada', lastName: 'Admin' },
|
|
};
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
function DetailStub() {
|
|
const { id } = useParams();
|
|
return <div>GYM-DETAIL-{id}</div>;
|
|
}
|
|
|
|
function renderPage() {
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
return render(
|
|
<MemoryRouter initialEntries={['/gyms/g1/logs']}>
|
|
<QueryClientProvider client={queryClient}>
|
|
<Routes>
|
|
<Route path="/gyms/:id" element={<DetailStub />} />
|
|
<Route path="/gyms/:id/logs" element={<GymLogsPage />} />
|
|
</Routes>
|
|
</QueryClientProvider>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setAccessToken('admin-tok');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin GymLogsPage', () => {
|
|
it('renderuje wspólną tabelę logów z operacjami siłowni', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = String(input);
|
|
if (url.includes('/logs')) {
|
|
return jsonResponse(200, { logs: [LOG2, LOG1], total: 2 });
|
|
}
|
|
return jsonResponse(200, GYM);
|
|
}),
|
|
);
|
|
renderPage();
|
|
expect(await screen.findByText('Siłownia utworzona')).toBeInTheDocument();
|
|
expect(screen.getAllByText('Ada Admin').length).toBeGreaterThan(0);
|
|
// szczegóły membership: powiązanie z siłownią widoczne w kolumnie szczegółów
|
|
expect(await screen.findByText('siłownia: g1')).toBeInTheDocument();
|
|
// powrót na detal siłowni
|
|
expect(screen.getByRole('link', { name: /Wstecz/ })).toHaveAttribute('href', '/gyms/g1');
|
|
});
|
|
|
|
it('pusty stan, gdy brak operacji powiązanych z siłownią', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = String(input);
|
|
if (url.includes('/logs')) return jsonResponse(200, { logs: [], total: 0 });
|
|
return jsonResponse(200, GYM);
|
|
}),
|
|
);
|
|
renderPage();
|
|
expect(await screen.findByText('Brak operacji powiązanych z tą siłownią.')).toBeInTheDocument();
|
|
});
|
|
|
|
it('„Pokaż jeszcze” dokłada limit i odpytuje API', async () => {
|
|
const urls: string[] = [];
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = String(input);
|
|
urls.push(url);
|
|
if (url.includes('/logs'))
|
|
return jsonResponse(200, {
|
|
logs: [LOG1],
|
|
total: 60,
|
|
});
|
|
return jsonResponse(200, GYM);
|
|
}),
|
|
);
|
|
const user = userEvent.setup();
|
|
renderPage();
|
|
await user.click(await screen.findByRole('button', { name: 'Pokaż jeszcze 59 wpisów' }));
|
|
await vi.waitFor(() => expect(urls.some((u) => u.includes('limit=100'))).toBe(true));
|
|
});
|
|
});
|