-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e6a374
commit 3bfb85c
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
QueryClient, | ||
QueryClientProvider, | ||
} from 'react-query'; | ||
|
||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { useDebouncedQuery } from './useDebouncedQuery'; | ||
|
||
const DELAY = 300; | ||
|
||
jest.useFakeTimers('modern'); | ||
const mockDataFormatter = jest.fn(({ poLines }) => { | ||
return poLines.map(({ id, poLineNumber }) => ({ label: poLineNumber, value: id })); | ||
}); | ||
|
||
const queryClient = new QueryClient(); | ||
const wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
|
||
describe('useDebouncedQuery', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
useOkapiKy.mockReturnValue({ | ||
get: jest.fn(() => ({ | ||
json: () => Promise.resolve({ poLines: [{ id: 'poLine-1', poLineNumber: '11111' }] }), | ||
})), | ||
}); | ||
}); | ||
|
||
it('should not call `dataFormatter` and return empty []', async () => { | ||
const { result } = renderHook(() => useDebouncedQuery({ | ||
api: 'api', | ||
queryBuilder: jest.fn(), | ||
dataFormatter: mockDataFormatter, | ||
debounceDelay: DELAY, | ||
}), { wrapper }); | ||
|
||
await act(async () => { | ||
await result.current.setInputValue(''); | ||
jest.advanceTimersByTime(1500); | ||
}); | ||
|
||
expect(mockDataFormatter).toHaveBeenCalledTimes(0); | ||
expect(result.current.options).toEqual([]); | ||
}); | ||
|
||
it('should call `dataFormatter` and return options', async () => { | ||
const { result } = renderHook(() => useDebouncedQuery({ | ||
api: 'api', | ||
queryBuilder: jest.fn(), | ||
dataFormatter: mockDataFormatter, | ||
}), { wrapper }); | ||
|
||
await act(async () => { | ||
await result.current.setInputValue('test'); | ||
jest.advanceTimersByTime(1500); | ||
}); | ||
|
||
expect(mockDataFormatter).toHaveBeenCalledTimes(1); | ||
expect(result.current.options).toEqual([{ label: '11111', value: 'poLine-1' }]); | ||
}); | ||
}); |