/
voting
/
frontend
Обзор
Документация
Войти
/
voting
/
frontend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
main
test/admin.query.test.tsx
139 строк
4 KB
TimPulin
033-admin-react-query-hooks
18 июл 2026, 23:48
18 июл 2026, 23:48
f10dd2d
Код
Авторство
О чём код?
import { QueryClientProvider } from '@tanstack/react-query' import { renderHook, waitFor } from '@testing-library/react' import type { ReactNode } from 'react' import { beforeEach, describe, expect, it, vi } from 'vitest' import { createQueryClient } from '../src/lib/reactQuery' vi.mock('../src/api/admin.service', () => ({ adminService: { listNominations: vi.fn(), getNomination: vi.fn(), listNominees: vi.fn(), getNominee: vi.fn(), listEvents: vi.fn(), getEvent: vi.fn(), getCurrentNomination: vi.fn() } })) import { adminService } from '../src/api/admin.service' import { useEventsList, useNominationDetail, useNominationsList } from '../src/api/admin.query' function createWrapper() { const client = createQueryClient() client.setDefaultOptions({ queries: { retry: false }, mutations: { retry: false } }) function Wrapper({ children }: { children: ReactNode }) { return <QueryClientProvider client={client}>{children}</QueryClientProvider> } return { client, Wrapper } } describe('admin query hooks', () => { beforeEach(() => { vi.clearAllMocks() }) it('useEventsList calls listEvents and returns data', async () => { const events = [ { id: 'evt-1', name: 'Event', status: 'DRAFT', currentScreenType: 'WAITING', currentNominationId: null, logoFileId: null, createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z' } ] vi.mocked(adminService.listEvents).mockResolvedValue(events) const { Wrapper } = createWrapper() const { result } = renderHook(() => useEventsList(), { wrapper: Wrapper }) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(adminService.listEvents).toHaveBeenCalledTimes(1) expect(result.current.data).toEqual(events) }) it('useNominationDetail calls getNomination and returns data', async () => { const nomination = { id: 'nom-1', eventId: 'evt-1', name: 'Best', position: 1, status: 'DRAFT', winnerNomineeId: null, openedAt: null, closedAt: null, createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z' } vi.mocked(adminService.getNomination).mockResolvedValue(nomination) const { Wrapper } = createWrapper() const { result } = renderHook(() => useNominationDetail('nom-1'), { wrapper: Wrapper }) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(adminService.getNomination).toHaveBeenCalledWith('nom-1') expect(result.current.data).toEqual(nomination) }) it('useNominationsList passes eventId filter to listNominations', async () => { vi.mocked(adminService.listNominations).mockResolvedValue([]) const { Wrapper } = createWrapper() const { result } = renderHook(() => useNominationsList('evt-1'), { wrapper: Wrapper }) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(adminService.listNominations).toHaveBeenCalledWith('evt-1') }) it('useNominationsList without filter calls listNominations without args', async () => { vi.mocked(adminService.listNominations).mockResolvedValue([]) const { Wrapper } = createWrapper() const { result } = renderHook(() => useNominationsList(), { wrapper: Wrapper }) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(adminService.listNominations).toHaveBeenCalledWith(undefined) }) it('useNominationDetail with empty id does not call the service', async () => { const { Wrapper } = createWrapper() const { result } = renderHook(() => useNominationDetail(''), { wrapper: Wrapper }) expect(result.current.fetchStatus).toBe('idle') expect(adminService.getNomination).not.toHaveBeenCalled() }) it('useNominationDetail surfaces service errors', async () => { const error = new Error('not found') vi.mocked(adminService.getNomination).mockRejectedValue(error) const { Wrapper } = createWrapper() const { result } = renderHook(() => useNominationDetail('nom-1'), { wrapper: Wrapper }) await waitFor(() => expect(result.current.isError).toBe(true)) expect(result.current.error).toBe(error) }) })