175 lines
6.5 KiB
TypeScript
175 lines
6.5 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';
|
|
import type { User, ExerciseProgress } from '../src/api/types';
|
|
|
|
const USER: User = {
|
|
id: 'u1',
|
|
email: 'jan@test.local',
|
|
firstName: 'Jan',
|
|
lastName: 'Kowalski',
|
|
birthDate: '1996-05-05T00:00:00.000Z',
|
|
heightCm: 180,
|
|
phone: '+48 600 000 000',
|
|
isAdmin: false,
|
|
availability: null,
|
|
priorities: null,
|
|
exclusions: null,
|
|
developMuscles: [],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: [],
|
|
createdAt: '2026-08-22T10:00:00.000Z',
|
|
};
|
|
|
|
const PROGRESS: ExerciseProgress[] = [
|
|
{
|
|
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 }],
|
|
},
|
|
];
|
|
|
|
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(progress: ExerciseProgress[]) {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
const url = String(input);
|
|
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/exercise-progress')) return jsonResponse(200, { exercises: progress });
|
|
if (url.includes('/me')) return jsonResponse(200, { user: USER, memberships: [] });
|
|
if (url.includes('/dashboard'))
|
|
return jsonResponse(200, {
|
|
killstreak: 0,
|
|
activePlan: null,
|
|
nextTraining: null,
|
|
progress: null,
|
|
});
|
|
if (url.includes('/messages/conversations')) return jsonResponse(200, { conversations: [] });
|
|
if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] });
|
|
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 loginToProgress(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: 'Mój profil' }));
|
|
expect(await screen.findByRole('heading', { name: 'Mój profil' })).toBeInTheDocument();
|
|
await user.click(screen.getByRole('link', { name: /Postępy/ }));
|
|
expect(await screen.findByRole('heading', { name: 'Postępy', level: 2 })).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();
|
|
useAuth.setState({ user: null, ready: false });
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('Profile progress — lista per ćwiczenie', () => {
|
|
it('pokazuje wiersze per ćwiczenie z 1RM, deltą, objętością i badge PR', async () => {
|
|
mockApi(PROGRESS);
|
|
const user = userEvent.setup();
|
|
renderApp();
|
|
await loginToProgress(user);
|
|
|
|
// wiersz z danymi: nazwa, partia, ostatnie 1RM, delta vs poprzednia sesja, objętość
|
|
expect(screen.getByText('Przysiady')).toBeInTheDocument();
|
|
expect(screen.getByText('76 kg')).toBeInTheDocument();
|
|
expect(screen.getByText('objętość 600 kg')).toBeInTheDocument();
|
|
// delta 1RM: 72 → 76 = +4 kg
|
|
expect(screen.getByText('+4 kg')).toBeInTheDocument();
|
|
// PR: ostatni 1RM = maksimum w historii ćwiczenia
|
|
expect(screen.getByText('PR')).toBeInTheDocument();
|
|
// wiersz bez obciążenia: puste wartości, bez PR i bez delty
|
|
expect(screen.getByText('Wyciskanie')).toBeInTheDocument();
|
|
expect(screen.getByText('brak objętości')).toBeInTheDocument();
|
|
});
|
|
|
|
it('rozwiniecie wiersza pokazuje zakładki 1RM/Objętość z wykresem', async () => {
|
|
mockApi(PROGRESS);
|
|
const user = userEvent.setup();
|
|
renderApp();
|
|
await loginToProgress(user);
|
|
|
|
// domyślnie zwinięte — brak zakładek
|
|
expect(screen.queryByRole('button', { name: 'Objętość' })).not.toBeInTheDocument();
|
|
|
|
// tap na wierszu rozwija kartę z zakładkami
|
|
await user.click(screen.getByRole('button', { name: /Przysiady/ }));
|
|
expect(await screen.findByRole('button', { name: '1RM' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'Objętość' })).toBeInTheDocument();
|
|
// aktywna zakładka 1RM → wykres liniowy
|
|
expect(document.querySelector('.recharts-line')).not.toBeNull();
|
|
|
|
// przełączenie na Objętość → wykres słupkowy
|
|
await user.click(screen.getByRole('button', { name: 'Objętość' }));
|
|
expect(document.querySelector('.recharts-bar')).not.toBeNull();
|
|
});
|
|
|
|
it('pusty stan gdy brak ukończonych sesji', async () => {
|
|
mockApi([]);
|
|
const user = userEvent.setup();
|
|
renderApp();
|
|
await loginToProgress(user);
|
|
|
|
expect(screen.getByText('Brak postępów')).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText('Postępy pojawią się po pierwszym ukończonym treningu.'),
|
|
).toBeInTheDocument();
|
|
});
|
|
});
|