我知道this解决方案,但不幸的是instance()
仅适用于类,不适用于功能组件。
如果我在功能组件内部有子组件,如何检查功能组件的方法已传递给子组件?
例如,如果这是我的组件设置:
const ParentComponent = () => {
const sampleFunction = () => {
// whatever
};
return (
<ChildComponent sampleProp={sampleFunction} />
);
}
我的测试看起来像这样:
it('should pass the correct prop to the child component', () => {
expect(wrapper.find(ChildComponent).props().sampleProp).toBe(wrapper.sampleFunction);
});
由于未定义wrapper.sampleFunction
,因此会引发错误。由于我无法使用instance()
,如何检查道具是否等于方法?
我认为,如果测试这种情况,则需要测试是否调用了该函数。jest-spyOn