JEST react-testing-library无法读取未定义的属性“ push”

时间:2019-12-20 18:35:05

标签: jestjs react-testing-library

import React from 'react';
import { Provider } from 'react-redux';
import { RouteComponentProps, withRouter } from 'react-router-dom';
import { store } from './store';
import Routes from './router/router';

const App: React.FC<RouteComponentProps> = () => {
  return (
    <Provider store={store}>
      <Routes />
    </Provider>
  );
};

export default withRouter(App);
function renderWithRouterAndStore(
  ui,
  { route = '/', history = createMemoryHistory({ initialEntries: [route] }) } = {}
) {
  const Wrapper = ({ children }) => {
    return (
      <Provider store={store}>
        <IntlProvider locale="en">
          <MemoryRouter initialEntries={[route]}>
            <Route path="/login">{children}</Route>
          </MemoryRouter>
        </IntlProvider>
      </Provider>
    );
  };

  return {
    ...render(ui, { wrapper: Wrapper }),
    history,
  };
}

我有登录组件,当我提交表单时,如果登录成功,它将导航到“ /”。

我正在使用this.props.history.push('/')

但是我的测试用例失败,无法读取未定义的属性“ push”

如何使用RouteComponentProps测试React组件?

1 个答案:

答案 0 :(得分:0)

我不确定这是否可以解决此问题,但是您可以像这样模拟history.push:

df['Country'].str.split(', ').explode().value_counts()