我想确保 HOC 组件被开玩笑地调用,但我似乎无法理解jest.mock
上班。我的 HOC 是这样的:
const withEntity = (
...args
) => {
const wrappedComponent = WrappedComponent => {
const innerComponent = ({ ...props }) => {
return (
<WrapperComponent
{...props}
>
<WrappedComponent />
</WrapperComponent>
);
};
innerComponent.propTypes = {
...
};
return innerComponent;
};
wrappedComponent.propTypes = {
...
};
return wrappedComponent;
};
withEntity.propTypes = {
...
};
export default withEntity;
在一个单独的文件中,withEntity
函数的调用方式如下:
export const DoSomething = withEntity(...args)(MyComponent);
然后,在测试文件中DoSomething
组件,我正在尝试导入withEntity
函数并像这样模拟它:
import withEntity from "../../../shared/entity/higher_order_components/withEntity";
jest.mock("../../../shared/entity/higher_order_components/withEntity");
但是当我实际尝试运行测试时,我收到此错误:
● Test suite failed to run
TypeError: (0 , _withEntity.default)(...) is not a function
不知道该怎么处理这个错误,我在这里做错了什么?
嘲笑你的 HOC 应该是这样的:
jest.mock('../your/HOC', () => () =>
Component => props => <Component {...props} />
)
它可以读作:
jest.mock('../your/HOC', () => `
创建一个返回 HOC 函数的模拟,
() =>
返回你的 HOC 的函数withEntity(...args)
,
Component => props => <Component {...props} />
HOC 本身,它是一个获取组件并返回一个获取 props 的函数,并返回一个返回渲染组件及其 props 的函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)