-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useTimeout): discovery-154 setTimeout hook (#145)
- Loading branch information
Showing
5 changed files
with
60 additions
and
0 deletions.
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,8 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`useTimeout should apply a hook for useTimeout: timeout 1`] = ` | ||
Object { | ||
"cancel": [Function], | ||
"update": undefined, | ||
} | ||
`; |
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,17 @@ | ||
import { useTimeout } from '../useTimeout'; | ||
|
||
describe('useTimeout', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
it('should apply a hook for useTimeout', async () => { | ||
const mockCallback = jest.fn(); | ||
const mockSetTimeout = jest.spyOn(global, 'setTimeout'); | ||
const { result } = await mountHook(() => useTimeout(mockCallback)); | ||
|
||
expect(mockSetTimeout).toHaveBeenCalledTimes(2); | ||
expect(mockCallback).toHaveBeenCalledTimes(1); | ||
expect(result).toMatchSnapshot('timeout'); | ||
}); | ||
}); |
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,5 @@ | ||
import { useTimeout } from './useTimeout'; | ||
|
||
const hooks = { useTimeout }; | ||
|
||
export { hooks as default, hooks, useTimeout }; |
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,29 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { helpers } from '../common'; | ||
|
||
/** | ||
* setTimeout hook | ||
* | ||
* @param {Function} callback | ||
* @param {number} pollInterval | ||
* @returns {Function} | ||
*/ | ||
const useTimeout = (callback, pollInterval = 0) => { | ||
const timer = useRef(); | ||
const [update, setUpdate] = useState(); | ||
const result = callback(); | ||
|
||
useEffect(() => { | ||
if (result !== false) { | ||
timer.current = window.setTimeout(() => setUpdate(helpers.getCurrentDate().getTime()), pollInterval); | ||
} | ||
|
||
return () => { | ||
window.clearTimeout(timer.current); | ||
}; | ||
}, [pollInterval, update, result]); | ||
|
||
return { update, cancel: () => window.clearTimeout(timer.current) }; | ||
}; | ||
|
||
export { useTimeout as default, useTimeout }; |