2026-09-13 17:02:14 +02:00
|
|
|
|
import { render, screen, within } from '@testing-library/react';
|
2026-09-04 19:06:19 +02:00
|
|
|
|
import userEvent from '@testing-library/user-event';
|
|
|
|
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
|
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
|
import App from '../src/App';
|
|
|
|
|
|
import { useAuth } from '../src/store/auth';
|
|
|
|
|
|
import { setAccessToken } from '../src/api/client';
|
|
|
|
|
|
import type { User } from '../src/api/types';
|
|
|
|
|
|
|
|
|
|
|
|
const USER: User = {
|
|
|
|
|
|
id: 'u1',
|
|
|
|
|
|
email: 'jan@test.local',
|
|
|
|
|
|
firstName: 'Jan',
|
|
|
|
|
|
lastName: 'Kowalski',
|
|
|
|
|
|
age: 30,
|
|
|
|
|
|
heightCm: 180,
|
|
|
|
|
|
phone: '+48 600 000 000',
|
|
|
|
|
|
isAdmin: false,
|
|
|
|
|
|
availability: null,
|
|
|
|
|
|
priorities: 'chest',
|
|
|
|
|
|
exclusions: 'knee',
|
|
|
|
|
|
developMuscles: [],
|
|
|
|
|
|
canDoMuscles: [],
|
|
|
|
|
|
difficultMuscles: [],
|
|
|
|
|
|
avoidMuscles: [],
|
|
|
|
|
|
createdAt: '2026-08-22T10:00:00.000Z',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
|
|
|
|
return new Response(JSON.stringify(body), {
|
|
|
|
|
|
status,
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function mockApi() {
|
|
|
|
|
|
vi.stubGlobal(
|
|
|
|
|
|
'fetch',
|
|
|
|
|
|
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
|
|
|
|
const url = String(input);
|
|
|
|
|
|
const method = init?.method || 'GET';
|
|
|
|
|
|
if (url.includes('/auth/login')) return jsonResponse(200, { user: USER, accessToken: 'tok-1' });
|
|
|
|
|
|
if (url.includes('/auth/refresh'))
|
|
|
|
|
|
return jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'no cookie' } });
|
|
|
|
|
|
if (url.includes('/me/body-parameters'))
|
|
|
|
|
|
return jsonResponse(200, {
|
|
|
|
|
|
weight: [{ id: 'w-1', value: 82.5, date: '2026-08-15T08:00:00.000Z' }],
|
|
|
|
|
|
circumferences: [],
|
|
|
|
|
|
});
|
|
|
|
|
|
if (url.includes('/me/history'))
|
|
|
|
|
|
return jsonResponse(200, {
|
|
|
|
|
|
sessions: [
|
|
|
|
|
|
{
|
2026-09-13 17:02:14 +02:00
|
|
|
|
// bieżąca data (wczoraj) — wchodzi w „Objętość 7 dni”
|
2026-09-04 19:06:19 +02:00
|
|
|
|
id: 's-1',
|
|
|
|
|
|
status: 'COMPLETED',
|
2026-09-13 17:02:14 +02:00
|
|
|
|
startedAt: new Date(Date.now() - 86400000).toISOString(),
|
|
|
|
|
|
endedAt: new Date(Date.now() - 86400000 + 3600000).toISOString(),
|
|
|
|
|
|
date: new Date(Date.now() - 86400000).toISOString().slice(0, 10),
|
2026-09-04 19:06:19 +02:00
|
|
|
|
dayOfWeek: 1,
|
|
|
|
|
|
plan: 'Plan A',
|
2026-09-13 17:02:14 +02:00
|
|
|
|
exercises: [
|
|
|
|
|
|
{
|
|
|
|
|
|
id: 'e-1',
|
|
|
|
|
|
name: 'Przysiady',
|
|
|
|
|
|
muscle: 'legs',
|
|
|
|
|
|
isExtra: false,
|
|
|
|
|
|
comments: [],
|
|
|
|
|
|
sets: [
|
|
|
|
|
|
{
|
|
|
|
|
|
index: 0,
|
|
|
|
|
|
reps: 10,
|
|
|
|
|
|
rir: 2,
|
|
|
|
|
|
difficulty: null,
|
|
|
|
|
|
planned: { reps: 10, loadKg: 60, restSec: 90, rir: null },
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
2026-09-04 19:06:19 +02:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
id: 's-2',
|
|
|
|
|
|
status: 'COMPLETED',
|
|
|
|
|
|
startedAt: '2026-08-22T16:00:00.000Z',
|
|
|
|
|
|
endedAt: '2026-08-22T17:00:00.000Z',
|
|
|
|
|
|
date: '2026-08-22',
|
|
|
|
|
|
dayOfWeek: 6,
|
|
|
|
|
|
plan: 'Plan A',
|
|
|
|
|
|
exercises: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
id: 's-3',
|
|
|
|
|
|
status: 'ABANDONED',
|
|
|
|
|
|
startedAt: '2026-08-20T16:00:00.000Z',
|
|
|
|
|
|
endedAt: null,
|
|
|
|
|
|
date: '2026-08-20',
|
|
|
|
|
|
dayOfWeek: 4,
|
|
|
|
|
|
plan: 'Plan A',
|
|
|
|
|
|
exercises: [],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
if (url.includes('/gyms'))
|
|
|
|
|
|
return jsonResponse(200, {
|
|
|
|
|
|
gyms: [
|
|
|
|
|
|
{
|
|
|
|
|
|
id: 'g-1',
|
|
|
|
|
|
name: 'Żelazna Świątynia',
|
|
|
|
|
|
location: 'Warszawa',
|
|
|
|
|
|
availability: null,
|
|
|
|
|
|
capacity: null,
|
|
|
|
|
|
equipment: [],
|
|
|
|
|
|
roles: ['MEMBER'],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
if (url.includes('/me')) return jsonResponse(200, { user: USER, memberships: [] });
|
|
|
|
|
|
if (url.includes('/dashboard'))
|
2026-09-13 17:02:14 +02:00
|
|
|
|
return jsonResponse(200, { killstreak: 3, activePlan: null, nextTraining: null, progress: null });
|
2026-09-04 19:06:19 +02:00
|
|
|
|
if (url.includes('/exercises')) return jsonResponse(200, { exercises: [] });
|
|
|
|
|
|
if (url.includes('/plans')) return jsonResponse(200, { plans: [] });
|
|
|
|
|
|
if (url.includes('/calendar')) return jsonResponse(200, { month: '2026-08', days: [] });
|
|
|
|
|
|
return jsonResponse(404, { error: { code: 'NOT_FOUND', message: 'not found' } });
|
|
|
|
|
|
}),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function loginToProfile(user: userEvent.User) {
|
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'FitOS' })).toBeInTheDocument();
|
|
|
|
|
|
const email = document.querySelector('input[type="email"]') as HTMLInputElement;
|
|
|
|
|
|
const password = document.querySelector('input[type="password"]') as HTMLInputElement;
|
|
|
|
|
|
await user.type(email, 'jan@test.local');
|
|
|
|
|
|
await user.type(password, 'Password123!');
|
|
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Zaloguj' }));
|
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Dashboard' })).toBeInTheDocument();
|
|
|
|
|
|
await user.click(screen.getByRole('link', { name: 'Profil' }));
|
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Profil' })).toBeInTheDocument();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderApp() {
|
|
|
|
|
|
const queryClient = new QueryClient({
|
|
|
|
|
|
defaultOptions: { queries: { retry: 0, refetchOnWindowFocus: false } },
|
|
|
|
|
|
});
|
|
|
|
|
|
render(
|
|
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
|
|
<MemoryRouter initialEntries={['/profile']}>
|
|
|
|
|
|
<App />
|
|
|
|
|
|
</MemoryRouter>
|
|
|
|
|
|
</QueryClientProvider>,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
|
await new Promise((r) => setTimeout(r, 25));
|
|
|
|
|
|
setAccessToken(null);
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
|
useAuth.setState({ user: null, ready: false });
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
|
vi.unstubAllGlobals();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-09-13 17:02:14 +02:00
|
|
|
|
describe('Profile overview — hero, statystyki i wiersze sekcji', () => {
|
|
|
|
|
|
it('renders hero, mini-stats and section rows with summaries', async () => {
|
2026-09-04 19:06:19 +02:00
|
|
|
|
mockApi();
|
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
|
renderApp();
|
|
|
|
|
|
await loginToProfile(user);
|
|
|
|
|
|
|
2026-09-13 17:02:14 +02:00
|
|
|
|
// hero: nazwa + e-mail
|
|
|
|
|
|
expect(screen.getByText('Jan Kowalski')).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText('jan@test.local')).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
|
|
// pasek statystyk: killstreak, ostatnia waga, objętość 7 dni (60×10 z wczorajszej sesji), sesje
|
|
|
|
|
|
// (wartość killstreak w zakresie karty — „Sesje” też ma wartość 3)
|
|
|
|
|
|
const killStat = screen.getByText('Killstreak').closest('div.min-w-0') as HTMLElement;
|
|
|
|
|
|
expect(within(killStat).getByText('3')).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText('Ostatnia waga')).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText('82.5 kg')).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText('Objętość 7 dni')).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText('600 kg')).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByText('Sesje')).toBeInTheDocument();
|
|
|
|
|
|
|
2026-09-04 19:06:19 +02:00
|
|
|
|
// dane osobowe: imię i nazwisko + wiek/wzrost
|
2026-09-13 17:02:14 +02:00
|
|
|
|
const infoRow = await screen.findByRole('link', { name: /Dane osobowe/ });
|
|
|
|
|
|
expect(infoRow).toHaveTextContent('Jan Kowalski');
|
|
|
|
|
|
expect(infoRow).toHaveTextContent('30 lat · 180 cm');
|
2026-09-04 19:06:19 +02:00
|
|
|
|
|
|
|
|
|
|
// parametry ciała: najnowsza waga z datą
|
2026-09-13 17:02:14 +02:00
|
|
|
|
const bpRow = screen.getByRole('link', { name: /Parametry ciała/ });
|
|
|
|
|
|
expect(bpRow).toHaveTextContent('82.5 kg · 15.08.2026');
|
2026-09-04 19:06:19 +02:00
|
|
|
|
|
2026-09-13 17:02:14 +02:00
|
|
|
|
// historia: liczba sesji (polska odmiana) + data ostatniej (wczoraj)
|
|
|
|
|
|
const histRow = screen.getByRole('link', { name: /Historia treningów/ });
|
|
|
|
|
|
const lastDate = new Date(Date.now() - 86400000).toLocaleDateString('pl-PL', {
|
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
});
|
|
|
|
|
|
expect(histRow).toHaveTextContent(`3 sesje · ostatnia ${lastDate}`);
|
2026-09-04 19:06:19 +02:00
|
|
|
|
|
2026-09-13 17:02:14 +02:00
|
|
|
|
// siłownie: nazwy (sidebar ma też link „Siłownie" — wiersz niesie nazwę siłowni)
|
|
|
|
|
|
const gymsRow = screen.getByRole('link', { name: /^Siłownie / });
|
|
|
|
|
|
expect(gymsRow).toHaveTextContent('Żelazna Świątynia');
|
2026-09-04 19:06:19 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-09-13 17:02:14 +02:00
|
|
|
|
it('section rows navigate to the subpage and back', async () => {
|
2026-09-04 19:06:19 +02:00
|
|
|
|
mockApi();
|
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
|
renderApp();
|
|
|
|
|
|
await loginToProfile(user);
|
|
|
|
|
|
|
|
|
|
|
|
await user.click(await screen.findByRole('link', { name: /Historia treningów/ }));
|
|
|
|
|
|
// tytuł podstrony jest tylko w topmenu (nagłówek Layout)
|
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Profil — historia' })).toBeInTheDocument();
|
2026-09-13 17:02:14 +02:00
|
|
|
|
// pełna sekcja na podstronie (nagłówek sesji: data · plan)
|
|
|
|
|
|
const lastDate = new Date(Date.now() - 86400000).toLocaleDateString('pl-PL', {
|
|
|
|
|
|
day: '2-digit',
|
|
|
|
|
|
month: '2-digit',
|
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
|
});
|
|
|
|
|
|
expect(screen.getByText(`${lastDate} · Plan A`)).toBeInTheDocument();
|
2026-09-04 19:06:19 +02:00
|
|
|
|
|
2026-09-13 17:02:14 +02:00
|
|
|
|
// wstecz wraca na przegląd („Wstecz” jest w topmenu i w treści — oba prowadzą do /profile)
|
2026-09-11 16:31:11 +02:00
|
|
|
|
await user.click(screen.getAllByRole('button', { name: 'Wstecz' })[0]);
|
2026-09-04 19:06:19 +02:00
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Profil' })).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByRole('link', { name: /Dane osobowe/ })).toBeInTheDocument();
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|