开玩笑的错误” TypeError:无法读取未定义的属性'preventDefault'

时间:2018-12-26 05:50:09

标签: javascript reactjs jestjs

我有一个波纹管功能,当我要测试该功能时,出现了以上错误。

功能

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

我该如何克服此错误以及导致上述错误的原因

1 个答案:

答案 0 :(得分:1)

在实例上调用let source = "lat/lng: (-6.2222391,106.7035684)" let allowedCharactersString = "-01234567890.," let latLongValues = String(source.characters.filter { allowedCharactersString.characters.contains($0) }).components(separatedBy: ",") print(latLongValues.first!) // "-6.2222391" print(latLongValues.last!) // "106.7035684" 时,您可以像下面那样传递toggleRecovery模拟

preventDefault