使用商店Mocha和酶测试组件

时间:2018-08-24 16:59:39

标签: reactjs testing mocha store enzyme

我正在尝试测试我的组件,但是由于商店原因,我出现了错误。

import React from 'react';
import { shallow, simulate } from 'enzyme';
import configureStore from 'redux-mock-store';
import { Provider } from "react-redux";

import DropzoneComponent from '../components/DropzoneComponent';

const mockStore = configureStore();
const store = mockStore({});


describe('Dropzone component live test', () => {
    it('renders the component', () => {
      const wrapper = shallow(
        <Provider store = {store}>
          <DropzoneComponent />
        </Provider>).dive();
      expect(wrapper.exists()).to.equal(true)
    });
});

错误是:

  

永久违反:在上下文或以下环境中均找不到“存储”   “ Connect(withRouter(DropzoneComponent))”的道具。要么包装   中的根组件,或显式传递“ store”作为道具   到“ Connect(withRouter(DropzoneComponent))”。

1 个答案:

答案 0 :(得分:1)

您的组件被withRoter包裹,您需要在使用WrappedComponent对其进行浅层渲染时将其公开,如下所示:

describe('Dropzone component live test', () => {
it('renders the component', () => {
  const wrapper = shallow(
    <Provider store = {store}>
      <DropzoneComponent.WrappedComponent />
    </Provider>).dive();
  expect(wrapper.exists()).to.equal(true)
 });
});

让我知道问题是否仍然存在