尚未使用Jest调用反应属性方法的测试 [英] Test that React prop method has not been called with Jest
本文介绍了尚未使用Jest调用反应属性方法的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何测试有条件调用的属性(回调)函数 我有一个组件,它将一个属性函数传递给它的子级,条件如下:
if(this.props.myFun) {
this.props.myFun();
}
为了测试这一点,我这里有两个案例:
1.将道具传递给组件的位置
<ChildComp myFun={value => value } /> and I can test it from the child-
const comp = mountWithIntl(<ChildComp myFun={value => value } />);
expect(comp.instance().props.myFun).toHaveBeenCalled();
- 道具未通过的地方:我正在尝试
const comp = mountWithIntl(<MyComp />);
expect(comp.instance().props.myFun).not.toHaveBeenCalled();
但是,由于如果我在安装组件时模拟道具,
方法将被调用。但我如何测试未定义或未被嘲笑的
道具?
如果我没有通过道具而这样做:
expect(comp.instance().props.myFun).not.toHaveBeenCalled();
我将获得:
jest.fn()值必须是模拟函数或间谍
根据我的代码,我不能这样做 请帮帮忙
推荐答案
我认为您无法测试未传递的函数是否未被调用。您可以做些什么来测试它是否在没有该函数的情况下被渲染。这应该足够了
这篇关于尚未使用Jest调用反应属性方法的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文