React JS Jest Unit Testing SpyOn ComponentDidMount action ca



我正在学习开玩笑测试来自componentDidMount((方法的反应js反流操作调用

JSX 像这样导入存储调用

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

我无法找到涉及致电商店的反流操作测试。

有修复吗?有没有更好的方法来测试组件DidMount中的调用?

谢谢

假设getDataprototype上的一种方法,你应该使用:

const spy = jest.spyOn(MyStore.prototype, 'getData')

如果您想监视prototype上的方法.

相关内容

  • 没有找到相关文章

最新更新