Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Nov 8, 2024
1 parent bfb6f96 commit 5a5f239
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/hooks/useAddresses/useAddresses.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { renderHook } from '@testing-library/react-hooks';
import {
QueryClient,
QueryClientProvider,
} from 'react-query';

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

import { getAddresses } from '../../utils';
import { useAddresses } from './useAddresses';

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

const queryClient = new QueryClient();
const wrapper = ({ children }) => (
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
);

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

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

useOkapiKy.mockReturnValue({
get: mockGet,
});
});

it('should return addresses and totalRecords', async () => {
const configs = [{ value: 'address1' }, { value: 'address2' }];
const totalRecords = 2;

mockGet.mockReturnValue({
json: jest.fn().mockResolvedValue({ configs, totalRecords }),
});

getAddresses.mockReturnValue(['address1', 'address2']);

const { result, waitFor } = renderHook(() => useAddresses(), { wrapper });

await waitFor(() => !result.current.isLoading);

expect(result.current.addresses).toEqual(['address1', 'address2']);
expect(result.current.totalRecords).toBe(2);
});
});

0 comments on commit 5a5f239

Please sign in to comment.