Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 1m55s
104 lines
3.7 KiB
TypeScript
104 lines
3.7 KiB
TypeScript
// src/hooks/queries/useSuggestedCorrectionsQuery.test.tsx
|
|
import { renderHook, waitFor } from '@testing-library/react';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
import type { ReactNode } from 'react';
|
|
import { useSuggestedCorrectionsQuery } from './useSuggestedCorrectionsQuery';
|
|
import * as apiClient from '../../services/apiClient';
|
|
|
|
vi.mock('../../services/apiClient');
|
|
|
|
const mockedApiClient = vi.mocked(apiClient);
|
|
|
|
describe('useSuggestedCorrectionsQuery', () => {
|
|
let queryClient: QueryClient;
|
|
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
);
|
|
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
queryClient = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should fetch suggested corrections successfully', async () => {
|
|
const mockCorrections = [
|
|
{ correction_id: 1, item_name: 'Milk', suggested_name: 'Whole Milk', status: 'pending' },
|
|
{ correction_id: 2, item_name: 'Bread', suggested_name: 'White Bread', status: 'pending' },
|
|
];
|
|
// API returns wrapped response: { success: true, data: [...] }
|
|
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({ success: true, data: mockCorrections }),
|
|
} as Response);
|
|
|
|
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
|
|
|
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
|
|
expect(mockedApiClient.getSuggestedCorrections).toHaveBeenCalled();
|
|
expect(result.current.data).toEqual(mockCorrections);
|
|
});
|
|
|
|
it('should handle API error with error message', async () => {
|
|
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
|
ok: false,
|
|
status: 403,
|
|
json: () => Promise.resolve({ message: 'Admin access required' }),
|
|
} as Response);
|
|
|
|
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
|
|
|
await waitFor(() => expect(result.current.isError).toBe(true));
|
|
|
|
expect(result.current.error?.message).toBe('Admin access required');
|
|
});
|
|
|
|
it('should handle API error without message', async () => {
|
|
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
json: () => Promise.reject(new Error('Parse error')),
|
|
} as Response);
|
|
|
|
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
|
|
|
await waitFor(() => expect(result.current.isError).toBe(true));
|
|
|
|
expect(result.current.error?.message).toBe('Request failed with status 500');
|
|
});
|
|
|
|
it('should use fallback message when error.message is empty', async () => {
|
|
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
json: () => Promise.resolve({ message: '' }),
|
|
} as Response);
|
|
|
|
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
|
|
|
await waitFor(() => expect(result.current.isError).toBe(true));
|
|
|
|
expect(result.current.error?.message).toBe('Failed to fetch suggested corrections');
|
|
});
|
|
|
|
it('should return empty array for no corrections', async () => {
|
|
// API returns wrapped response: { success: true, data: [] }
|
|
mockedApiClient.getSuggestedCorrections.mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({ success: true, data: [] }),
|
|
} as Response);
|
|
|
|
const { result } = renderHook(() => useSuggestedCorrectionsQuery(), { wrapper });
|
|
|
|
await waitFor(() => expect(result.current.isSuccess).toBe(true));
|
|
|
|
expect(result.current.data).toEqual([]);
|
|
});
|
|
});
|