2026-10-17 09:19:57 +02:00
|
|
|
|
import { fireEvent, render, screen } from '@testing-library/react';
|
2026-10-14 06:12:41 +02:00
|
|
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom';
|
|
|
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
|
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
|
|
|
|
import UserBodyParameters from '../src/pages/users/UserBodyParameters';
|
|
|
|
|
|
import { setAccessToken } from '../src/api/client';
|
|
|
|
|
|
|
|
|
|
|
|
const BP = {
|
2026-10-17 09:19:57 +02:00
|
|
|
|
weight: [
|
|
|
|
|
|
{ id: 'w1', value: 82, date: '2026-09-01T10:00:00.000Z' },
|
|
|
|
|
|
{ id: 'w2', value: 81.5, date: '2026-09-10T10:00:00.000Z' },
|
|
|
|
|
|
],
|
2026-10-14 06:12:41 +02:00
|
|
|
|
circumferences: [
|
|
|
|
|
|
{
|
2026-10-17 11:17:03 +02:00
|
|
|
|
typeId: 'biceps',
|
|
|
|
|
|
side: 'left',
|
|
|
|
|
|
name: null,
|
2026-10-17 09:19:57 +02:00
|
|
|
|
points: [
|
|
|
|
|
|
{ id: 'c1', value: 35, date: '2026-09-01T10:00:00.000Z' },
|
|
|
|
|
|
{ id: 'c3', value: 36, date: '2026-09-20T10:00:00.000Z' },
|
|
|
|
|
|
],
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2026-10-17 11:17:03 +02:00
|
|
|
|
typeId: 'biceps',
|
|
|
|
|
|
side: 'right',
|
|
|
|
|
|
name: null,
|
2026-10-17 09:19:57 +02:00
|
|
|
|
points: [{ id: 'c2', value: 35.5, date: '2026-09-09T10:00:00.000Z' }],
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
2026-10-17 11:17:03 +02:00
|
|
|
|
typeId: 'chest',
|
|
|
|
|
|
side: null,
|
|
|
|
|
|
name: null,
|
2026-10-17 09:19:57 +02:00
|
|
|
|
points: [{ id: 'c4', value: 100, date: '2026-09-05T10:00:00.000Z' }],
|
2026-10-14 06:12:41 +02:00
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Mock fetch wg fragmentu URL (odwrócona kolejność = specyficzne trasy wygrywają). */
|
|
|
|
|
|
function mockFetchRoutes(routes: Array<[string, () => unknown]>) {
|
|
|
|
|
|
const calls: { url: string; init?: RequestInit }[] = [];
|
|
|
|
|
|
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
|
|
|
|
|
calls.push({ url, init });
|
|
|
|
|
|
const entry = [...routes].reverse().find(([frag]) => url.includes(frag));
|
|
|
|
|
|
if (!entry) throw new Error(`No mocked route for url: ${url}`);
|
|
|
|
|
|
const body = entry[1]();
|
|
|
|
|
|
if (body === null) return new Response(null, { status: 204 });
|
|
|
|
|
|
return new Response(JSON.stringify(body), {
|
|
|
|
|
|
status: 200,
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
|
|
return calls;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderPage() {
|
|
|
|
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
|
|
|
|
return render(
|
|
|
|
|
|
<MemoryRouter initialEntries={['/users/u1/body-parameters']}>
|
|
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
|
|
<Routes>
|
|
|
|
|
|
<Route path="/users/:id/body-parameters" element={<UserBodyParameters />} />
|
|
|
|
|
|
</Routes>
|
|
|
|
|
|
</QueryClientProvider>
|
|
|
|
|
|
</MemoryRouter>,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
setAccessToken('admin-tok');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
|
vi.unstubAllGlobals();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('admin UserBodyParameters page', () => {
|
2026-10-17 09:19:57 +02:00
|
|
|
|
it('zakładki z pełną historią: waga + obwody grupowane po części (pełne nazwy)', async () => {
|
2026-10-14 06:12:41 +02:00
|
|
|
|
mockFetchRoutes([['/body-parameters', () => BP]]);
|
|
|
|
|
|
renderPage();
|
|
|
|
|
|
|
2026-10-16 23:33:17 +02:00
|
|
|
|
expect(
|
|
|
|
|
|
await screen.findByRole('heading', { name: 'Parametry ciała', level: 2 }),
|
|
|
|
|
|
).toBeInTheDocument();
|
2026-10-17 09:19:57 +02:00
|
|
|
|
|
|
|
|
|
|
// zakładki części z pełnymi nazwami (bez skrótu „Przedr.”)
|
|
|
|
|
|
expect(await screen.findByRole('button', { name: 'Waga' })).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Klatka piersiowa' })).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Biceps' })).toBeInTheDocument();
|
|
|
|
|
|
|
|
|
|
|
|
// panel Wagi (pierwsza zakładka): najnowszy pomiar + postęp + pełna historia
|
|
|
|
|
|
expect(screen.getByRole('heading', { name: 'Waga', level: 3 })).toBeInTheDocument();
|
2026-10-14 06:12:41 +02:00
|
|
|
|
expect(screen.getAllByText('81.5 kg').length).toBeGreaterThan(0);
|
2026-10-17 09:19:57 +02:00
|
|
|
|
expect(screen.getByRole('heading', { name: 'Postęp' })).toBeInTheDocument();
|
|
|
|
|
|
// separator dziesiętny bywa kropką lub przecinkiem zależnie od implementacji Intl
|
|
|
|
|
|
expect(screen.getByText(/−0[.,]5 kg od pierwszego/)).toBeInTheDocument();
|
|
|
|
|
|
// lista z pełnej historii (oba pomiary, nawet sprzed „bieżącego miesiąca”);
|
|
|
|
|
|
// wartość i jednostka to osobne węzły tekstowe w kafelku listy
|
|
|
|
|
|
const tiles = () =>
|
2026-10-17 11:17:03 +02:00
|
|
|
|
Array.from(document.querySelectorAll('li')).map((li) =>
|
|
|
|
|
|
(li.textContent ?? '').replace(/\s+/g, ' ').trim(),
|
2026-10-17 09:19:57 +02:00
|
|
|
|
);
|
|
|
|
|
|
expect(tiles().some((t) => t.startsWith('82 kg'))).toBe(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('strony mierzone jako dwa niezależne panele z własnym postępem', async () => {
|
|
|
|
|
|
mockFetchRoutes([['/body-parameters', () => BP]]);
|
|
|
|
|
|
renderPage();
|
|
|
|
|
|
|
|
|
|
|
|
const bicepsTab = await screen.findByRole('button', { name: 'Biceps' });
|
|
|
|
|
|
fireEvent.click(bicepsTab);
|
|
|
|
|
|
|
|
|
|
|
|
// lewy zawsze pierwszy, potem prawy — każdy jako osobna karta
|
|
|
|
|
|
expect(await screen.findByRole('heading', { name: 'Biceps — lewy' })).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getByRole('heading', { name: 'Biceps — prawy' })).toBeInTheDocument();
|
|
|
|
|
|
expect(screen.getAllByRole('heading', { name: 'Postęp' }).length).toBe(2);
|
|
|
|
|
|
expect(screen.getAllByText('Za mało danych na wykres (min. 2 pomiary).').length).toBe(1);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('pokaż więcej odkrywa kolejne pomiary z historii', async () => {
|
|
|
|
|
|
const many = {
|
|
|
|
|
|
weight: Array.from({ length: 5 }, (_, i) => ({
|
|
|
|
|
|
id: `w${i}`,
|
|
|
|
|
|
value: 80 + i / 10,
|
|
|
|
|
|
date: `2026-08-0${i + 1}T10:00:00.000Z`,
|
|
|
|
|
|
})),
|
|
|
|
|
|
circumferences: [],
|
|
|
|
|
|
};
|
|
|
|
|
|
mockFetchRoutes([['/body-parameters', () => many]]);
|
|
|
|
|
|
renderPage();
|
|
|
|
|
|
await screen.findByRole('heading', { name: 'Waga', level: 3 });
|
|
|
|
|
|
|
|
|
|
|
|
// domyślnie 3 najnowsze, gwóźdź programu: 4. pomiar ukryty do kliknięcia
|
|
|
|
|
|
const tiles = () =>
|
2026-10-17 11:17:03 +02:00
|
|
|
|
Array.from(document.querySelectorAll('li')).map((li) =>
|
|
|
|
|
|
(li.textContent ?? '').replace(/\s+/g, ' ').trim(),
|
2026-10-17 09:19:57 +02:00
|
|
|
|
);
|
|
|
|
|
|
expect(tiles().some((t) => /^80[.,]1 kg/.test(t))).toBe(false);
|
|
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'Pokaż więcej' }));
|
|
|
|
|
|
expect(tiles().some((t) => /^80[.,]1 kg/.test(t))).toBe(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('pokazuje stan pusty, gdy użytkownik nie ma pomiarów', async () => {
|
2026-10-17 11:17:03 +02:00
|
|
|
|
mockFetchRoutes([['/body-parameters', () => ({ weight: [], circumferences: [] })]]);
|
2026-10-17 09:19:57 +02:00
|
|
|
|
renderPage();
|
|
|
|
|
|
|
|
|
|
|
|
expect(await screen.findByText('Brak pomiarów')).toBeInTheDocument();
|
|
|
|
|
|
expect(
|
2026-10-17 11:17:03 +02:00
|
|
|
|
screen.getByText(
|
|
|
|
|
|
'Pomiary pojawią się tutaj, gdy użytkownik doda wagę lub obwód w aplikacji.',
|
|
|
|
|
|
),
|
2026-10-17 09:19:57 +02:00
|
|
|
|
).toBeInTheDocument();
|
2026-10-14 06:12:41 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-10-16 23:33:17 +02:00
|
|
|
|
it('jest tylko do odczytu — brak kontrolek dodawania i usuwania pomiarów', async () => {
|
|
|
|
|
|
mockFetchRoutes([['/body-parameters', () => BP]]);
|
2026-10-14 06:12:41 +02:00
|
|
|
|
renderPage();
|
|
|
|
|
|
await screen.findByRole('heading', { name: 'Waga', level: 3 });
|
|
|
|
|
|
|
2026-10-16 23:33:17 +02:00
|
|
|
|
expect(screen.queryByRole('button', { name: '+ Waga' })).not.toBeInTheDocument();
|
|
|
|
|
|
expect(screen.queryByRole('button', { name: 'Dodaj wagę' })).not.toBeInTheDocument();
|
2026-10-17 09:19:57 +02:00
|
|
|
|
expect(screen.queryByRole('button', { name: 'Dodaj pomiar' })).not.toBeInTheDocument();
|
2026-10-16 23:33:17 +02:00
|
|
|
|
expect(screen.queryAllByRole('button', { name: /Usuń pomiar/ })).toHaveLength(0);
|
2026-10-14 06:12:41 +02:00
|
|
|
|
});
|
|
|
|
|
|
});
|