190 lines
7.9 KiB
TypeScript
190 lines
7.9 KiB
TypeScript
|
|
import { fireEvent, render, screen, waitFor } 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 } from '../src/api/types';
|
|||
|
|
|
|||
|
|
const USER: User = {
|
|||
|
|
id: 'u1',
|
|||
|
|
email: 'jan@test.local',
|
|||
|
|
firstName: 'Jan',
|
|||
|
|
lastName: 'Kowalski',
|
|||
|
|
age: 30,
|
|||
|
|
heightCm: 180,
|
|||
|
|
phone: null,
|
|||
|
|
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() {
|
|||
|
|
const sent: { name: string; value: number }[] = [];
|
|||
|
|
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/circumference') && method === 'POST') {
|
|||
|
|
const body = JSON.parse(String(init?.body)) as { name: string; value: number };
|
|||
|
|
sent.push(body);
|
|||
|
|
return jsonResponse(201, body);
|
|||
|
|
}
|
|||
|
|
if (url.includes('/me/body-parameters')) return jsonResponse(200, { weight: [], circumferences: [] });
|
|||
|
|
if (url.includes('/me/history')) return jsonResponse(200, { sessions: [] });
|
|||
|
|
if (url.includes('/me/focus-chain')) return jsonResponse(200, { priorities: [], exclusions: [] });
|
|||
|
|
if (url.includes('/me')) return jsonResponse(200, { user: USER, memberships: [] });
|
|||
|
|
if (url.includes('/focus-areas')) return jsonResponse(200, { focusAreas: [] });
|
|||
|
|
if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] });
|
|||
|
|
if (url.includes('/dashboard'))
|
|||
|
|
return jsonResponse(200, { killstreak: 0, activePlan: null, nextTraining: null, progress: null });
|
|||
|
|
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' } });
|
|||
|
|
}),
|
|||
|
|
);
|
|||
|
|
return { sent };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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();
|
|||
|
|
// body parameters live on their own subpage — open it via the tile
|
|||
|
|
await user.click(await screen.findByRole('link', { name: /Parametry ciała/ }));
|
|||
|
|
// tytuł podstrony jest tylko w topmenu (nagłówek Layout)
|
|||
|
|
expect(await screen.findByRole('heading', { name: 'Profil — parametry ciała' })).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();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
/** Formularz obwodu żyje w modalu otwieranym z menu kontekstowego. */
|
|||
|
|
async function openAddModal(user: userEvent.User) {
|
|||
|
|
// akcja „Dodaj pomiar” jest za ikoną ➕ (grupa dodawania)
|
|||
|
|
await user.click(screen.getByRole('button', { name: 'Dodawanie' }));
|
|||
|
|
await user.click(screen.getByRole('button', { name: 'Dodaj pomiar' }));
|
|||
|
|
expect(await screen.findByRole('dialog')).toBeInTheDocument();
|
|||
|
|
await user.click(screen.getByRole('button', { name: 'Obwód' }));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
describe('Profile — circumference form (predefined list)', () => {
|
|||
|
|
it('uses a predefined type list and shows the side toggle only for sided types', async () => {
|
|||
|
|
mockApi();
|
|||
|
|
const user = userEvent.setup();
|
|||
|
|
renderApp();
|
|||
|
|
await loginToProfile(user);
|
|||
|
|
|
|||
|
|
await openAddModal(user);
|
|||
|
|
const select = screen.getByLabelText('Rodzaj pomiaru');
|
|||
|
|
// predefiniowana lista typów pomiarów
|
|||
|
|
expect(screen.getByRole('option', { name: 'Klatka piersiowa' })).toBeInTheDocument();
|
|||
|
|
expect(screen.getByRole('option', { name: 'Biceps' })).toBeInTheDocument();
|
|||
|
|
expect(screen.getByRole('option', { name: 'Talia' })).toBeInTheDocument();
|
|||
|
|
expect(screen.getByRole('option', { name: 'Udo' })).toBeInTheDocument();
|
|||
|
|
expect(screen.getByRole('option', { name: 'Łydka' })).toBeInTheDocument();
|
|||
|
|
|
|||
|
|
// domyślny typ (klatka) nie ma strony lewy/prawy
|
|||
|
|
expect(screen.queryByRole('button', { name: 'Lewy' })).not.toBeInTheDocument();
|
|||
|
|
|
|||
|
|
// biceps ma przełącznik lewy/prawy
|
|||
|
|
await user.selectOptions(select, 'biceps');
|
|||
|
|
expect(await screen.findByRole('button', { name: 'Lewy' })).toBeInTheDocument();
|
|||
|
|
expect(screen.getByRole('button', { name: 'Prawy' })).toBeInTheDocument();
|
|||
|
|
|
|||
|
|
// talia znów ukrywa przełącznik
|
|||
|
|
await user.selectOptions(select, 'waist');
|
|||
|
|
expect(screen.queryByRole('button', { name: 'Lewy' })).not.toBeInTheDocument();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('sends the composed name (type + side) to the API', async () => {
|
|||
|
|
const { sent } = mockApi();
|
|||
|
|
const user = userEvent.setup();
|
|||
|
|
renderApp();
|
|||
|
|
await loginToProfile(user);
|
|||
|
|
|
|||
|
|
await openAddModal(user);
|
|||
|
|
const select = screen.getByLabelText('Rodzaj pomiaru');
|
|||
|
|
await user.selectOptions(select, 'biceps');
|
|||
|
|
await user.click(await screen.findByRole('button', { name: 'Prawy' }));
|
|||
|
|
await user.type(screen.getByPlaceholderText('wartość (cm)'), '32.5');
|
|||
|
|
// data pomiaru — nie musi być dniem dodawania
|
|||
|
|
fireEvent.change(screen.getByLabelText('Data obwodu'), { target: { value: '2026-08-10' } });
|
|||
|
|
|
|||
|
|
const form = select.closest('form')!;
|
|||
|
|
await user.click(form.querySelector('button[type="submit"]')!);
|
|||
|
|
|
|||
|
|
expect(await screen.findByText('Obwód dodany')).toBeInTheDocument();
|
|||
|
|
await waitFor(() =>
|
|||
|
|
expect(sent).toEqual([{ name: 'Biceps prawy', value: 32.5, date: '2026-08-10T00:00:00.000Z' }]),
|
|||
|
|
);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it('sends the plain type name for non-sided types', async () => {
|
|||
|
|
const { sent } = mockApi();
|
|||
|
|
const user = userEvent.setup();
|
|||
|
|
renderApp();
|
|||
|
|
await loginToProfile(user);
|
|||
|
|
|
|||
|
|
await openAddModal(user);
|
|||
|
|
const select = screen.getByLabelText('Rodzaj pomiaru');
|
|||
|
|
await user.selectOptions(select, 'waist');
|
|||
|
|
await user.type(screen.getByPlaceholderText('wartość (cm)'), '80');
|
|||
|
|
// pusta data = API używa bieżącej daty (pole date jest wstępnie wypełnione)
|
|||
|
|
fireEvent.change(screen.getByLabelText('Data obwodu'), { target: { value: '' } });
|
|||
|
|
|
|||
|
|
const form = select.closest('form')!;
|
|||
|
|
await user.click(form.querySelector('button[type="submit"]')!);
|
|||
|
|
|
|||
|
|
expect(await screen.findByText('Obwód dodany')).toBeInTheDocument();
|
|||
|
|
await waitFor(() => expect(sent).toEqual([{ name: 'Talia', value: 80 }]));
|
|||
|
|
});
|
|||
|
|
});
|