我想断言在我的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 /酶。