import { render, screen } from '@testing-library/react'; 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 UserMemberships from '../src/pages/users/UserMemberships'; import { setAccessToken } from '../src/api/client'; import { useTopbarStore } from '../src/topbar/store'; import type { Gym, User } from '../src/api/types'; const G1: Gym = { id: 'g1', name: 'Gym A', location: 'Warszawa', availability: null, capacity: 50, equipment: [], _count: { memberships: 1 }, }; const U1: User = { id: 'u1', email: 'jan@test.local', firstName: 'Jan', lastName: 'Kowalski', nickname: null, socialMedia: null, age: 30, heightCm: 180, phone: null, isAdmin: false, availability: null, priorities: null, exclusions: null, developMuscles: [], canDoMuscles: [], difficultMuscles: [], avoidMuscles: [], status: 'ACTIVE', suspensionUntil: null, restrictionReason: null, createdAt: '2026-08-01T10:00:00.000Z', memberships: [{ gymId: 'g1', roles: ['OWNER', 'TRAINER'], gym: G1 }], }; function jsonResponse(status: number, body: unknown) { return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' }, }); } function mockFetchSequence(...responses: Response[]) { const fetchMock = vi.fn(async (url: string) => { const next = responses.shift(); if (!next) throw new Error(`No more mocked responses for url: ${url}`); return next; }); vi.stubGlobal('fetch', fetchMock); } function renderMemberships(path: string) { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); return render( } /> , ); } beforeEach(() => { setAccessToken('admin-tok'); }); afterEach(() => { vi.unstubAllGlobals(); }); describe('admin UserMemberships page (tylko do odczytu)', () => { it('lists memberships with role badges and no editing controls', async () => { mockFetchSequence(jsonResponse(200, { users: [U1] })); renderMemberships('/users/u1/memberships'); expect( await screen.findByRole('heading', { name: 'Członkostwa w siłowniach' }), ).toBeInTheDocument(); expect(screen.getByText('Gym A')).toBeInTheDocument(); expect(screen.getByText('Warszawa')).toBeInTheDocument(); expect(screen.getByText('Właściciel')).toBeInTheDocument(); expect(screen.getByText('Trener')).toBeInTheDocument(); // read-only: brak kontrolek edycji (role są badge'ami, nie checkboxami) expect(screen.queryAllByRole('checkbox')).toHaveLength(0); expect(screen.queryByRole('button', { name: 'Usuń' })).not.toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'Dodaj członkostwo' })).not.toBeInTheDocument(); // i w konfiguracji topbara żadnej akcji expect(useTopbarStore.getState().config?.actions ?? []).toHaveLength(0); }); it('shows an empty state when the user has no memberships', async () => { mockFetchSequence(jsonResponse(200, { users: [{ ...U1, memberships: [] }] })); renderMemberships('/users/u1/memberships'); expect( await screen.findByRole('heading', { name: 'Członkostwa w siłowniach' }), ).toBeInTheDocument(); expect(screen.getByText('Brak członkostw.')).toBeInTheDocument(); }); });