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 { ADMIN_URL } from '../src/config'; import { useAuth } from '../src/store/auth'; import { setAccessToken } from '../src/api/client'; import type { User } from '../src/api/types'; function user(overrides: Partial = {}): User { return { 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', ...overrides, }; } function jsonResponse(status: number, body: unknown) { return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' }, }); } function mockApi(current: User) { vi.stubGlobal( 'fetch', vi.fn(async (input: RequestInfo | URL) => { const url = String(input); if (url.includes('/auth/refresh')) return jsonResponse(401, { error: { code: 'UNAUTHORIZED', message: 'no cookie' } }); if (url.includes('/auth/login')) return jsonResponse(200, { user: current, accessToken: 'tok-1' }); if (url.includes('/me/active-session')) return jsonResponse(200, { session: null }); if (url.includes('/me/feed/unread-count')) return jsonResponse(200, { count: 0 }); if (url.includes('/me')) return jsonResponse(200, { user: current, memberships: [] }); if (url.includes('/messages/conversations')) return jsonResponse(200, { conversations: [] }); if (url.includes('/gyms/invitations')) return jsonResponse(200, { invitations: [] }); if (url.includes('/trainer/invitations')) return jsonResponse(200, { invitations: [] }); if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] }); if (url.includes('/dashboard')) return jsonResponse(200, { killstreak: 0, activePlan: null, nextTraining: null, progress: null, }); return jsonResponse(404, { error: { code: 'NOT_FOUND', message: 'not found' } }); }), ); } async function login(current: User) { mockApi(current); const tester = userEvent.setup(); const queryClient = new QueryClient({ defaultOptions: { queries: { retry: 0, refetchOnWindowFocus: false } }, }); render( , ); 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 tester.type(email, 'jan@test.local'); await tester.type(password, 'Password123!'); await tester.click(screen.getByRole('button', { name: 'Zaloguj' })); expect(await screen.findByRole('heading', { name: 'Dashboard' })).toBeInTheDocument(); } beforeEach(() => { setAccessToken(null); localStorage.clear(); useAuth.setState({ user: null, ready: false }); }); afterEach(() => { vi.unstubAllGlobals(); }); describe('Panel admina w aplikacji', () => { it('administrator widzi link „Panel admina” w stopce sidebaru', async () => { await login(user({ isAdmin: true })); const link = screen.getByRole('link', { name: 'Panel admina' }); expect(link).toBeInTheDocument(); expect(link.getAttribute('href')).toBe(ADMIN_URL); }); it('ikony w railu (przyciemniony sidebar) są tej samej wielkości', async () => { await login(user({ isAdmin: true })); // rail = zwinięty sidebar; jedyna instancja ThemeToggle to ta w stopce raila const toggle = screen.getByRole('button', { name: 'Przełącz motyw' }); const logout = screen.getByRole('button', { name: 'Wyloguj' }); const ts = toggle.getBoundingClientRect(); const ls = logout.getBoundingClientRect(); expect(Math.round(ts.width)).toBe(Math.round(ls.width)); expect(Math.round(ts.height)).toBe(Math.round(ls.height)); const tsvg = toggle.querySelector('svg')!.getBoundingClientRect(); const lsvg = logout.querySelector('svg')!.getBoundingClientRect(); expect(Math.round(tsvg.width)).toBe(Math.round(lsvg.width)); expect(Math.round(tsvg.height)).toBe(Math.round(lsvg.height)); }); it('zwykły użytkownik nie widzi linku „Panel admina”', async () => { await login(user({ isAdmin: false })); expect(screen.queryByRole('link', { name: 'Panel admina' })).not.toBeInTheDocument(); }); });