[英]Is there a way to spy/mock using Jest on fat arrow class instance methods for React components?
I have a handleClick
function:我有一个
handleClick
函数:
handleClick = tagIndex => {
console.log(tagIndex)
if (tagIndex >= 0 && tagIndex < this.state.suggestedTags.length)
this.state.suggestedTags.splice(tagIndex, 1);
}
inside of my React class and I'm not able to mock or spy on it properly.在我的 React 类中,我无法正确地模拟或监视它。
I'm currently trying this我目前正在尝试这个
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()
})
but all I get back is Expected mock function to have been called
但我得到的只是
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()
https://github.com/airbnb/enzyme/issues/365 https://github.com/airbnb/enzyme/issues/365
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.