-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some tests to useSelectWithDebounce
The test is not complete, since we're having an issue to simulate the debounce timers.
- Loading branch information
Showing
2 changed files
with
44 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,44 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useSelectWithDebounce from './use-select-with-debounce'; | ||
|
||
jest.mock( '@wordpress/data' ); | ||
|
||
describe( 'useSelectWithDebounce', () => { | ||
it( 'Should change the deps to call useSelect after timer only', () => { | ||
jest.useFakeTimers(); | ||
const mapSelect = () => {}; | ||
const timer = 1000; | ||
const mockFn = jest.fn(); | ||
useSelect.mockImplementation( mockFn ); | ||
|
||
const TestComponent = ( { deps } ) => { | ||
useSelectWithDebounce( mapSelect, deps, timer ); | ||
return <div />; | ||
}; | ||
|
||
const deps1 = [ 1 ]; | ||
const deps2 = [ 2 ]; | ||
|
||
const { rerender } = render( <TestComponent deps={ deps1 } /> ); | ||
expect( mockFn ).toBeCalledWith( mapSelect, deps1 ); | ||
|
||
rerender( <TestComponent deps={ deps2 } /> ); | ||
expect( mockFn ).toBeCalledWith( mapSelect, deps1 ); | ||
|
||
// TODO: Complete test running debounce to make sure it was called after the time. | ||
// jest.runAllTimers(); | ||
// expect( mockFn ).toBeCalledWith( mapSelect, deps2 ); | ||
} ); | ||
} ); |