Files
FitOS/admin/test/UserLogsPage.test.tsx

121 lines
3.7 KiB
TypeScript

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import UserLogsPage from '../src/pages/users/UserLogsPage';
import { setAccessToken } from '../src/api/client';
import type { ActivityLog, User } from '../src/api/types';
const U1: User = {
id: 'u1',
email: 'jan@test.local',
firstName: 'Jan',
lastName: 'Kowalski',
nickname: 'jano',
socialMedia: null,
birthDate: null,
heightCm: null,
phone: null,
isAdmin: false,
availability: null,
priorities: null,
exclusions: null,
developMuscles: [],
canDoMuscles: [],
difficultMuscles: [],
avoidMuscles: [],
status: 'ACTIVE',
suspensionUntil: null,
restrictionReason: null,
createdAt: '2026-08-01T10:00:00.000Z',
};
const LOG1: ActivityLog = {
id: 'log-1',
action: 'user.logged_in',
entityType: 'user',
entityId: 'u1',
details: { actorId: 'u1', client: 'app', ip: '203.0.113.9' },
createdAt: '2026-10-19T18:30:00.000Z',
actor: { id: 'u1', email: 'jan@test.local', firstName: 'Jan', lastName: 'Kowalski' },
};
function jsonResponse(status: number, body: unknown) {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
});
}
function renderPage() {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
return render(
<MemoryRouter initialEntries={['/users/u1/logs']}>
<QueryClientProvider client={queryClient}>
<Routes>
<Route path="/users/:id/logs" element={<UserLogsPage />} />
</Routes>
</QueryClientProvider>
</MemoryRouter>,
);
}
beforeEach(() => {
setAccessToken('admin-tok');
});
afterEach(() => {
vi.unstubAllGlobals();
});
describe('admin UserLogsPage', () => {
it('renderuje wspólną tabelę logów na koncie (IP w szczegółach)', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.includes('/activity')) {
return jsonResponse(200, { logs: [LOG1], total: 1 });
}
return jsonResponse(200, { users: [U1] });
}),
);
renderPage();
expect(await screen.findByText('Logowanie')).toBeInTheDocument();
expect(await screen.findByText('adres IP: 203.0.113.9')).toBeInTheDocument();
// powrót do konsoli użytkownika
expect(screen.getByRole('link', { name: /Wstecz/ })).toHaveAttribute('href', '/users/u1');
});
it('pusty stan, gdy brak zdarzeń na koncie', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.includes('/activity')) return jsonResponse(200, { logs: [], total: 0 });
return jsonResponse(200, { users: [U1] });
}),
);
renderPage();
expect(await screen.findByText('Brak zdarzeń audytu na tym koncie.')).toBeInTheDocument();
});
it('„Pokaż kolejne” 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('/activity')) return jsonResponse(200, { logs: [LOG1], total: 60 });
return jsonResponse(200, { users: [U1] });
}),
);
const user = userEvent.setup();
renderPage();
await user.click(await screen.findByRole('button', { name: 'Pokaż kolejne 59' }));
await vi.waitFor(() => expect(urls.some((u) => u.includes('limit=100'))).toBe(true));
});
});