126 lines
4.6 KiB
TypeScript
126 lines
4.6 KiB
TypeScript
import { 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',
|
|
birthDate: '1996-05-05T00:00:00.000Z',
|
|
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() {
|
|
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'))
|
|
return jsonResponse(200, { weight: [], circumferences: [] });
|
|
if (url.includes('/me/history')) return jsonResponse(200, { sessions: [] });
|
|
if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] });
|
|
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('/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' } });
|
|
}),
|
|
);
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
await new Promise((r) => setTimeout(r, 25));
|
|
setAccessToken(null);
|
|
localStorage.clear();
|
|
useAuth.setState({ user: null, ready: false });
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('Profile flow (repro of React #301)', () => {
|
|
it('login -> navigate to /profile -> profile renders without hook errors', async () => {
|
|
mockApi();
|
|
const user = userEvent.setup();
|
|
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: { queries: { retry: 0, refetchOnWindowFocus: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={queryClient}>
|
|
<MemoryRouter initialEntries={['/profile']}>
|
|
<App />
|
|
</MemoryRouter>
|
|
</QueryClientProvider>,
|
|
);
|
|
|
|
// not logged in -> redirected to /login
|
|
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' }));
|
|
|
|
// after login -> dashboard
|
|
expect(await screen.findByRole('heading', { name: 'Dashboard' })).toBeInTheDocument();
|
|
|
|
// navigate to profile overview
|
|
await user.click(screen.getByRole('link', { name: 'Mój profil' }));
|
|
expect(await screen.findByRole('heading', { name: 'Mój profil' })).toBeInTheDocument();
|
|
|
|
// open the personal data subpage via the section row
|
|
await user.click(await screen.findByRole('link', { name: /Dane osobowe/ }));
|
|
// wspólna nazwa sekcji: topbar (h1) = PageHeader (h2)
|
|
expect(
|
|
await screen.findByRole('heading', { name: 'Dane osobowe', level: 2 }),
|
|
).toBeInTheDocument();
|
|
|
|
// edycja z menu kontekstowego (topbar): „Edytuj dane” otwiera modal z formularzem
|
|
await user.click(screen.getByRole('button', { name: 'Edytuj dane' }));
|
|
// formularz zainicjalizowany z danych /me
|
|
await waitFor(() => {
|
|
const birthDate = document.querySelector('input[type="date"]') as HTMLInputElement;
|
|
expect(birthDate.value).toBe('1996-05-05');
|
|
});
|
|
});
|
|
});
|