0
votes
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,
  };
}

I have Login Component, when i submit the form and if the login is successfully, it navigates to '/'.

I am using this.props.history.push('/')

But my test case fail, Cannot read property 'push' of undefined

How to test a React component with RouteComponentProps?

1

1 Answers

0
votes

I am not sure if this is a solution to this problem but you can mock history.push like this:

let props = {
   history: historyMock
};
shallow(<LoginComponent {...props} />);