TypeError:无法读取null的属性'componentDidMount'

时间:2018-10-25 12:17:41

标签: reactjs jestjs enzyme

我想断言在我的componentDidMount生命周期中使用正确的props调用方法。

  componentDidMount () {
    const { fetchContentForUser } = this.props
    fetchContentForUser({ userId: '6ae42468-83f2-0bf39a917713' })
  }

我的测试看起来像这样

let props
let wrapper
      beforeEach(() => {
        props = {
          fetchContentForUser: jest.fn(),
          loading: false
        }
      })

          it('should call fetchContentForUser on mount', () => {
            wrapper = shallow(<LandingComponent {...props} />)
            wrapper.update()
            wrapper.instance().componentDidMount()

            expect(props.fetchContentForUser).toHaveBeenCalled()
          })

但是我遇到错误

TypeError: Cannot read property 'componentDidMount' of null

如何断言此方法被调用?我正在使用Jest /酶。

0 个答案:

没有答案