【发布时间】:2020-09-14 15:23:03
【问题描述】:
我正在学习从 componentDidMount() 方法中开玩笑测试反应 js 回流动作调用
jsx 像这样导入 store 调用
import MyStore from '~/stores/myStore'
componentDidMount() {
MyStore.getData()
}
在我的 myStore.test.jsx 代码中
import React from 'react'
import renderer from 'react-test-renderer'
jest.dontMock('./myStore.jsx')
const MyStore = require('./myStore.jsx').default
describe('', () => {
it(''), () => {
const spy = jest.spyOn(MyStore.prototype, 'MyStore.getData')
render.create(<MyStore/>).getInstance()
expect(spy).toHaveBeenCalled()
}
}
我在关注另一个类似的帖子
https://stackoverflow.com/questions/43245040/using-jest-to-spy-on-method-call-in-componentdidmount
但我收到以下开玩笑测试错误
Cannot spy the MyStore.getData() property because it is not a function
我无法找到涉及致电商店的回流操作测试。
有解决办法吗?有没有更好的方法来测试 componentDidMount 中的调用?
谢谢
【问题讨论】: