diff --git a/lib/hooks/useAddresses/useAddresses.test.js b/lib/hooks/useAddresses/useAddresses.test.js new file mode 100644 index 00000000..fb3f673e --- /dev/null +++ b/lib/hooks/useAddresses/useAddresses.test.js @@ -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 }) => ( + + {children} + +); + +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); + }); +});