148 lines
4.7 KiB
TypeScript
148 lines
4.7 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 UserInfo from '../src/pages/users/UserInfo';
|
|
import { setAccessToken } from '../src/api/client';
|
|
import type { FocusArea, User } from '../src/api/types';
|
|
|
|
const AREAS: FocusArea[] = [
|
|
{
|
|
id: 'fa-1',
|
|
name: 'Klatka piersiowa',
|
|
muscle: 'chest',
|
|
description: null,
|
|
exercisesCount: 0,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
updatedAt: '2026-01-01T00:00:00.000Z',
|
|
},
|
|
{
|
|
id: 'fa-2',
|
|
name: 'Nogi',
|
|
muscle: 'legs',
|
|
description: null,
|
|
exercisesCount: 0,
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
updatedAt: '2026-01-01T00:00:00.000Z',
|
|
},
|
|
];
|
|
|
|
const CHAIN = {
|
|
develop: [
|
|
{ muscle: 'chest', name: 'Klatka piersiowa', exercises: [{ id: 'pz-1', name: 'Wyciskanie' }] },
|
|
],
|
|
canDo: [],
|
|
difficult: [],
|
|
avoid: [],
|
|
};
|
|
|
|
const U1: User = {
|
|
id: 'u1',
|
|
email: 'jan@test.local',
|
|
firstName: 'Jan',
|
|
lastName: 'Kowalski',
|
|
nickname: 'jano',
|
|
socialMedia: [{ platform: 'INSTAGRAM', url: 'https://instagram.com/jano' }],
|
|
age: 30,
|
|
heightCm: 180,
|
|
phone: '+48 500 100 200',
|
|
isAdmin: true,
|
|
availability: null,
|
|
priorities: null,
|
|
exclusions: null,
|
|
developMuscles: ['chest'],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: ['legs'],
|
|
status: 'ACTIVE',
|
|
suspensionUntil: null,
|
|
restrictionReason: null,
|
|
createdAt: '2026-08-01T10:00:00.000Z',
|
|
memberships: [],
|
|
};
|
|
|
|
/** Mock fetch wg fragmentu URL (odwrócona kolejność = specyficzne trasy wygrywają). */
|
|
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 });
|
|
const entry = [...routes].reverse().find(([frag]) => url.includes(frag));
|
|
if (!entry) throw new Error(`No mocked route for url: ${url}`);
|
|
return new Response(JSON.stringify(entry[1]()), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
return calls;
|
|
}
|
|
|
|
function renderUserInfo() {
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
return render(
|
|
<MemoryRouter initialEntries={['/users/u1/info']}>
|
|
<QueryClientProvider client={queryClient}>
|
|
<Routes>
|
|
<Route path="/users/:id/info" element={<UserInfo />} />
|
|
</Routes>
|
|
</QueryClientProvider>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
function defaultRoutes() {
|
|
return mockFetchRoutes([
|
|
['/admin/users', () => ({ users: [U1] })],
|
|
['/focus-areas', () => ({ focusAreas: AREAS })],
|
|
['/focus-chain', () => CHAIN],
|
|
]);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setAccessToken('admin-tok');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin UserInfo page', () => {
|
|
it('renderuje karty konta, danych, priorytetów i social media', async () => {
|
|
defaultRoutes();
|
|
renderUserInfo();
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Dane osobowe — jano' })).toBeInTheDocument();
|
|
|
|
// karta „Konto” (dodatek admina) + dane adresowe
|
|
expect(screen.getByText('Konto')).toBeInTheDocument();
|
|
expect(screen.getByText('jan@test.local')).toBeInTheDocument();
|
|
expect(screen.getByText('30 lat')).toBeInTheDocument();
|
|
expect(screen.getByText('180 cm')).toBeInTheDocument();
|
|
expect(screen.getByText('+48 500 100 200')).toBeInTheDocument();
|
|
|
|
// priorytety: nazwy mięśni rozwinięte z listy obszarów
|
|
expect(screen.getAllByText('Co rozwija').length).toBeGreaterThan(0);
|
|
expect(screen.getAllByText('Klatka piersiowa').length).toBeGreaterThan(0);
|
|
expect(screen.getAllByText('Co unikać').length).toBeGreaterThan(0);
|
|
expect(screen.getByText('Nogi')).toBeInTheDocument();
|
|
|
|
// social media jako link
|
|
const ig = screen.getByRole('link', { name: 'Instagram' });
|
|
expect(ig).toHaveAttribute('href', 'https://instagram.com/jano');
|
|
|
|
// ciąg przyczynowo-skutkowy z endpointu
|
|
expect(screen.getByText('Ciąg przyczynowo-skutkowy')).toBeInTheDocument();
|
|
expect(await screen.findByText('do rozwoju: Wyciskanie')).toBeInTheDocument();
|
|
});
|
|
|
|
it('jest wyłącznie do odczytu — brak kontrolek edycji', async () => {
|
|
defaultRoutes();
|
|
renderUserInfo();
|
|
await screen.findByRole('heading', { name: 'Dane osobowe — jano' });
|
|
|
|
expect(screen.queryAllByRole('button', { name: 'Edytuj' })).toHaveLength(0);
|
|
expect(screen.queryByRole('button', { name: 'Zapisz' })).not.toBeInTheDocument();
|
|
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
|
});
|
|
});
|