0

我正在尝试为组件创建快照测试,但是,我的商店不断出现未定义,我不知道为什么。我需要将什么传递到我的商店才能使此正确?我已经使用其他不需要 mapStateToProps 或 withRouter 的组件进行了快照测试,之前没有任何问题。

工作流表单.jsx

const mapStateToProps = (state, ownProps) => {
  return {
    ...ownProps,
    userID: state.data.userID,
    userObj: state.ui.userObj
  };
};

export default withRouter(
  connect(mapStateToProps, null)(WorkflowForm)
);

WorkflowForm.test.jsx

import React from 'react';
import renderer from 'react-test-renderer';
import { Provider } from 'react-redux';
import { BrowserRouter } from "react-router-dom";
import  WorkflowForm from "../WorkflowForm";
import { cleanup } from '@testing-library/react';
import configureMockStore from 'redux-mock-store';

afterEach(cleanup);

const mockStore = configureMockStore();
const store = mockStore({
  userID: "121",
  userObj: "Name"
});

describe('Test suits for WorkflowForm', () => {
  it('should match with snapshot', () => {
    const tree = renderer
      .create(
        <Provider store={store}>
          <BrowserRouter>
            <WorkflowForm />
          </BrowserRouter>
        </Provider>
      )
      .toJSON();
    expect(tree).toMatchSnapshot();
   });
});

这是我运行测试时遇到的错误

TypeError: Cannot read property 'userID' of undefined

      392 |   return {
      393 |     ...ownProps,
    > 394 |     userID: state.data.userID,
          |                        ^
      395 |     userObj: state.ui.userObj
      396 |   };
      397 | };
4

1 回答 1

1

您没有state正确提供模拟商店。它应该是:

const store = mockStore({
  data: { userID: '121' },
  ui: { userObj: 'Name' },
});
于 2021-07-01T03:12:56.617 回答