[英]Is there a way to spy/mock using Jest on fat arrow class instance methods for React components?
我有一个handleClick
函数:
handleClick = tagIndex => {
console.log(tagIndex)
if (tagIndex >= 0 && tagIndex < this.state.suggestedTags.length)
this.state.suggestedTags.splice(tagIndex, 1);
}
在我的 React 类中,我无法正确地模拟或监视它。
我目前正在尝试这个
it('should call handleClick when a tag is clicked', () => {
wrapper.setState({ suggestedTags: ['Mozart'] })
const spy = jest.spyOn(component, 'handleClick')
component.forceUpdate()
wrapper.find('.suggested-tags__item').simulate('click')
expect(spy).toHaveBeenCalled()
})
但我得到的只是Expected mock function to have been called
const spy = jest.spyOn(component, 'handleClick')
wrapper.instance().forceUpdate(); // Here is the difference
wrapper.find('.suggested-tags__item').simulate('click')
expect(spy).toHaveBeenCalled()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.