使用酶和玩笑,如何检查方法是否已作为道具传递给功能组件的子代?

问题描述 投票:0回答:1

我知道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(),如何检查道具是否等于方法?

javascript reactjs enzyme react-functional-component
1个回答
0
投票

我认为,如果测试这种情况,则需要测试是否调用了该函数。jest-spyOn

© www.soinside.com 2019 - 2024. All rights reserved.