Skip to content

Commit

Permalink
add tests for useErrorsPreview hook
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Dec 23, 2024
1 parent 38a9ff6 commit b5381fc
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/hooks/api/useErrorsPreview.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { renderHook } from '@testing-library/react-hooks';
import { useQuery } from 'react-query';

import { useNamespace, useOkapiKy } from '@folio/stripes/core';

import { useErrorMessages } from '../useErrorMessages';
import { useErrorsPreview, PREVIEW_ERRORS_KEY } from './useErrorsPreview';
import { PREVIEW_LIMITS } from '../../constants';

jest.mock('react-query', () => ({
useQuery: jest.fn(),
}));

jest.mock('@folio/stripes/core', () => ({
useNamespace: jest.fn(),
useOkapiKy: jest.fn(),
}));

jest.mock('../useErrorMessages', () => ({
useErrorMessages: jest.fn(),
}));

describe('useErrorsPreview', () => {
const mockGet = jest.fn();
const mockShowErrorMessage = jest.fn();

beforeEach(() => {
jest.clearAllMocks();

useOkapiKy.mockReturnValue({ get: mockGet });
useNamespace.mockReturnValue([PREVIEW_ERRORS_KEY]);
useErrorMessages.mockReturnValue({ showErrorMessage: mockShowErrorMessage });
});

it('should return errors and isFetching from useQuery', () => {
const mockData = { errors: ['error1', 'error2'] };

useQuery.mockReturnValue({
data: mockData,
isFetching: true,
});

const { result } = renderHook(() => useErrorsPreview({ id: '123', enabled: true }));

expect(result.current.errors).toEqual(['error1', 'error2']);
expect(result.current.isFetching).toBe(true);

expect(useNamespace).toHaveBeenCalledWith({ key: PREVIEW_ERRORS_KEY });
expect(useQuery).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [PREVIEW_ERRORS_KEY, '123', PREVIEW_LIMITS.ERRORS, 0],
queryFn: expect.any(Function),
enabled: true,
})
);
});

it('should call ky.get with the correct parameters in queryFn', async () => {
mockGet.mockResolvedValue({ json: () => ({ errors: [] }) });
useQuery.mockImplementation(({ queryFn }) => {
queryFn();
return { data: null, isFetching: false };
});

renderHook(() => useErrorsPreview({ id: '123', enabled: true, offset: 10, limit: 20 }));

expect(mockGet).toHaveBeenCalledWith('bulk-operations/123/errors', {
searchParams: { limit: 20, offset: 10 },
});
});

it('should handle onError and onSuccess callbacks', () => {
useQuery.mockImplementation(({ onError, onSuccess }) => {
onError();
onSuccess();
return { data: null, isFetching: false };
});

renderHook(() => useErrorsPreview({ id: '123', enabled: true }));

expect(mockShowErrorMessage).toHaveBeenCalledTimes(2);
});

it('should return an empty array if data.errors is undefined', () => {
useQuery.mockReturnValue({
data: undefined,
isFetching: false,
});

const { result } = renderHook(() => useErrorsPreview({ id: '123', enabled: true }));

expect(result.current.errors).toEqual([]);
expect(result.current.isFetching).toBe(false);
});
});

0 comments on commit b5381fc

Please sign in to comment.