Files
FitOS/admin/test/GymDetail.test.tsx

118 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { render, screen, fireEvent, waitFor } 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 GymDetail from '../src/pages/gyms/GymDetail';
import { setAccessToken } from '../src/api/client';
import { useTopbarStore } from '../src/topbar/store';
import type { Equipment, Gym } from '../src/api/types';
const G1: Gym = {
id: 'g1',
name: 'Gym A',
location: 'Warszawa',
availability: [{ start: '06:00', end: '22:00' }, null, null, null, null, null, null],
capacity: 50,
equipment: [],
_count: { memberships: 12 },
};
const EQ1: Equipment = {
id: 'eq-1',
name: 'sztanga',
kind: 'BARBELL',
spec: { maxLoadKg: 100 },
description: null,
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
};
const EQ2: Equipment = {
id: 'eq-2',
name: 'hantle',
kind: 'DUMBBELL',
spec: { minKg: 2.5, maxKg: 25 },
description: null,
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
};
const G1_EQ: Gym = {
...G1,
equipment: [{ id: 'ge-2', gymId: 'g1', equipmentId: 'eq-2', quantity: 2, equipment: EQ2 }],
};
function jsonResponse(status: number, body: unknown) {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
});
}
function renderGymDetail() {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
return render(
<MemoryRouter initialEntries={['/gyms/g1']}>
<QueryClientProvider client={queryClient}>
<Routes>
<Route path="/gyms/:id" element={<GymDetail />} />
</Routes>
</QueryClientProvider>
</MemoryRouter>,
);
}
beforeEach(() => {
setAccessToken('admin-tok');
});
afterEach(() => {
vi.unstubAllGlobals();
});
describe('admin GymDetail page', () => {
it('shows gym details and the edit form', async () => {
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.includes('/admin/equipment')) return jsonResponse(200, { equipment: [EQ1, EQ2] });
if (url.includes('/exceptions')) return jsonResponse(200, { exceptions: [] });
if (url.includes('/admin/gyms/g1')) return jsonResponse(200, G1);
throw new Error(`Unexpected fetch: ${url}`);
});
vi.stubGlobal('fetch', fetchMock);
renderGymDetail();
expect(await screen.findByRole('heading', { name: 'Gym A' })).toBeInTheDocument();
expect(screen.getByText('Warszawa')).toBeInTheDocument();
expect(screen.getByText('12 członk.')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Zapisz' })).toBeInTheDocument();
// „Usuń siłownię" jest w top menu (menu kontekstowe) — sprawdzamy zarejestrowaną konfigurację
expect(useTopbarStore.getState().config?.actions?.some((a) => a.label === 'Usuń siłownię')).toBe(true);
});
it('adding already-added equipment increases its quantity via PATCH', async () => {
let patchBody: unknown;
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
if (url.includes('/admin/equipment')) return jsonResponse(200, { equipment: [EQ1, EQ2] });
if (url.includes('/exceptions')) return jsonResponse(200, { exceptions: [] });
if (url.includes('/admin/gyms/g1/equipment/eq-2') && init?.method === 'PATCH') {
patchBody = JSON.parse(String(init.body));
return jsonResponse(200, { gym: G1_EQ });
}
if (url.includes('/admin/gyms/g1')) return jsonResponse(200, G1_EQ);
throw new Error(`Unexpected fetch: ${url}`);
});
vi.stubGlobal('fetch', fetchMock);
renderGymDetail();
expect(await screen.findByRole('heading', { name: 'Gym A' })).toBeInTheDocument();
// select pokazuje cały katalog ze specyfikacją — sprzęt już dodany jest oznaczony bieżącą ilością
expect(await screen.findByRole('option', { name: 'hantle · 2.5–25 kg (2 szt.)' })).toBeInTheDocument();
fireEvent.change(screen.getByRole('combobox', { name: 'Dodaj urządzenie' }), {
target: { value: 'eq-2' },
});
fireEvent.click(screen.getByRole('button', { name: 'Dodaj' }));
await waitFor(() => expect(patchBody).toEqual({ quantity: 3 }));
});
});