reactjs - 传递所有 props 的 HOC

标签 reactjs testing jestjs enzyme higher-order-components

我正在考虑编写一些 React 的实用程序(可能是 HOC),它允许我测试此类代码:

    const wrapper = enzyme
      .mount(
        withTestTheme(
          <JsonInput
            onChange={onChange}
            onValueChange={mockOnValueChange}
            value={exampleJsonStringValidated}
          />),
      );

withTestTheme 是一个包装器,它提供主题的属性,但它让我在测试期间出现问题,因为它会导致访问我想要测试的根组件时出现问题。对一些有用的实用程序有什么想法可以更轻松地测试上面的代码吗?

下面的 withTestTheme 提供主题 - 浅色和深色:

export const withTestTheme = (Component: React.ReactChild) => (
  <ThemeProvider theme={TestTheme}>
  {Component}
</ThemeProvider>
);

最佳答案

解决我问题的代码:

export const withTestThemeWrapper = (props: { children: React.ReactElement }) => (
  <ThemeProvider theme={TestTheme}>
    {props.children}
  </ThemeProvider>
);

关于reactjs - 传递所有 props 的 HOC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58554425/

相关文章:

node.js - 如何创建模拟 http.ServerRequest 和 http.ServerResponse?

typescript - Jest 不转换导入的 typescript

css - 如何增加 Material UI 下拉菜单的宽度

reactjs - Redux Saga 的好例子

超时后的Java/测试&运行

npm - VSCode 调试和 Jest : No breakpoints running with npm, 使用节点运行时出现 CWD 错误

javascript - 如何模拟作为参数传递的回调的调用?

javascript - 带有react-redux组件的React typescript需要所有传递的props

javascript - react BaseTable rowSpan "flickering"

javascript - redux 中的应用程序与本地状态