I have a function as bellow and when i going to test the function i got the above error.
function
toggleRecovery = e => {
e.preventDefault()
this.setState(
{
recovery: !this.state.recovery
},
() => {
this.props.reset()
}
)
}
test
test('if the recovery state is true should set the state to the false', () => {
wrapper.setState({ recovery: true })
//Check if the state is true initially
expect(wrapper.state().recovery).toBeTruthy()
//Calling the toggleRecovery()
wrapper.instance().toggleRecovery()
expect(wrapper.state().recovery).toBeFalsy()
expect(props.reset).toHaveBeenCalled()
})
error
TypeError: Cannot read property 'preventDefault' of undefined
How can i overcome this error and what is cause the above error
While calling toggleRecovery
on instance you can pass the preventDefault
mock like
test('if the recovery state is true should set the state to the false', () => {
wrapper.setState({ recovery: true })
//Check if the state is true initially
expect(wrapper.state().recovery).toBeTruthy()
//Calling the toggleRecovery()
wrapper.instance().toggleRecovery({
preventDefault: () => {
})
expect(wrapper.state().recovery).toBeFalsy()
expect(props.reset).toHaveBeenCalled()
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.