130 lines
4.0 KiB
TypeScript
130 lines
4.0 KiB
TypeScript
import { render, screen, waitFor } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { UserRestrictionModal } from '../src/pages/users/UserRestrictionModal';
|
|
import { setAccessToken } from '../src/api/client';
|
|
import type { User } from '../src/api/types';
|
|
|
|
const BASE_USER: User = {
|
|
id: 'u1',
|
|
email: 'jan@test.local',
|
|
firstName: 'Jan',
|
|
lastName: 'Kowalski',
|
|
nickname: null,
|
|
socialMedia: null,
|
|
birthDate: null,
|
|
heightCm: null,
|
|
phone: null,
|
|
isAdmin: false,
|
|
availability: null,
|
|
priorities: null,
|
|
exclusions: null,
|
|
developMuscles: [],
|
|
canDoMuscles: [],
|
|
difficultMuscles: [],
|
|
avoidMuscles: [],
|
|
createdAt: '2026-01-01T00:00:00.000Z',
|
|
status: 'ACTIVE',
|
|
suspensionUntil: null,
|
|
restrictionReason: null,
|
|
};
|
|
|
|
function renderModal(overrides: Partial<User>, initialStatus: User['status'], onClose = vi.fn()) {
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
render(
|
|
<QueryClientProvider client={queryClient}>
|
|
<UserRestrictionModal
|
|
user={{ ...BASE_USER, ...overrides }}
|
|
initialStatus={initialStatus}
|
|
onClose={onClose}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
return onClose;
|
|
}
|
|
|
|
function mockPatch() {
|
|
const calls: { url: string; init?: RequestInit }[] = [];
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async (url: string, init?: RequestInit) => {
|
|
calls.push({ url, init });
|
|
return new Response(JSON.stringify({ user: BASE_USER }), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}),
|
|
);
|
|
return calls;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setAccessToken('admin-tok');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe('admin UserRestrictionModal (kary z menu)', () => {
|
|
it('blokuje ban bez powodu — zero żądań PATCH', async () => {
|
|
const calls = mockPatch();
|
|
const onClose = renderModal({}, 'BANNED');
|
|
const user = userEvent.setup();
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
|
|
|
expect(await screen.findByText('Podaj powód blokady konta.')).toBeInTheDocument();
|
|
expect(calls).toHaveLength(0);
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('blokuje zawieszenie bez terminu', async () => {
|
|
const calls = mockPatch();
|
|
renderModal({}, 'SUSPENDED');
|
|
const user = userEvent.setup();
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
|
|
|
expect(
|
|
await screen.findByText('Podaj datę, do kiedy konto ma być zawieszone.'),
|
|
).toBeInTheDocument();
|
|
expect(calls).toHaveLength(0);
|
|
});
|
|
|
|
it('ban z powodem PATCHuje status i zamyka modal', async () => {
|
|
const calls = mockPatch();
|
|
const onClose = renderModal({}, 'BANNED');
|
|
const user = userEvent.setup();
|
|
|
|
await user.type(screen.getByRole('textbox', { name: 'Powód kary' }), 'Sprzedaż dostępu');
|
|
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
const patch = calls.find((c) => c.init?.method === 'PATCH');
|
|
expect(JSON.parse(patch?.init?.body as string)).toMatchObject({
|
|
status: 'BANNED',
|
|
restrictionReason: 'Sprzedaż dostępu',
|
|
suspensionUntil: null,
|
|
});
|
|
});
|
|
|
|
it('cofnięcie bana wysyła status ACTIVE i zeruje karę', async () => {
|
|
const calls = mockPatch();
|
|
const onClose = renderModal({ status: 'BANNED', restrictionReason: 'Stary powód' }, 'BANNED');
|
|
const user = userEvent.setup();
|
|
|
|
await user.selectOptions(screen.getByRole('combobox', { name: 'Status konta' }), 'ACTIVE');
|
|
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
const patch = calls.find((c) => c.init?.method === 'PATCH');
|
|
expect(JSON.parse(patch?.init?.body as string)).toMatchObject({
|
|
status: 'ACTIVE',
|
|
suspensionUntil: null,
|
|
restrictionReason: null,
|
|
});
|
|
});
|
|
});
|