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', 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() { 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( , ); // 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: 'Profil' })); expect(await screen.findByRole('heading', { name: 'Profil' })).toBeInTheDocument(); // open the personal data subpage via the section row await user.click(await screen.findByRole('link', { name: /Dane osobowe/ })); // tytuł podstrony jest tylko w topmenu (nagłówek Layout) expect(await screen.findByRole('heading', { name: 'Profil — dane osobowe' })).toBeInTheDocument(); // edycja per karta: „Edytuj” w karcie „Podstawowe dane” otwiera modal z formularzem // (pierwszy z trzech przycisków „Edytuj” — kolejność kart na stronie) const editButtons = await screen.findAllByRole('button', { name: 'Edytuj' }); await user.click(editButtons[0]); // formularz zainicjalizowany z danych /me await waitFor(() => { const age = document.querySelector('input[type="number"]') as HTMLInputElement; expect(age.value).toBe('30'); }); }); });