Skip to content

Commit

Permalink
add simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Oct 25, 2024
1 parent 5510d94 commit 2620c5e
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/hooks/api/useBulkOperationDetails.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { useQuery } from 'react-query';
import { useNamespace, useOkapiKy } from '@folio/stripes/core';
import { useHistory } from 'react-router-dom';
import { useBulkOperationDetails, BULK_OPERATION_DETAILS_KEY } from './useBulkOperationDetails';
import { useErrorMessages } from '../useErrorMessages';


jest.mock('@folio/stripes-acq-components', () => ({
buildSearch: jest.fn(),
}));

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

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

jest.mock('react-router-dom', () => ({
useHistory: jest.fn(),
}));

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

describe('useBulkOperationDetails', () => {
let kyMock;
let historyMock;
let showErrorMessageMock;

beforeEach(() => {
kyMock = { get: jest.fn() };
historyMock = { replace: jest.fn(), location: { search: '' } };
showErrorMessageMock = jest.fn();

useOkapiKy.mockReturnValue(kyMock);
useNamespace.mockReturnValue(['namespace-key']);
useHistory.mockReturnValue(historyMock);
useErrorMessages.mockReturnValue({ showErrorMessage: showErrorMessageMock });

// Set a default implementation for useQuery to prevent infinite renders
useQuery.mockReturnValue({ data: null, isLoading: false });
jest.clearAllMocks();
});

it('should initialize with the correct refetch interval and query key', () => {
const id = 'test-id';
const refetchInterval = 5000;

const { result } = renderHook(() => useBulkOperationDetails({ id, interval: refetchInterval }));

expect(result.current.isLoading).toBe(false);
expect(useQuery).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: [BULK_OPERATION_DETAILS_KEY, 'namespace-key', id, refetchInterval],
refetchInterval,
enabled: true,
})
);
});

it('should clear interval and redirect when clearIntervalAndRedirect is called', () => {
const id = 'test-id';
const pathname = '/new-path';
const searchParams = { query: 'test' };

const { result } = renderHook(() => useBulkOperationDetails({ id }));

act(() => {
result.current.clearIntervalAndRedirect(pathname, searchParams);
});

expect(historyMock.replace).toHaveBeenCalledWith({
pathname,
search: undefined,
});
});
});

0 comments on commit 2620c5e

Please sign in to comment.