简体   繁体   English

如何用酶测试包裹的成分?

[英]How to test wrapped component with enzyme?

My React components is wrapped with differents HOCs (i18next, graphql etc...), unfortunately I can't test my wrappedComponents with enzyme. 我的React组件包装有不同的HOC(i18next,graphql等...),不幸的是我无法用酶测试包装好的组件。

What is your best practice to test your wrappedComponents? 您测试包装组件的最佳实践是什么?

I do not wish to export wrapped and unwrapped components version, I would like to only export wrapped component for my application logic and be able to test unwrapped component inside my wrapped component. 我不希望导出包装和未包装的组件版本,我只想为我的应用程序逻辑导出包装的组件,并且能够在包装的组件内部测试包装的组件。

Thanks for reading me! 感谢您阅读我!

I tend to favour the same approach that Redux takes with connect , and make the actual component available as a static on the exported component. 我倾向于使用Redux与connect相同的方法,并使实际组件在导出的组件上可以作为静态组件使用。

const MyComponent = (props) => ( ... );

const ConnectedComponent = usingMyHoc(...)(MyComponent) ;
ConnectedComponent.WrappedComponent = MyComponent;

export default ConnectedComponent;

I agree that exporting unwrapped components feels unnatural -- you want to test your components exactly how they are used in production. 我同意导出未包装的组件感觉很不自然-您想准确测试组件在生产中的使用方式。

My preferred method is creating a wrapped render function to use in testing: 我的首选方法是创建一个包装的渲染函数以用于测试:

function renderWithProviders(componentTree, options = {}) {
    const {initialState} = options;
    const store = initializeStore(initialState);
    return {
        ...render(
    <Provider store={store}>
        <SnackbarProvider>{componentTree}</SnackbarProvider>
    </Provider>
    )};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM