199 lines
6.1 KiB
TypeScript
199 lines
6.1 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
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 UserDetail from '../src/pages/users/UserDetail';
|
|
import { setAccessToken } from '../src/api/client';
|
|
import type { Gym, User } from '../src/api/types';
|
|
|
|
const G1: Gym = {
|
|
id: 'g1',
|
|
name: 'Gym A',
|
|
location: 'Warszawa',
|
|
availability: null,
|
|
capacity: 50,
|
|
equipment: [],
|
|
_count: { memberships: 1 },
|
|
};
|
|
|
|
const U1: User = {
|
|
id: 'u1',
|
|
email: 'jan@test.local',
|
|
firstName: 'Jan',
|
|
lastName: 'Kowalski',
|
|
nickname: 'jano',
|
|
socialMedia: null,
|
|
age: 30,
|
|
heightCm: 180,
|
|
phone: null,
|
|
isAdmin: true,
|
|
availability: null,
|
|
priorities: null,
|
|
exclusions: null,
|
|
developMuscles: ['chest'],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: [],
|
|
status: 'ACTIVE',
|
|
suspensionUntil: null,
|
|
restrictionReason: null,
|
|
createdAt: '2026-08-01T10:00:00.000Z',
|
|
memberships: [{ gymId: 'g1', roles: ['OWNER', 'TRAINER'], gym: G1 }],
|
|
};
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Mockuje fetch wg fragmentu URL (odporny na kolejność żądań). Zwraca listę wywołań.
|
|
* Fragmenty pasujące do wielu endpointów muszą być rejestrowane od najbardziej ogólnego.
|
|
*/
|
|
function mockFetchRoutes(routes: Array<[string, () => unknown]>) {
|
|
const calls: { url: string; init?: RequestInit }[] = [];
|
|
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
|
calls.push({ url, init });
|
|
// odwrócona kolejność rejestracji: specyficzne trasy wygrywają z ogólnymi
|
|
const entry = [...routes].reverse().find(([frag]) => url.includes(frag));
|
|
if (!entry) throw new Error(`No mocked route for url: ${url}`);
|
|
return jsonResponse(200, entry[1]());
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
return calls;
|
|
}
|
|
|
|
function renderUserDetail() {
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
return render(
|
|
<MemoryRouter initialEntries={['/users/u1']}>
|
|
<QueryClientProvider client={queryClient}>
|
|
<Routes>
|
|
<Route path="/users/:id" element={<UserDetail />} />
|
|
</Routes>
|
|
</QueryClientProvider>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setAccessToken('admin-tok');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin UserDetail (przegląd profilu)', () => {
|
|
it('pokazuje badge bana na koncie zablokowanym', async () => {
|
|
mockFetchRoutes([
|
|
[
|
|
'/admin/users',
|
|
() => ({ users: [{ ...U1, status: 'BANNED', restrictionReason: 'Powód' }] }),
|
|
],
|
|
['/killstreak', () => ({ killstreak: 0 })],
|
|
['/body-parameters', () => ({ weight: [], circumferences: [] })],
|
|
['/history', () => ({ sessions: [] })],
|
|
['/claims', () => ({ claims: [] })],
|
|
]);
|
|
renderUserDetail();
|
|
expect(await screen.findByText('Zablokowane')).toBeInTheDocument();
|
|
});
|
|
|
|
it('pokazuje badge zawieszenia z terminem', async () => {
|
|
mockFetchRoutes([
|
|
[
|
|
'/admin/users',
|
|
() => ({
|
|
users: [
|
|
{
|
|
...U1,
|
|
status: 'SUSPENDED',
|
|
suspensionUntil: '2026-11-01T12:00:00.000Z',
|
|
restrictionReason: null,
|
|
},
|
|
],
|
|
}),
|
|
],
|
|
['/killstreak', () => ({ killstreak: 0 })],
|
|
['/body-parameters', () => ({ weight: [], circumferences: [] })],
|
|
['/history', () => ({ sessions: [] })],
|
|
['/claims', () => ({ claims: [] })],
|
|
]);
|
|
renderUserDetail();
|
|
expect(await screen.findByText(/Zawieszone do/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('renderuje hero, mini-statystyki i wiersze sekcji', async () => {
|
|
mockFetchRoutes([
|
|
['/admin/users', () => ({ users: [U1] })],
|
|
['/killstreak', () => ({ killstreak: 7 })],
|
|
[
|
|
'/body-parameters',
|
|
() => ({
|
|
weight: [{ id: 'w1', value: 81.5, date: '2026-09-10T10:00:00.000Z' }],
|
|
circumferences: [],
|
|
}),
|
|
],
|
|
['/history', () => ({ sessions: [] })],
|
|
['/claims', () => ({ claims: [] })],
|
|
]);
|
|
renderUserDetail();
|
|
|
|
// hero: nazwa + e-mail + nick + rola admina
|
|
expect(await screen.findByText('jano')).toBeInTheDocument();
|
|
expect(screen.getByText('jan@test.local')).toBeInTheDocument();
|
|
expect(screen.getByText('@jano')).toBeInTheDocument();
|
|
expect(screen.getByText('Admin')).toBeInTheDocument();
|
|
|
|
// mini-statystyki z endpointów
|
|
expect(screen.getByText('Killstreak')).toBeInTheDocument();
|
|
expect(screen.getByText('7')).toBeInTheDocument();
|
|
expect(screen.getByText('81.5 kg')).toBeInTheDocument();
|
|
|
|
// wiersze sekcji prowadzące na podstrony
|
|
for (const title of [
|
|
'Dane osobowe',
|
|
'Parametry ciała',
|
|
'Historia treningów',
|
|
'Postępy',
|
|
'Członkostwa w siłowniach',
|
|
'Wnioski',
|
|
]) {
|
|
expect(screen.getByRole('link', { name: new RegExp(title) })).toHaveAttribute(
|
|
'href',
|
|
`/users/u1/${kebab(title)}`,
|
|
);
|
|
}
|
|
});
|
|
|
|
it('podsumowanie członkostw pokazuje siłownie i role', async () => {
|
|
mockFetchRoutes([
|
|
['/admin/users', () => ({ users: [U1] })],
|
|
['/killstreak', () => ({ killstreak: 0 })],
|
|
['/body-parameters', () => ({ weight: [], circumferences: [] })],
|
|
['/history', () => ({ sessions: [] })],
|
|
['/claims', () => ({ claims: [] })],
|
|
]);
|
|
renderUserDetail();
|
|
const row = await screen.findByRole('link', { name: /Członkostwa w siłowniach/ });
|
|
expect(row).toHaveTextContent('Gym A');
|
|
expect(row).toHaveTextContent('OWNER, TRAINER');
|
|
});
|
|
});
|
|
|
|
// tytuł sekcji → memberships | claims | info | body-parameters | history | progress
|
|
function kebab(title: string): string {
|
|
const map: Record<string, string> = {
|
|
'Dane osobowe': 'info',
|
|
'Parametry ciała': 'body-parameters',
|
|
'Historia treningów': 'history',
|
|
Postępy: 'progress',
|
|
'Członkostwa w siłowniach': 'memberships',
|
|
Wnioski: 'claims',
|
|
};
|
|
return map[title];
|
|
}
|