344 lines
11 KiB
TypeScript
344 lines
11 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
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';
|
|
|
|
const TRAINEE_ID = 'trainee-1';
|
|
|
|
const USER = {
|
|
id: 'trainer-1',
|
|
email: 'trener@test.local',
|
|
firstName: 'Tomasz',
|
|
lastName: 'Trener',
|
|
age: 35,
|
|
heightCm: 185,
|
|
phone: null,
|
|
isAdmin: false,
|
|
availability: null,
|
|
priorities: 'chest',
|
|
exclusions: 'knee',
|
|
developMuscles: [],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: [],
|
|
createdAt: '2026-08-01T10:00:00.000Z',
|
|
};
|
|
|
|
const OTHER_TRAINER = { id: 'trainer-2', firstName: 'Jan', lastName: 'Kowalski' };
|
|
|
|
const EXERCISES = [
|
|
{ id: 'ex-1', name: 'Przysiady', primaryMuscle: 'legs' },
|
|
{ id: 'ex-2', name: 'Wyciskanie', primaryMuscle: 'chest' },
|
|
];
|
|
|
|
const TRAINEE_DETAIL = {
|
|
user: { id: TRAINEE_ID, firstName: 'Anna', lastName: 'Nowak' },
|
|
gyms: [],
|
|
activePlan: null,
|
|
progress: null,
|
|
killstreak: 0,
|
|
nextTraining: null,
|
|
extra: null,
|
|
missing: null,
|
|
lastSessionAt: null,
|
|
flags: [],
|
|
};
|
|
|
|
const PLANS = [
|
|
{
|
|
id: 'plan-1',
|
|
title: 'Plan masowy',
|
|
status: 'DRAFT',
|
|
weeksCount: 1,
|
|
startDate: null,
|
|
creator: OTHER_TRAINER,
|
|
weeks: [],
|
|
},
|
|
{
|
|
id: 'plan-2',
|
|
title: 'Plan aktywny',
|
|
status: 'ACTIVE',
|
|
weeksCount: 4,
|
|
startDate: '2026-08-24',
|
|
creator: null,
|
|
weeks: [],
|
|
},
|
|
];
|
|
|
|
const DRAFT_DETAIL = {
|
|
id: 'plan-1',
|
|
title: 'Plan masowy',
|
|
status: 'DRAFT',
|
|
weeksCount: 1,
|
|
startDate: null,
|
|
creator: OTHER_TRAINER,
|
|
weeks: [
|
|
{
|
|
id: 'w1',
|
|
index: 0,
|
|
days: [
|
|
{
|
|
id: 'd1',
|
|
dayOfWeek: 1,
|
|
date: null,
|
|
status: 'PLANNED',
|
|
moveLog: [],
|
|
sessions: [],
|
|
exercises: [
|
|
{
|
|
id: 'pe1',
|
|
exerciseId: 'ex-1',
|
|
order: 0,
|
|
exercise: { id: 'ex-1', name: 'Przysiady', primaryMuscle: 'legs' },
|
|
sets: [
|
|
{
|
|
id: 's1',
|
|
index: 0,
|
|
reps: 10,
|
|
loadKg: 60,
|
|
restSec: 90,
|
|
rir: 2,
|
|
difficulty: 3,
|
|
cardioBpm: null,
|
|
timeSec: null,
|
|
speed: null,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
const ACTIVE_DETAIL = {
|
|
id: 'plan-2',
|
|
title: 'Plan aktywny',
|
|
status: 'ACTIVE',
|
|
weeksCount: 4,
|
|
startDate: '2026-08-24',
|
|
creator: null,
|
|
weeks: [
|
|
{
|
|
id: 'w1',
|
|
index: 0,
|
|
days: [
|
|
{
|
|
id: 'd1',
|
|
dayOfWeek: 1,
|
|
date: '2026-08-24',
|
|
status: 'PLANNED',
|
|
moveLog: [],
|
|
sessions: [],
|
|
exercises: [
|
|
{
|
|
id: 'pe2',
|
|
exerciseId: 'ex-2',
|
|
order: 0,
|
|
exercise: { id: 'ex-2', name: 'Wyciskanie', primaryMuscle: 'chest' },
|
|
sets: [
|
|
{
|
|
id: 's2',
|
|
index: 0,
|
|
reps: 8,
|
|
loadKg: 40,
|
|
restSec: 90,
|
|
rir: 2,
|
|
difficulty: 3,
|
|
cardioBpm: null,
|
|
timeSec: null,
|
|
speed: null,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
const LATEST_RESULTS = [
|
|
{ exerciseId: 'ex-1', name: 'Przysiady', muscle: 'legs', date: '2026-08-20', sets: [{ reps: 12, loadKg: 70 }], oneRm: 80.6 },
|
|
];
|
|
|
|
const PROGRESS = [
|
|
{
|
|
exerciseId: 'ex-1',
|
|
name: 'Przysiady',
|
|
muscle: 'legs',
|
|
points: [
|
|
{ date: '2026-08-13', oneRm: 75, volumeKg: 420, reps: 30, sets: 3 },
|
|
{ date: '2026-08-20', oneRm: 80.6, volumeKg: 480, reps: 36, sets: 3 },
|
|
],
|
|
},
|
|
];
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
function mockApi(opts: { plans?: unknown[] } = {}) {
|
|
const plans = opts.plans ?? PLANS;
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
const url = String(input);
|
|
const method = init?.method || 'GET';
|
|
if (url.includes('/auth/refresh')) return jsonResponse(200, { user: USER, accessToken: 'tok-1' });
|
|
if (url.includes('/me/active-session')) return jsonResponse(200, { session: null });
|
|
if (url.endsWith('/me'))
|
|
return jsonResponse(200, { user: USER, memberships: [{ gymId: 'gym-1', roles: ['TRAINER'] }] });
|
|
if (url.includes('/dashboard'))
|
|
return jsonResponse(200, {
|
|
killstreak: 0,
|
|
activePlan: null,
|
|
nextTraining: null,
|
|
progress: null,
|
|
extra: null,
|
|
missing: null,
|
|
});
|
|
if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] });
|
|
if (url.includes('/trainer/invitations')) return jsonResponse(200, { invitations: [] });
|
|
if (url.includes('/exercises')) return jsonResponse(200, { exercises: EXERCISES });
|
|
// najpierw szczegóły planu (URL zawiera też /plans i /trainees/:id)
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/plans/plan-1`)) {
|
|
if (method === 'PATCH' || method === 'DELETE') return jsonResponse(200, { ok: true });
|
|
return jsonResponse(200, { plan: DRAFT_DETAIL });
|
|
}
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/plans/plan-2`))
|
|
return jsonResponse(200, { plan: ACTIVE_DETAIL });
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/plans`)) {
|
|
if (method === 'POST') return jsonResponse(201, { plan: { id: 'plan-new' } });
|
|
return jsonResponse(200, { plans });
|
|
}
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/exercise-latest`))
|
|
return jsonResponse(200, { results: LATEST_RESULTS });
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/exercise-progress`))
|
|
return jsonResponse(200, { exercises: PROGRESS });
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/history`)) return jsonResponse(200, { sessions: [] });
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/report`))
|
|
return jsonResponse(200, { plan: null, muscles: [], days: [] });
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/moves`)) return jsonResponse(200, { moves: [] });
|
|
if (url.includes(`/trainer/trainees/${TRAINEE_ID}`)) return jsonResponse(200, TRAINEE_DETAIL);
|
|
return jsonResponse(200, {});
|
|
}),
|
|
);
|
|
}
|
|
|
|
function renderAt(path: string) {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: { queries: { retry: 0, refetchOnWindowFocus: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={queryClient}>
|
|
<MemoryRouter initialEntries={[path]}>
|
|
<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();
|
|
});
|
|
|
|
describe('Zakładka „Plany" w panelu podopiecznego', () => {
|
|
it('lista planów z badge statusu i etykietą twórcy; „Utwórz plan" w menu kontekstowym', async () => {
|
|
mockApi();
|
|
renderAt(`/trainees/${TRAINEE_ID}?tab=plans`);
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Anna Nowak' })).toBeInTheDocument();
|
|
await screen.findByRole('heading', { name: 'Plany podopiecznego' });
|
|
|
|
// akcja tworzenia planu jest w menu kontekstowym strony (topbar), nie w zakładce
|
|
expect(screen.getByRole('button', { name: 'Utwórz plan' })).toBeInTheDocument();
|
|
expect(screen.getByText('Plan masowy')).toBeInTheDocument();
|
|
expect(screen.getByText('Plan aktywny')).toBeInTheDocument();
|
|
// badge statusu
|
|
expect(screen.getByText('Szkic')).toBeInTheDocument();
|
|
expect(screen.getByText('Aktywny')).toBeInTheDocument();
|
|
// etykieta twórcy (twórca ≠ zalogowany trener)
|
|
expect(screen.getByText('utworzył: Jan Kowalski')).toBeInTheDocument();
|
|
});
|
|
|
|
it('pusty stan, gdy podopieczny nie ma planów', async () => {
|
|
mockApi({ plans: [] });
|
|
renderAt(`/trainees/${TRAINEE_ID}?tab=plans`);
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Plany podopiecznego' })).toBeInTheDocument();
|
|
expect(screen.getByText('Podopieczny nie ma żadnych planów.')).toBeInTheDocument();
|
|
});
|
|
|
|
it('szkic: akcje Edytuj/Aktywuj/Usuń i struktura inline', async () => {
|
|
mockApi();
|
|
const user = userEvent.setup();
|
|
renderAt(`/trainees/${TRAINEE_ID}?tab=plans`);
|
|
|
|
await screen.findByRole('heading', { name: 'Plany podopiecznego' });
|
|
await user.click(screen.getByRole('button', { name: 'Plan masowy' }));
|
|
|
|
expect(await screen.findByRole('button', { name: 'Edytuj' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'Aktywuj' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'Usuń' })).toBeInTheDocument();
|
|
// struktura inline
|
|
expect(screen.getByText('Tydzień 1')).toBeInTheDocument();
|
|
expect(screen.getByText('Poniedziałek')).toBeInTheDocument();
|
|
expect(screen.getByText(/Przysiady/)).toBeInTheDocument();
|
|
});
|
|
|
|
it('aktywny: akcje „Oznacz jako ukończony" i „Porzuć"', async () => {
|
|
mockApi();
|
|
const user = userEvent.setup();
|
|
renderAt(`/trainees/${TRAINEE_ID}?tab=plans`);
|
|
|
|
await screen.findByRole('heading', { name: 'Plany podopiecznego' });
|
|
await user.click(screen.getByRole('button', { name: 'Plan aktywny' }));
|
|
|
|
expect(await screen.findByRole('button', { name: 'Porzuć' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'Oznacz jako ukończony' })).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe('PlanEditor — tryb podopiecznego', () => {
|
|
it('„Wstaw wyniki" prefylluje serie z ostatniej sesji', async () => {
|
|
mockApi();
|
|
const user = userEvent.setup();
|
|
renderAt(`/trainees/${TRAINEE_ID}/plans/new`);
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Plan dla podopiecznego' })).toBeInTheDocument();
|
|
|
|
// dodaj dzień (Poniedziałek)
|
|
await user.click(screen.getByRole('button', { name: 'Pon' }));
|
|
// dodaj ćwiczenie Przysiady
|
|
const exerciseSelect = screen
|
|
.getAllByRole('combobox')
|
|
.find((s) => s.querySelector('option[value="ex-1"]'));
|
|
await user.selectOptions(exerciseSelect, 'ex-1');
|
|
|
|
// otwórz panel wyników i wstaw
|
|
await user.click(screen.getByRole('button', { name: 'Wyniki' }));
|
|
expect(await screen.findByText('Wstaw wyniki')).toBeInTheDocument();
|
|
await user.click(screen.getByRole('button', { name: 'Wstaw wyniki' }));
|
|
|
|
// serie z ostatniej sesji: 12 powt. @ 70 kg
|
|
expect(screen.getByDisplayValue('12')).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue('70')).toBeInTheDocument();
|
|
});
|
|
});
|