-
Notifications
You must be signed in to change notification settings - Fork 179
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
Showing
5 changed files
with
99 additions
and
4 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
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,51 @@ | ||
// @flow | ||
import * as React from 'react' | ||
import { mount } from 'enzyme' | ||
import { useTimeout } from '..' | ||
|
||
describe('useTimeouthook', () => { | ||
const callback = jest.fn() | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
jest.clearAllTimers() | ||
jest.useRealTimers() | ||
}) | ||
|
||
const TestUseTimeout = (props: { delay: number | null }) => { | ||
useTimeout(callback, props.delay) | ||
return <span /> | ||
} | ||
|
||
test('delay `null` results in no calls', () => { | ||
mount(<TestUseTimeout delay={null} />) | ||
jest.runTimersToTime(10000) | ||
|
||
expect(callback).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
test('delay sets a timeout', () => { | ||
mount(<TestUseTimeout delay={2} />) | ||
jest.runTimersToTime(3) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('re-rendering with delay={null} clears the interval', () => { | ||
const wrapper = mount(<TestUseTimeout delay={4} />) | ||
|
||
jest.runTimersToTime(2) | ||
wrapper.setProps({ delay: null }) | ||
|
||
expect(callback).toHaveBeenCalledTimes(0) | ||
|
||
wrapper.setProps({ delay: 4 }) | ||
jest.runTimersToTime(6) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
|
||
export * from './usePrevious' | ||
export * from './useInterval' | ||
export * from './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,30 @@ | ||
// @flow | ||
import { useEffect, useRef } from 'react' | ||
|
||
/** | ||
* React hook to call a function on an interval; copied from: | ||
* https://overreacted.io/making-setinterval-declarative-with-react-hooks/ | ||
* | ||
* @template T (type of the input value) | ||
* @param {() => mixed} callback (function to call after timeout) | ||
* @param {number | null} delay (timeout delay, or null to disable the timeout) | ||
* @returns {void} | ||
*/ | ||
export function useTimeout(callback: () => mixed, delay: number | null): void { | ||
const savedCallback = useRef() | ||
|
||
// remember the latest callback | ||
useEffect(() => { | ||
savedCallback.current = callback | ||
}, [callback]) | ||
|
||
// set up the interval | ||
useEffect(() => { | ||
const currentCallback = () => | ||
savedCallback.current && savedCallback.current() | ||
if (delay !== null) { | ||
const id = setTimeout(currentCallback, delay) | ||
return () => clearTimeout(id) | ||
} | ||
}, [delay]) | ||
} |