Files
FitOS/app/test/profile-progress.test.tsx
2026-09-04 19:06:19 +02:00

140 lines
5.1 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',
age: 30,
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: 'Profil' }));
expect(await screen.findByRole('heading', { name: 'Profil' })).toBeInTheDocument();
await user.click(screen.getByRole('link', { name: /Postępy/ }));
expect(await screen.findByRole('heading', { name: 'Postępy per ćwiczenie' })).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 — karty per ćwiczenie', () => {
it('pokazuje karty per ćwiczenie z ostatnim 1RM i objętością', async () => {
mockApi(PROGRESS);
const user = userEvent.setup();
renderApp();
await loginToProgress(user);
// karta z danymi: nazwa, partia, ostatnie 1RM i objętość
expect(screen.getByText('Przysiady')).toBeInTheDocument();
expect(screen.getByText('76 kg')).toBeInTheDocument();
expect(screen.getByText('600 kg')).toBeInTheDocument();
// karta bez obciążenia: puste wartości
expect(screen.getByText('Wyciskanie')).toBeInTheDocument();
});
it('pusty stan gdy brak ukończonych sesji', async () => {
mockApi([]);
const user = userEvent.setup();
renderApp();
await loginToProgress(user);
expect(screen.getByText('Brak ukończonych sesji — postępy pojawią się po pierwszym treningu.')).toBeInTheDocument();
});
});