[英]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.