161 lines
5.8 KiB
TypeScript
161 lines
5.8 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 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'],
|
|
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('modal „Konto” wysyła PATCH z e-mailem i flagą admina', async () => {
|
|
const calls = defaultRoutes();
|
|
const user = userEvent.setup();
|
|
renderUserInfo();
|
|
await screen.findByRole('heading', { name: 'Dane osobowe — jano' });
|
|
|
|
// pierwsza karta to „Konto” → pierwszy przycisk „Edytuj”
|
|
await user.click(screen.getAllByRole('button', { name: 'Edytuj' })[0]);
|
|
const emailInput = (await screen.findByRole('dialog')).querySelector(
|
|
'input[type="email"]',
|
|
) as HTMLInputElement;
|
|
await user.clear(emailInput);
|
|
await user.type(emailInput, 'nowy@fitos.pl');
|
|
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
|
|
|
const patch = calls.find((c) => c.init?.method === 'PATCH');
|
|
expect(patch).toBeDefined();
|
|
expect(JSON.parse(patch?.init?.body as string)).toMatchObject({
|
|
email: 'nowy@fitos.pl',
|
|
isAdmin: true,
|
|
});
|
|
});
|
|
|
|
it('modal „Podstawowe dane” wysyła PATCH z nickiem', async () => {
|
|
const calls = defaultRoutes();
|
|
const user = userEvent.setup();
|
|
renderUserInfo();
|
|
await screen.findByRole('heading', { name: 'Dane osobowe — jano' });
|
|
|
|
// druga karta (za kontem) → drugi przycisk „Edytuj”
|
|
await user.click(screen.getAllByRole('button', { name: 'Edytuj' })[1]);
|
|
const nickInput = await screen.findByPlaceholderText('np. fitfan');
|
|
await user.clear(nickInput);
|
|
await user.type(nickInput, 'jasiu');
|
|
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
|
|
|
const patch = calls.find((c) => c.init?.method === 'PATCH');
|
|
expect(patch).toBeDefined();
|
|
expect(JSON.parse(patch?.init?.body as string)).toMatchObject({ nickname: 'jasiu' });
|
|
});
|
|
});
|