如何使用Redux和酶测试React Router参数

Nosajool

这个已经解决了

我有一个用connect()包装的组件withRouter为了呈现自己,它使用中的路由信息this.props.match.params在我的酶测试中,当我登录时match,传递给道具WrappedComponent似乎没有传播props但是,如果我添加了其他道具(除了以外的其他键match),它们可以很好地传播。

以下测试复制了我的问题。我对React还是很陌生,所以如果有人对我做错了什么以及我可以做些什么来调试它有一些提示,将不胜感激。

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import { MemoryRouter, withRouter } from 'react-router-dom';
import { Provider, connect } from 'react-redux';


class TestComponent extends Component {
  constructor(props) {
    super(props);
    console.log(props);
  }

  render() {
    const { params } = this.props.match;
    return (
      <div>
        { params.var1 }
      </div>
    );
  }
}

TestComponent.propTypes = {
  match: PropTypes.shape({
    params: PropTypes.shape({
      var1: PropTypes.string.isRequired,
    }),
  }).isRequired,
};

const WrappedComponent = withRouter(connect()(TestComponent));

describe('WrappedComponent', () => {
  const mockStore = configureStore();
  const initialState = {};
  let store;
  let wrapper;

  beforeEach(() => {
    store = mockStore(initialState);

    const props = {
      match: {
        params: {
          roomType: 'public',
          roomName: 'test',
        },
      },
    };

    wrapper = mount(
      <Provider store={store}>
        <MemoryRouter>
          <WrappedComponent {...props} />
        </MemoryRouter>
      </Provider>,
    );
  });

  it('renders without crashing', () => {
    expect(wrapper).toHaveLength(1);
  });
});

当我运行测试时,match.params{}

console.error node_modules/react/node_modules/fbjs/lib/warning.js:33
Warning: Failed prop type: The prop `match.params.roomType` is marked as required in `TestComponent`, but its value is `undefined`.
    in TestComponent (created by Connect(TestComponent))
    in Connect(TestComponent) (created by Route)
    in Route (created by withRouter(Connect(TestComponent)))
    in withRouter(Connect(TestComponent)) (at TestComponent.js:56)
    in Router (created by MemoryRouter)
    in MemoryRouter (at TestComponent.js:55)
    in Provider (created by WrapperComponent)
    in WrapperComponent

console.log src/components/__tests__/TestComponent.js:11
{ match: { path: '/', url: '/', params: {}, isExact: true },
  location:
   { pathname: '/',
     search: '',
     hash: '',
     state: undefined,
     key: '5gc3i1' },
  history:
   { length: 1,
     action: 'POP',
     location:
      { pathname: '/',
        search: '',
        hash: '',
        state: undefined,
        key: '5gc3i1' },
     index: 0,
     entries: [ [Object] ],
     createHref: [Function: createPath],
     push: [Function: push],
     replace: [Function: replace],
     go: [Function: go],
     goBack: [Function: goBack],
     goForward: [Function: goForward],
     canGo: [Function: canGo],
     block: [Function: block],
     listen: [Function: listen] },
  staticContext: undefined,
  dispatch: [Function: dispatch] }

编辑:我已经通过修改测试以使用它来通过测试:

wrapper = mount(
  <Provider store={store}>
    <MemoryRouter initialEntries={['/param1val/param2val']}>
      <Switch>
        <Route path="/:param1/:param2" component={WrappedTestComponent} />
      </Switch>
    </MemoryRouter>
  </Provider>,
);

如果没有使用Switchand的推荐方法,我将开放这个开放问题Route

Nosajool

Slawa的答案是我最终要做的事情。测试React Router和Redux的功能没有用。导出普通的React组件使测试变得更加简单。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章