reactjs - TypeError:无法读取 null 的属性“componentDidMount”
问题描述
我想断言在我的componentDidMount
生命周期中使用正确的道具调用了一个方法。
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 / Enzyme。
解决方案
推荐阅读
- azure-devops-migration-tools - FieldMergeMap - 将两列合二为一
- javascript - 如何围绕未折叠的文本制作卡片形状?
- javascript - 如何使用此图像上的颜色蒙版简单地替换颜色然后保存?以 RGBA 通道为例
- flutter - 我有一个关于定义在颤动中移动页面的路线的问题
- gnuplot - Octave 一直说 gnuplot 不可用
- bookdown - Bookdown 未合并,导致隐式链接出现问题
- r - 如何使用 R 中的 xyz 值表制作曲面图
- jenkins - 如何使用 HTML 发布者插件在文件夹路径内发布 html 报告?
- css - 如何使用 CSS 平滑滚动来补偿固定标题?
- python - 在 C++ 中嵌入 python 脚本