简体   繁体   English

如何使用Jest和Enzyme测试具有Router,Redux和两个HOC的React组件?

[英]How to test a React component that has Router, Redux and two HOCs… with Jest and Enzyme?

I am currently unable to find a solution to this problem. 我目前无法找到解决此问题的方法。 I have a React Component that is connected to React Router 4, Redux store and is wrapped by two HOCs. 我有一个React组件连接到React Router 4,Redux存储并由两个HOC包装。 Its pretty crazy, but this is how it was coded. 它非常疯狂,但这是它的编码方式。 Here is the export to give you an idea: 这是出口给你一个想法:

export default withFetch(Component)(fetchData, mapStateToProps)

I am trying to run some basic tests on it: 我正在尝试对它进行一些基本测试:

  it('should render self and subcomponents', () => {
    const wrapper = shallow(<Component {...props} />)
    expect(toJson(wrapper)).toMatchSnapshot()
  })

Which outputs a console.log/snapshot of: 其中输出一个console.log / snapshot:

<Route render={[Function: render]} />

Things tried but no succeed: 事情尝试但没有成功:

  1. I've tried wrapping my component in the Memory Router 我已经尝试将我的组件包装在Memory Router
  2. Supply a redux store to the component 为组件提供redux存储
  3. Used .dive() and .chilndren() to try and see the children 使用.dive().chilndren()来尝试查看孩子们
  4. Tried mount and render with no success. 尝试mountrender没有成功。

Still keeps rendering the <Route render={[Function: render]} /> 仍然保持渲染<Route render={[Function: render]} />

Trying out : 尝试 :

<MemoryRouter>
    <Component {...props} />
</MemoryRouter>

Still produces the same result. 仍然产生相同的结果。 Note that I've also tried importing my component as 请注意,我也尝试将我的组件导入为

import { Component } from './components/'

But it returns undefined. 但它返回undefined。

Any help is deeply appreciated. 非常感谢任何帮助。 Thank you! 谢谢! 😊🙏 😊🙏

I assume that by <Router> you are referring to BrowserRouter. 我假设通过<Router>你指的是BrowserRouter。 The best way is to isolate the wrapped component and test it with testing alternatives. 最好的方法是隔离包裹的组件并使用测试替代方案对其进行测试。

For example assume that you want to test that: 例如,假设您要测试:

// App.jsx

export const App = () => 
<Router>
  <ReduxProvider>
     <AppInner>
  </ReduxProvider>
</Router>

My suggestion is to test AppInner with testing env of Router & ReduxProvider. 我的建议是使用Router&ReduxProvider的测试AppInner测试AppInner。

In tests: 在测试中:

// AppInner.test.jsx

import {mount} from 'enzyme';
import {MemoryRouter} from 'react-router';
describe('AppInner', () => {
   it('should do something', () => {
      const TestingComponent = () =>
      <MemoryRouter>
         <ReduxProvider>
            <AppInner />
         <ReduxProvider>
      <MemoryRouter>;
      const component = mount(TestingComponent);
   });
})

Pay attention that I've wrapped the AppInner with MemoryRouter , it allows your mimic router but without the dependency of the browser. 注意我已经将AppInnerMemoryRouter包装MemoryRouter ,它允许你的模拟路由器但没有浏览器的依赖。

For more info you can read the testing section of react-router ; 有关更多信息,您可以阅读react-routertesting部分;

暂无
暂无

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

相关问题 如何 Jest / Enzyme 测试 react-router-dom withRouter() 在 React 中包装的组件? - How to Jest / Enzyme test a react-router-dom withRouter() wrapped component in React? 如何在Enzyme / React中测试包含连接到Redux的组件的组件? - How to test component that contains a component connected to Redux in Enzyme / React? 如何使用 Jest 和/或 Enzyme 测试由 React 组件呈现的样式和媒体查询 - How to test styles and media queries rendered by a React component with Jest and/or Enzyme 如何使用玩笑和酶测试异步数据获取反应组件? - How to test async data fetching react component using jest and enzyme? 如何使用 Jest/Enzyme 在功能性 React 组件中测试 lambda 函数? - How to test lambda functions in a functional React Component using Jest/Enzyme? 如何在反应中使用 jest 和酶对高阶组件进行单元测试 - How to unit test a higher order component using jest and enzyme in react 如何使用 Jest + Enzyme 覆盖 Svg 反应组件的测试用例 - How to cover test cases for Svg react component using Jest + Enzyme 如何测试默认道具功能是否附加到组件? | 反应| Jest | 酶 - How to test default props functions is attached to component or not ? | React | Jest | Enzyme 如何使用Jest + Enzyme测试React组件的样式? - How can I test React component's style with Jest + Enzyme? 如何使用Jest和Enzyme为简单的React组件编写测试用例 - How to write a test case for a simple React component using Jest and Enzyme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM