我有如下功能,当我要测试该功能时,出现上述错误。
功能
toggleRecovery = e => {
e.preventDefault()
this.setState(
{
recovery: !this.state.recovery
},
() => {
this.props.reset()
}
)
}
测试
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()
})
错误
TypeError: Cannot read property 'preventDefault' of undefined
我如何克服此错误,是什么原因导致上述错误
最佳答案
在实例上调用toggleRecovery
时,您可以像下面那样传递preventDefault
模拟
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()
})