Location cannot be moved in testing-library
joyfuI opened this issue · 1 comments
joyfuI commented
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.
john-pimq commented
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