93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { MemoryRouter, Route, Routes, useParams } from 'react-router-dom';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import UserForm from '../src/pages/users/UserForm';
|
|
import { setAccessToken } from '../src/api/client';
|
|
import { API_BASE } from '../src/config';
|
|
import type { User } from '../src/api/types';
|
|
|
|
const U3: User = {
|
|
id: 'u3',
|
|
email: 'nowy3@test.local',
|
|
firstName: 'Nowy',
|
|
lastName: null,
|
|
age: null,
|
|
heightCm: null,
|
|
phone: null,
|
|
isAdmin: false,
|
|
availability: null,
|
|
priorities: null,
|
|
exclusions: null,
|
|
developMuscles: [],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: [],
|
|
createdAt: '2026-08-03T10:00:00.000Z',
|
|
memberships: [],
|
|
};
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
function mockFetchSequence(...responses: Response[]) {
|
|
const calls: { url: string; init?: RequestInit }[] = [];
|
|
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
|
calls.push({ url, init });
|
|
const next = responses.shift();
|
|
if (!next) throw new Error(`No more mocked responses for url: ${url}`);
|
|
return next;
|
|
});
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
return calls;
|
|
}
|
|
|
|
function DetailStub() {
|
|
const { id } = useParams();
|
|
return <div>DETAIL-{id}</div>;
|
|
}
|
|
|
|
function renderForm() {
|
|
return render(
|
|
<MemoryRouter initialEntries={['/users/new']}>
|
|
<Routes>
|
|
<Route path="/users/new" element={<UserForm />} />
|
|
<Route path="/users/:id" element={<DetailStub />} />
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setAccessToken('admin-tok');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin UserForm page', () => {
|
|
it('creates a user and opens the detail page', async () => {
|
|
const calls = mockFetchSequence(jsonResponse(201, { user: U3 }));
|
|
const user = userEvent.setup();
|
|
const { container } = renderForm();
|
|
const email = container.querySelector('input[type="email"]') as HTMLInputElement;
|
|
const password = container.querySelector('input[type="password"]') as HTMLInputElement;
|
|
await user.type(email, 'nowy3@test.local');
|
|
await user.type(password, 'Password123!');
|
|
await user.click(screen.getByRole('button', { name: 'Utwórz użytkownika' }));
|
|
expect(await screen.findByText('DETAIL-u3')).toBeInTheDocument();
|
|
const post = calls.find((c) => c.init?.method === 'POST');
|
|
expect(post?.url).toBe(`${API_BASE}/admin/users`);
|
|
expect(JSON.parse(post?.init?.body as string)).toMatchObject({
|
|
email: 'nowy3@test.local',
|
|
password: 'Password123!',
|
|
isAdmin: false,
|
|
});
|
|
});
|
|
});
|