I am trying to implement tests for my hook, which depending on the type of key, changes its value.
Of course, hook works, but tests aren't..
I cannot dispatchEvent
on window object in jasmine!
I get this error:
My hook's code ( of course I simplified it for this question ):
export function useHook(initialState = false): boolean {
const [a, setA] = useState(initialState)
useEffect(() => {
const fn = () => {
setA(!a)
}
window.addEventListener('keydown', fn)
return () => window.removeEventListener('keydown', fn)
}, [a])
return a
}
and Tests code:
import { renderHook } from '@testing-library/react-hooks'
describe('Describe', () => {
it(`It`, () => {
const { result } = renderHook(() => Keyboard.useHook())
window.dispatchEvent(new KeyboardEvent('keydown'))
expect(result.current).toBe(false)
})
})
Should I mock window object somehow? Maybe, there is better solution for dispatchEvent ( especially keydown
)?
Thanks for any help.. :)
Aucun commentaire:
Enregistrer un commentaire