806 lines
32 KiB
TypeScript
806 lines
32 KiB
TypeScript
import { render, screen, within } 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 { useListSearch } from '../src/store/list-search';
|
||
import { setAccessToken } from '../src/api/client';
|
||
import type { User } from '../src/api/types';
|
||
|
||
const TRAINER: User = {
|
||
id: 'trainer-1',
|
||
email: 'trener@test.local',
|
||
firstName: 'Marek',
|
||
lastName: 'Trener',
|
||
birthDate: '1991-01-01T00:00:00.000Z',
|
||
heightCm: 185,
|
||
phone: null,
|
||
isAdmin: false,
|
||
availability: null,
|
||
priorities: null,
|
||
exclusions: null,
|
||
developMuscles: [],
|
||
canDoMuscles: [],
|
||
difficultMuscles: [],
|
||
avoidMuscles: [],
|
||
createdAt: '2026-01-01T00:00:00.000Z',
|
||
};
|
||
|
||
const GYM = {
|
||
id: 'gym-1',
|
||
name: 'Żelazna Świątynia',
|
||
location: 'Warszawa',
|
||
availability: null,
|
||
capacity: null,
|
||
equipment: [],
|
||
};
|
||
|
||
const GYM_2 = {
|
||
id: 'gym-2',
|
||
name: 'Mega Gym',
|
||
location: 'Kraków',
|
||
availability: null,
|
||
capacity: null,
|
||
equipment: [],
|
||
};
|
||
|
||
const TRAINEE = {
|
||
user: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak' },
|
||
gyms: [GYM],
|
||
activePlan: {
|
||
id: 'plan-1',
|
||
title: 'Plan testowy',
|
||
progress: { completed: 5, total: 20, percent: 25 },
|
||
},
|
||
killstreak: 3,
|
||
lastSessionAt: '2026-08-24T07:00:00.000Z',
|
||
flags: ['streak_reset'],
|
||
};
|
||
|
||
const TRAINEE_DETAIL = {
|
||
coachingId: 'coaching-1',
|
||
user: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak' },
|
||
gyms: [GYM],
|
||
activePlan: { id: 'plan-1', title: 'Plan testowy', startDate: '2026-08-01' },
|
||
progress: { completed: 5, total: 20, percent: 25 },
|
||
killstreak: 3,
|
||
nextTraining: {
|
||
dayId: 'day-1',
|
||
date: '2026-08-25',
|
||
exercises: [{ name: 'Przysiady', muscle: 'legs' }],
|
||
},
|
||
extra: { sets: 5 },
|
||
missing: { sets: 1 },
|
||
lastSessionAt: '2026-08-24T07:00:00.000Z',
|
||
flags: ['streak_reset'],
|
||
};
|
||
|
||
const TRAINEE_2 = {
|
||
user: { id: 'trainee-2', firstName: 'Piotr', lastName: 'Kowal' },
|
||
gyms: [],
|
||
activePlan: null,
|
||
killstreak: 0,
|
||
lastSessionAt: null,
|
||
flags: [],
|
||
};
|
||
|
||
const TRAINEE_3 = {
|
||
user: { id: 'trainee-3', firstName: 'Zofia', lastName: 'Wiśniewska' },
|
||
gyms: [],
|
||
activePlan: null,
|
||
killstreak: 10,
|
||
lastSessionAt: '2026-09-03T07:00:00.000Z',
|
||
flags: [],
|
||
};
|
||
|
||
// trenowany w DWÓCH siłowniach trenera — przy grupowaniu widoczny w obu grupach
|
||
const TRAINEE_4 = {
|
||
user: { id: 'trainee-4', firstName: 'Tomek', lastName: 'Kowalczyk' },
|
||
gyms: [GYM, GYM_2],
|
||
activePlan: null,
|
||
killstreak: 2,
|
||
lastSessionAt: null,
|
||
flags: [],
|
||
};
|
||
|
||
const CANDIDATE = {
|
||
id: 'candidate-1',
|
||
firstName: 'Jan',
|
||
lastName: 'Kowalski',
|
||
email: 'jan.kowalski@test.local',
|
||
phone: '+48 500 100 200',
|
||
gyms: [{ id: 'gym-2', name: 'Mega Gym' }],
|
||
};
|
||
|
||
function jsonResponse(status: number, body: unknown) {
|
||
if (status === 204) return new Response(null, { status });
|
||
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: TRAINER, accessToken: 'tok-1' });
|
||
if (url.includes('/auth/refresh'))
|
||
return jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'no cookie' } });
|
||
if (url.includes('/messages/conversations/coaching-1'))
|
||
return jsonResponse(200, {
|
||
role: 'trainer',
|
||
other: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak', nickname: null },
|
||
messages: [],
|
||
});
|
||
if (url.includes('/messages/conversations'))
|
||
return jsonResponse(200, {
|
||
conversations: [
|
||
{
|
||
coachingId: 'coaching-1',
|
||
role: 'trainer',
|
||
other: { id: 'trainee-1', firstName: 'Anna', lastName: 'Nowak', nickname: null },
|
||
lastMessage: {
|
||
body: 'Dobrze się czuję',
|
||
createdAt: '2026-08-24T10:00:00.000Z',
|
||
senderId: 'trainee-1',
|
||
},
|
||
unreadCount: 1,
|
||
},
|
||
],
|
||
});
|
||
if (url.includes('/me'))
|
||
return jsonResponse(200, {
|
||
user: TRAINER,
|
||
memberships: [
|
||
{ gymId: GYM.id, roles: ['TRAINER'], gym: GYM },
|
||
{ gymId: GYM_2.id, roles: ['TRAINER'], gym: GYM_2 },
|
||
],
|
||
});
|
||
if (url.includes('/trainer/invitations/inv-1/accept'))
|
||
return jsonResponse(200, { coaching: { id: 'inv-1', status: 'ACTIVE' } });
|
||
if (url.includes('/trainer/invitations'))
|
||
return jsonResponse(
|
||
method === 'POST' ? 201 : 200,
|
||
method === 'POST'
|
||
? { coaching: { id: 'inv-new', status: 'PENDING' } }
|
||
: {
|
||
invitations: [
|
||
{
|
||
id: 'inv-1',
|
||
direction: 'received',
|
||
other: {
|
||
id: 'trainer-2',
|
||
firstName: 'Piotr',
|
||
lastName: 'Trener',
|
||
email: 'p.trener@test.local',
|
||
phone: null,
|
||
},
|
||
invitedAt: '2026-08-20T00:00:00.000Z',
|
||
expiresAt: '2026-08-27T00:00:00.000Z',
|
||
},
|
||
{
|
||
id: 'inv-2',
|
||
direction: 'sent',
|
||
other: {
|
||
id: 'trainee-2',
|
||
firstName: 'Pola',
|
||
lastName: 'Podopieczna',
|
||
email: 'pola@test.local',
|
||
phone: null,
|
||
},
|
||
invitedAt: '2026-08-21T00:00:00.000Z',
|
||
expiresAt: '2026-08-28T00:00:00.000Z',
|
||
},
|
||
],
|
||
},
|
||
);
|
||
if (url.includes('/trainer/trainees/trainee-1/moves'))
|
||
return jsonResponse(200, {
|
||
moves: [
|
||
{
|
||
dayId: 'day-3',
|
||
date: '2026-08-21T00:00:00.000Z',
|
||
fromDate: '2026-08-19T00:00:00.000Z',
|
||
toDate: '2026-08-21T00:00:00.000Z',
|
||
reason: 'choroba',
|
||
movedAt: '2026-08-19T09:00:00.000Z',
|
||
},
|
||
],
|
||
});
|
||
if (url.includes('/trainer/trainees/trainee-1/report'))
|
||
return jsonResponse(200, {
|
||
plan: { id: 'plan-1', title: 'Plan testowy', startDate: '2026-08-01T00:00:00.000Z' },
|
||
muscles: [
|
||
{
|
||
muscle: 'legs',
|
||
planned: { sets: 12, reps: 120 },
|
||
actual: { sets: 10, reps: 96 },
|
||
extra: { sets: 2 },
|
||
missing: { sets: 2 },
|
||
completionPct: 83,
|
||
recommendedRange: { setsMin: 10, setsMax: 18 },
|
||
outOfRange: false,
|
||
},
|
||
{
|
||
muscle: 'chest',
|
||
planned: { sets: 10, reps: 90 },
|
||
actual: { sets: 4, reps: 32 },
|
||
extra: { sets: 0 },
|
||
missing: { sets: 6 },
|
||
completionPct: 40,
|
||
recommendedRange: { setsMin: 10, setsMax: 20 },
|
||
outOfRange: true,
|
||
},
|
||
],
|
||
days: [
|
||
{ date: '2026-08-18T00:00:00.000Z', status: 'COMPLETED', moved: false },
|
||
{ date: '2026-08-20T00:00:00.000Z', status: 'SKIPPED', moved: false },
|
||
{ date: '2026-08-21T00:00:00.000Z', status: 'COMPLETED', moved: true },
|
||
{ date: '2026-08-25T00:00:00.000Z', status: 'PLANNED', moved: false },
|
||
],
|
||
// detale per cwiczenie: seria z komentarzem (callout w tabeli serii)
|
||
exercises: [
|
||
{
|
||
id: 'rep-ex-1',
|
||
name: 'Martwy ciąg',
|
||
muscle: 'legs',
|
||
isExtra: false,
|
||
sets: [
|
||
{
|
||
index: 1,
|
||
planned: { reps: 8, loadKg: 100, restSec: 120, rir: null },
|
||
actual: { reps: 8, rir: 1, difficulty: 4 },
|
||
note: 'bolely plecy',
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
if (url.includes('/trainer/trainees/trainee-1/history'))
|
||
return jsonResponse(200, {
|
||
sessions: [
|
||
{
|
||
id: 'hist-1',
|
||
status: 'COMPLETED',
|
||
startedAt: '2026-08-24T07:00:00.000Z',
|
||
endedAt: '2026-08-24T08:30:00.000Z',
|
||
date: '2026-08-24',
|
||
dayOfWeek: 1,
|
||
plan: 'Plan testowy',
|
||
exercises: [
|
||
{
|
||
id: 'hist-ex-1',
|
||
name: 'Przysiady',
|
||
muscle: 'legs',
|
||
isExtra: false,
|
||
sets: [
|
||
{
|
||
index: 0,
|
||
reps: 10,
|
||
rir: 2,
|
||
difficulty: 3,
|
||
planned: { reps: 10, loadKg: 60, restSec: 90, rir: 2 },
|
||
},
|
||
],
|
||
comments: [],
|
||
},
|
||
{
|
||
id: 'hist-ex-2',
|
||
name: 'Wyciskanie',
|
||
muscle: 'chest',
|
||
isExtra: true,
|
||
sets: [{ index: 0, reps: 8, rir: 1, difficulty: null, planned: null }],
|
||
comments: [],
|
||
},
|
||
],
|
||
},
|
||
{
|
||
id: 'hist-2',
|
||
status: 'COMPLETED',
|
||
startedAt: '2026-08-17T07:00:00.000Z',
|
||
endedAt: '2026-08-17T08:30:00.000Z',
|
||
date: '2026-08-17',
|
||
dayOfWeek: 1,
|
||
plan: 'Plan testowy',
|
||
exercises: [
|
||
{
|
||
id: 'hist-ex-3',
|
||
name: 'Przysiady',
|
||
muscle: 'legs',
|
||
isExtra: false,
|
||
sets: [
|
||
{
|
||
index: 0,
|
||
reps: 8,
|
||
rir: 3,
|
||
difficulty: 3,
|
||
planned: { reps: 10, loadKg: 60, restSec: 90, rir: 2 },
|
||
},
|
||
],
|
||
comments: [],
|
||
},
|
||
],
|
||
},
|
||
],
|
||
});
|
||
if (url.includes('/trainer/trainees/trainee-1/exercise-progress'))
|
||
return jsonResponse(200, {
|
||
exercises: [
|
||
{
|
||
exerciseId: 'ex-1',
|
||
name: 'Przysiady',
|
||
muscle: 'legs',
|
||
points: [
|
||
{ date: '2026-08-17', oneRm: 72, volumeKg: 480, reps: 8, sets: 1 },
|
||
{ date: '2026-08-24', oneRm: 76, volumeKg: 600, reps: 10, sets: 1 },
|
||
],
|
||
},
|
||
{
|
||
exerciseId: 'ex-2',
|
||
name: 'Wyciskanie',
|
||
muscle: 'chest',
|
||
points: [{ date: '2026-08-24', oneRm: null, volumeKg: null, reps: 8, sets: 1 }],
|
||
},
|
||
],
|
||
});
|
||
if (url.includes('/trainer/trainees/trainee-1'))
|
||
return jsonResponse(
|
||
method === 'DELETE' ? 204 : 200,
|
||
method === 'DELETE' ? {} : TRAINEE_DETAIL,
|
||
);
|
||
if (url.includes('/trainer/trainees'))
|
||
return jsonResponse(200, { trainees: [TRAINEE, TRAINEE_2, TRAINEE_3, TRAINEE_4] });
|
||
if (url.includes('/trainer/search')) return jsonResponse(200, { users: [CANDIDATE] });
|
||
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('/plans')) return jsonResponse(200, { plans: [] });
|
||
if (url.includes('/exercises')) return jsonResponse(200, { exercises: [] });
|
||
if (url.includes('/calendar')) return jsonResponse(200, { month: '2026-08', days: [] });
|
||
return jsonResponse(404, { error: { code: 'NOT_FOUND', message: 'not found' } });
|
||
}),
|
||
);
|
||
}
|
||
|
||
async function loginToTrainees(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, 'trener@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: 'Podopieczni' }));
|
||
expect(await screen.findByRole('heading', { name: 'Podopieczni' })).toBeInTheDocument();
|
||
}
|
||
|
||
function renderApp() {
|
||
const queryClient = new QueryClient({
|
||
defaultOptions: { queries: { retry: 0, refetchOnWindowFocus: false } },
|
||
});
|
||
return render(
|
||
<QueryClientProvider client={queryClient}>
|
||
<MemoryRouter initialEntries={['/']}>
|
||
<App />
|
||
</MemoryRouter>
|
||
</QueryClientProvider>,
|
||
);
|
||
}
|
||
|
||
beforeEach(async () => {
|
||
await new Promise((r) => setTimeout(r, 25));
|
||
setAccessToken(null);
|
||
localStorage.clear();
|
||
// sessionStorage trzyma ostatnio wybraną zakładkę panelu — czyszcz, aby testy
|
||
// startowały od „Przegląd” niezależnie od kolejności
|
||
sessionStorage.clear();
|
||
useAuth.setState({ user: null, ready: false });
|
||
useListSearch.setState({ queries: { plans: '', gyms: '', trainees: '', invitations: '' } });
|
||
});
|
||
|
||
afterEach(() => {
|
||
vi.unstubAllGlobals();
|
||
});
|
||
|
||
describe('Trainees — lista i szczegóły', () => {
|
||
it('trener widzi listę podopiecznych z metrykami i flagami', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
|
||
expect(await screen.findByText('Anna Nowak')).toBeInTheDocument();
|
||
// domyślnie lista płaska (bez nagłówków grup); nazwa siłowni: meta wiersza + opcja selecta
|
||
expect(screen.queryByRole('heading', { name: 'Żelazna Świątynia' })).not.toBeInTheDocument();
|
||
expect(screen.getAllByText('Żelazna Świątynia').length).toBeGreaterThanOrEqual(1);
|
||
expect(screen.getByText('Zerowany streak')).toBeInTheDocument();
|
||
expect(screen.getByText('5/20 dni')).toBeInTheDocument();
|
||
expect(screen.getByText('Plan testowy')).toBeInTheDocument();
|
||
});
|
||
|
||
it('wyszukiwarka w menu kontekstowym filtruje listę podopiecznych', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
expect(screen.getByText('Piotr Kowal')).toBeInTheDocument();
|
||
|
||
// wyszukiwarka jest w menu kontekstowym (nagłówek) za ikoną 🔍
|
||
await user.click(screen.getByRole('button', { name: 'Wyszukiwarka' }));
|
||
const searchbox = screen.getByRole('searchbox', { name: 'Szukaj podopiecznego' });
|
||
await user.type(searchbox, 'anna');
|
||
expect(screen.queryByText('Piotr Kowal')).not.toBeInTheDocument();
|
||
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
||
|
||
// pełne imię i nazwisko również znajduje podopiecznego
|
||
await user.clear(searchbox);
|
||
await user.type(searchbox, 'anna nowak');
|
||
expect(screen.queryByText('Piotr Kowal')).not.toBeInTheDocument();
|
||
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
||
});
|
||
|
||
it('select sortowania w topmenu zmienia kolejność listy (flagi/killstreak/nazwa/ostatnia sesja)', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
const NAMES = ['Anna Nowak', 'Piotr Kowal', 'Tomek Kowalczyk', 'Zofia Wiśniewska'];
|
||
// kolejność wierszy = kolejność przycisków-wierszy zawierających imię i nazwisko
|
||
const rowOrder = () =>
|
||
Array.from(document.querySelectorAll('button'))
|
||
.filter((b) => NAMES.some((n) => b.textContent?.includes(n)))
|
||
.map((b) => NAMES.find((n) => b.textContent!.includes(n))!);
|
||
// wybór opcji sortowania: otwórz dropdown „Sortuj: …” i kliknij opcję
|
||
const pickSort = async (option: string) => {
|
||
await user.click(screen.getByRole('button', { name: /Sortuj:/ }));
|
||
await user.click(await screen.findByRole('button', { name: option }));
|
||
};
|
||
|
||
// lista płaska (grupowanie wyłączone) — sortowanie globalne
|
||
// domyślnie: „Brak” — kolejność z API (Anna, Piotr, Zofia, Tomek)
|
||
expect(rowOrder()).toEqual([
|
||
'Anna Nowak',
|
||
'Piotr Kowal',
|
||
'Zofia Wiśniewska',
|
||
'Tomek Kowalczyk',
|
||
]);
|
||
|
||
await pickSort('Killstreak');
|
||
expect(rowOrder()).toEqual([
|
||
'Zofia Wiśniewska',
|
||
'Anna Nowak',
|
||
'Tomek Kowalczyk',
|
||
'Piotr Kowal',
|
||
]);
|
||
|
||
await pickSort('Nazwa');
|
||
expect(rowOrder()).toEqual([
|
||
'Anna Nowak',
|
||
'Piotr Kowal',
|
||
'Tomek Kowalczyk',
|
||
'Zofia Wiśniewska',
|
||
]);
|
||
|
||
await pickSort('Ostatnia sesja');
|
||
expect(rowOrder()).toEqual([
|
||
'Zofia Wiśniewska',
|
||
'Anna Nowak',
|
||
'Piotr Kowal',
|
||
'Tomek Kowalczyk',
|
||
]);
|
||
|
||
await pickSort('Flagi');
|
||
expect(rowOrder()).toEqual([
|
||
'Anna Nowak',
|
||
'Piotr Kowal',
|
||
'Tomek Kowalczyk',
|
||
'Zofia Wiśniewska',
|
||
]);
|
||
|
||
// „Brak” — powrót do kolejności z API
|
||
await pickSort('Brak');
|
||
expect(rowOrder()).toEqual([
|
||
'Anna Nowak',
|
||
'Piotr Kowal',
|
||
'Zofia Wiśniewska',
|
||
'Tomek Kowalczyk',
|
||
]);
|
||
});
|
||
|
||
it('grupowanie wg siłowni (toggle w filtrach): podopieczny z dwóch siłowni w obu grupach', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
// domyślnie lista płaska — brak nagłówków grup
|
||
expect(screen.queryByRole('heading', { name: 'Żelazna Świątynia' })).not.toBeInTheDocument();
|
||
|
||
await user.click(screen.getByRole('button', { name: 'Grupuj wg siłowni' }));
|
||
|
||
// zakładki = lista siłowni z podopiecznymi (siłownie alfabetycznie, „Inne” na końcu)
|
||
expect(screen.getByRole('button', { name: 'Inne' })).toBeInTheDocument();
|
||
expect(screen.getByRole('button', { name: 'Mega Gym' })).toBeInTheDocument();
|
||
expect(screen.getByRole('button', { name: 'Żelazna Świątynia' })).toBeInTheDocument();
|
||
// domyślnie pierwsza zakładka (Mega Gym) — tylko jej wiersze
|
||
expect(screen.getByRole('heading', { name: 'Mega Gym' })).toBeInTheDocument();
|
||
expect(screen.getByText('Tomek Kowalczyk')).toBeInTheDocument();
|
||
expect(screen.queryByText('Anna Nowak')).not.toBeInTheDocument();
|
||
|
||
// przełączenie na siłownię — Anna + Tomek (Tomek trenowany w obu siłowniach)
|
||
await user.click(screen.getByRole('button', { name: 'Żelazna Świątynia' }));
|
||
expect(screen.getByRole('heading', { name: 'Żelazna Świątynia' })).toBeInTheDocument();
|
||
// lokalizacja siłowni pod nagłówkiem grupy
|
||
expect(screen.getByText('Warszawa')).toBeInTheDocument();
|
||
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
||
expect(screen.getAllByText('Tomek Kowalczyk')).toHaveLength(1);
|
||
|
||
// Mega Gym — lokalizacja Kraków
|
||
await user.click(screen.getByRole('button', { name: 'Mega Gym' }));
|
||
expect(screen.getByText('Kraków')).toBeInTheDocument();
|
||
|
||
// „Inne” — Piotr i Zofia (bez lokalizacji)
|
||
await user.click(screen.getByRole('button', { name: 'Inne' }));
|
||
expect(screen.queryByText('Warszawa')).not.toBeInTheDocument();
|
||
expect(screen.getByRole('heading', { name: 'Inne' })).toBeInTheDocument();
|
||
expect(screen.getByText('Piotr Kowal')).toBeInTheDocument();
|
||
expect(screen.queryByText('Anna Nowak')).not.toBeInTheDocument();
|
||
|
||
// wyłączenie wraca do listy płaskiej (wszyscy, Tomek raz)
|
||
await user.click(screen.getByRole('button', { name: 'Grupuj wg siłowni' }));
|
||
expect(screen.queryByRole('heading', { name: 'Mega Gym' })).not.toBeInTheDocument();
|
||
expect(screen.getByText('Anna Nowak')).toBeInTheDocument();
|
||
expect(screen.getAllByText('Tomek Kowalczyk')).toHaveLength(1);
|
||
});
|
||
|
||
it('trener może wyszukać kandydata i wysłać zaproszenie', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
// top menu „Dodaj podopiecznego” (za ikoną ➕) otwiera modal wyszukiwania
|
||
await user.click(screen.getByRole('button', { name: 'Dodawanie' }));
|
||
await user.click(screen.getByRole('button', { name: 'Dodaj podopiecznego' }));
|
||
await user.type(await screen.findByPlaceholderText('Imię'), 'Jan');
|
||
await user.type(screen.getByPlaceholderText('Nazwisko'), 'Kowalski');
|
||
await user.click(screen.getByRole('button', { name: 'Szukaj' }));
|
||
expect(await screen.findByText(/jan\.kowalski@test\.local/)).toBeInTheDocument();
|
||
expect(screen.getByText(/\+48 500 100 200/)).toBeInTheDocument();
|
||
|
||
await user.click(screen.getByRole('button', { name: 'Zaproś' }));
|
||
expect(await screen.findByText('Zaproszenie wysłane')).toBeInTheDocument();
|
||
});
|
||
|
||
it('szczegóły podopiecznego: przegląd i historia z planem vs. fakty', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
expect(await screen.findByRole('heading', { name: 'Anna Nowak' })).toBeInTheDocument();
|
||
// overview tab
|
||
expect(screen.getByText('Następny trening')).toBeInTheDocument();
|
||
expect(screen.getByText('Nogi')).toBeInTheDocument();
|
||
expect(screen.getByText('+5')).toBeInTheDocument();
|
||
|
||
// history tab: nagłówek sesji z objętością i czasem trwania
|
||
await user.click(screen.getByRole('button', { name: 'Historia' }));
|
||
// objętość sesji (60×10) + czas trwania (07:00–08:30) — obie sesje po 1 h 30 min
|
||
expect(screen.getByText('600 kg')).toBeInTheDocument();
|
||
expect(screen.getAllByText('1 h 30 min').length).toBe(2);
|
||
|
||
// rozwinięta sesja: karty ćwiczeń (master/detail) — klik otwiera dialog z seriami
|
||
await user.click(await screen.findByRole('button', { name: /24\.08\.2026/ }));
|
||
expect(await screen.findByText('Przysiady')).toBeInTheDocument();
|
||
expect(screen.getByText('dodane')).toBeInTheDocument();
|
||
await user.click(screen.getByRole('button', { name: /Przysiady/ }));
|
||
const dialog = await screen.findByRole('dialog');
|
||
expect(within(dialog).getByText('S1')).toBeInTheDocument();
|
||
expect(within(dialog).getByText('plan 10 / 60 kg / 90 s')).toBeInTheDocument();
|
||
expect(within(dialog).getByText('RIR 2')).toBeInTheDocument();
|
||
expect(within(dialog).getAllByRole('img', { name: 'Trudność 3/5' }).length).toBeGreaterThanOrEqual(2);
|
||
});
|
||
|
||
it('Przegląd: karta Rozmowa z ostatnią wiadomością, nieprzeczytanymi i przejściem do wątku', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
expect(await screen.findByRole('heading', { name: 'Anna Nowak' })).toBeInTheDocument();
|
||
|
||
// karta Rozmowa: ostatnia wiadomość + badge nieprzeczytanych
|
||
expect(await screen.findByText('Rozmowa')).toBeInTheDocument();
|
||
expect(screen.getByText('Dobrze się czuję')).toBeInTheDocument();
|
||
const card = screen.getByText('Rozmowa').closest('[data-slot="card"]') as HTMLElement;
|
||
expect(within(card).getByText('1')).toBeInTheDocument();
|
||
|
||
// „Napisz wiadomość" otwiera wątek komunikatora
|
||
await user.click(within(card).getByRole('button', { name: /Napisz wiadomość/ }));
|
||
expect(await screen.findByRole('heading', { name: 'Anna Nowak' })).toBeInTheDocument();
|
||
expect(screen.getByRole('textbox', { name: 'Treść wiadomości' })).toBeInTheDocument();
|
||
});
|
||
|
||
it('trener archiwizuje podopiecznego i wraca na listę', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
await screen.findByRole('heading', { name: 'Anna Nowak' });
|
||
// „Archiwizuj” jest w grupie „Akcje” menu kontekstowego (domyślnie zwinięta)
|
||
await user.click(screen.getByRole('button', { name: 'Akcje' }));
|
||
await user.click(screen.getByRole('button', { name: 'Archiwizuj' }));
|
||
const dialog = await screen.findByRole('dialog');
|
||
expect(dialog).toBeInTheDocument();
|
||
await user.click(within(dialog).getByRole('button', { name: 'Archiwizuj' }));
|
||
expect(await screen.findByText('Relacja zarchiwizowana')).toBeInTheDocument();
|
||
expect(await screen.findByRole('heading', { name: 'Podopieczni' })).toBeInTheDocument();
|
||
});
|
||
|
||
it('akcja „Napisz wiadomość” otwiera wątek komunikatora z podopiecznym', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
await screen.findByRole('heading', { name: 'Anna Nowak' });
|
||
await user.click(screen.getByRole('button', { name: 'Akcje' }));
|
||
// akcja z topmenu (nie z karty Rozmowa w treści)
|
||
await user.click(
|
||
within(screen.getByRole('banner')).getByRole('button', { name: 'Napisz wiadomość' }),
|
||
);
|
||
|
||
// jesteśmy w wątku: pole pisania wiadomości
|
||
expect(await screen.findByRole('textbox', { name: 'Treść wiadomości' })).toBeInTheDocument();
|
||
});
|
||
|
||
it('zakładka Postępy: karty per ćwiczenie (1RM/objętość) i objętość per partia', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
await screen.findByRole('heading', { name: 'Anna Nowak' });
|
||
await user.click(screen.getByRole('button', { name: 'Postępy' }));
|
||
|
||
// lista per ćwiczenie: ostatnie 1RM, delta i objętość
|
||
expect(await screen.findByText('Postępy per ćwiczenie')).toBeInTheDocument();
|
||
expect(screen.getByText('Przysiady')).toBeInTheDocument();
|
||
expect(screen.getByText('Wyciskanie')).toBeInTheDocument();
|
||
expect(screen.getByText('76 kg')).toBeInTheDocument();
|
||
expect(screen.getByText('objętość 600 kg')).toBeInTheDocument();
|
||
// delta 1RM (72 → 76) + badge PR (ostatni 1RM = maksimum)
|
||
expect(screen.getByText('+4 kg')).toBeInTheDocument();
|
||
expect(screen.getByText('PR')).toBeInTheDocument();
|
||
// volume per muscle group with the recommended range and out-of-range badge
|
||
expect(screen.getByText('Objętość per partia')).toBeInTheDocument();
|
||
expect(screen.getByText('poza zakresem')).toBeInTheDocument();
|
||
expect(screen.getAllByText('Klatka').length).toBeGreaterThan(0);
|
||
expect(screen.getAllByText('Nogi').length).toBeGreaterThan(0);
|
||
// pasek objętości: fakty/plany + zalecany zakres
|
||
expect(screen.getByText('10/12 · 10–18')).toBeInTheDocument();
|
||
expect(screen.getByText('4/10 · 10–20')).toBeInTheDocument();
|
||
// detale cwiczen: komentarz serii widoczny jako callout z etykieta sekcji
|
||
expect(screen.getByText('Martwy ciąg')).toBeInTheDocument();
|
||
expect(screen.getByText('Komentarze przy seriach')).toBeInTheDocument();
|
||
expect(screen.getAllByText('bolely plecy').length).toBeGreaterThanOrEqual(1);
|
||
});
|
||
|
||
it('zakładka Dyscyplina: heatmapa dni i log przesunięć', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
await screen.findByRole('heading', { name: 'Anna Nowak' });
|
||
await user.click(screen.getByRole('button', { name: 'Dyscyplina' }));
|
||
|
||
expect(await screen.findByRole('heading', { name: 'Dyscyplina' })).toBeInTheDocument();
|
||
// heatmap legend
|
||
expect(screen.getByText('ukończony')).toBeInTheDocument();
|
||
expect(screen.getByText('pominięty')).toBeInTheDocument();
|
||
expect(screen.getByText('przesunięty')).toBeInTheDocument();
|
||
// moved-day log
|
||
expect(screen.getByText('Przesunięte dni')).toBeInTheDocument();
|
||
expect(screen.getByText('· choroba')).toBeInTheDocument();
|
||
});
|
||
|
||
it('zapamiętuje ostatnio wybraną zakładkę przy powrocie do podopiecznego', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
await loginToTrainees(user);
|
||
await screen.findByText('Anna Nowak');
|
||
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
await screen.findByRole('heading', { name: 'Anna Nowak' });
|
||
await user.click(screen.getByRole('button', { name: 'Dyscyplina' }));
|
||
await screen.findByText('Przesunięte dni');
|
||
|
||
// powrót na listę i ponowne wejście — zakładka Dyscyplina przywrócona bez klikania
|
||
// („Wstecz” jest w topmenu i w treści — oba prowadzą na listę, klikamy pierwszy)
|
||
await user.click(screen.getAllByRole('button', { name: 'Wstecz' })[0]);
|
||
await screen.findByRole('heading', { name: 'Podopieczni' });
|
||
await user.click(screen.getByRole('button', { name: /Anna Nowak/ }));
|
||
expect(await screen.findByText('Przesunięte dni')).toBeInTheDocument();
|
||
});
|
||
});
|
||
|
||
describe('Invitations — zaproszenia', () => {
|
||
it('lista zaproszeń: otrzymane i wysłane', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
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, 'trener@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: 'Zaproszenia' }));
|
||
expect(await screen.findByRole('heading', { name: 'Zaproszenia' })).toBeInTheDocument();
|
||
expect(screen.getByText('Otrzymane')).toBeInTheDocument();
|
||
expect(screen.getByText('Piotr Trener')).toBeInTheDocument();
|
||
expect(screen.getByText('Wysłane')).toBeInTheDocument();
|
||
expect(screen.getByText('Pola Podopieczna')).toBeInTheDocument();
|
||
expect(screen.getByRole('button', { name: 'Przyjmij' })).toBeInTheDocument();
|
||
expect(screen.getByRole('button', { name: 'Wycofaj' })).toBeInTheDocument();
|
||
});
|
||
|
||
it('przyjęcie i wycofanie zaproszenia', async () => {
|
||
mockApi();
|
||
const user = userEvent.setup();
|
||
renderApp();
|
||
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, 'trener@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: 'Zaproszenia' }));
|
||
await screen.findByRole('heading', { name: 'Zaproszenia' });
|
||
|
||
await user.click(screen.getByRole('button', { name: 'Przyjmij' }));
|
||
expect(await screen.findByText('Zaproszenie przyjęte')).toBeInTheDocument();
|
||
|
||
await user.click(screen.getByRole('button', { name: 'Wycofaj' }));
|
||
expect(await screen.findByText('Zaproszenie wycofane')).toBeInTheDocument();
|
||
});
|
||
});
|