supasate / connected-react-router

A Redux binding for React Router v4
MIT License
4.73k stars 592 forks source link

Location cannot be moved in testing-library #533

Open joyfuI opened 3 years ago

joyfuI commented 3 years ago

I used history.push() to test rendering in a specific location, but the location did not move the rendering result.

When I output the history object to the log, it was confirmed that it returned to the first location after moving to the location.

If you use react-router-dom's <Router> instead of <ConnectedRouter>, this doesn't happen.

What's the problem? Here is an example code.

https://codesandbox.io/s/connected-react-router-2-x9dng

john-pimq commented 2 years ago

If I add noInitialPop on the <ConnectedRouter />, I found the location would be changed after execute history.push. But I don't know whether this is a correct way to write test like this.

App.js

const App = () => {
  const localtion = useLocation();
  // without 'noInitialPop', the location's pathname would be alway equal to '/'
  // with 'noInitialPop', the location's pathname would be equal to '/another-path'
  console.log(location);
}

App.test.jsx

test('location should be changed', () => {
  // After add noInitialPop this push would be trigger
  browserHistory.push('/another-path');

  render(
      <Provider store={store}>
        <ConnectedRouter history={browserHistory} noInitialPop>
          <App />
        </ConnectedRouter>
      </Provider>
  )
})

For reference, I just found this link to explain about the noInitialPop