-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
be85077
commit de40f9e
Showing
3 changed files
with
114 additions
and
18 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,74 @@ | ||
import { createElement, render, createRef } from 'preact'; | ||
import { | ||
useState, | ||
useEffect, | ||
useLayoutEffect, | ||
useCallback, | ||
useMemo, | ||
useImperativeHandle | ||
} from 'preact/hooks'; | ||
import { setupRerender } from 'preact/test-utils'; | ||
import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
import 'preact/debug'; | ||
|
||
/** @jsx createElement */ | ||
|
||
describe('Hook argument validation', () => { | ||
/** | ||
* @param {string} name | ||
* @param {(arg: number) => void} hook | ||
*/ | ||
function validateHook(name, hook) { | ||
const TestComponent = ({ initialValue }) => { | ||
const [value, setValue] = useState(initialValue); | ||
hook(value); | ||
|
||
return ( | ||
<button type="button" onClick={() => setValue(NaN)}> | ||
Set to NaN | ||
</button> | ||
); | ||
}; | ||
|
||
it(`should error if ${name} is mounted with NaN as an argument`, async () => { | ||
expect(() => | ||
render(<TestComponent initialValue={NaN} />, scratch) | ||
).to.throw(/Hooks should not be called with NaN in the dependency array/); | ||
}); | ||
|
||
it(`should error if ${name} is updated with NaN as an argument`, async () => { | ||
render(<TestComponent initialValue={0} />, scratch); | ||
|
||
expect(() => { | ||
scratch.querySelector('button').click(); | ||
rerender(); | ||
}).to.throw( | ||
/Hooks should not be called with NaN in the dependency array/ | ||
); | ||
}); | ||
} | ||
|
||
/** @type {HTMLElement} */ | ||
let scratch; | ||
/** @type {() => void} */ | ||
let rerender; | ||
|
||
beforeEach(() => { | ||
scratch = setupScratch(); | ||
rerender = setupRerender(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardown(scratch); | ||
}); | ||
|
||
validateHook('useEffect', arg => useEffect(() => {}, [arg])); | ||
validateHook('useLayoutEffect', arg => useLayoutEffect(() => {}, [arg])); | ||
validateHook('useCallback', arg => useCallback(() => {}, [arg])); | ||
validateHook('useMemo', arg => useMemo(() => {}, [arg])); | ||
|
||
const ref = createRef(); | ||
validateHook('useImperativeHandle', arg => { | ||
useImperativeHandle(ref, () => undefined, [arg]); | ||
}); | ||
}); |
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