Browser history pushState/onpopstate/window.location not well managed
See original GitHub issueHello,
I would say history management is not complete. This test on my side fails with jsdom version 9.4.1:
describe('jsdom history management', () => {
before(() => {
// Init DOM with a fake document
// <base> and uri (initial uri) allow to do pushState in jsdom
jsdom.env({
html: `
<html>
<head>
<base href="http://localhost:8080/"></base>
</head>
</html>
`,
url: 'http://localhost:8080/',
done(err, window) {
global.window = window;
global.document = window.document;
window.console = global.console;
}
});
});
it('location not updated', () => {
window.history.pushState({}, 'route1', '/route1');
assert(window.location.pathname === '/route1'); // this test is OK
window.history.back();
assert(window.location.pathname === '/'); // this test fails pathname still with value '/route1'
});
it('onpopstate not called', () => {
window.onpopstate = () => { };
const spy_onpopstate = sinon.spy(window, 'onpopstate');
window.history.pushState({}, 'route1', '/route1');
window.history.back();
window.history.forward();
assert(spy_onpopstate.called);// this test fails as well
});
});
Issue Analytics
- State:
- Created 7 years ago
- Reactions:11
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Window: popstate event - Web APIs | MDN
The popstate event of the Window interface is fired when the active history entry changes while the user navigates the session history.
Read more >javascript - history.pushState - not working? - Stack Overflow
Suppose now that the user now navigates to http://google.com, then clicks back. At this point, the URL bar will display http://mozilla.org/bar. ...
Read more >Disable browser back button - OutSystems
I'm trying to disable or unutilized the browser back button, so that the user may not go back, I have tried the following...
Read more >Pushing and Popping with the History API - HTML5 Doctor
Historical Events in Navigation # · popstate event when the user navigates through their history, whether backwards or forwards, provided it isn' ...
Read more >5.4 Session history and navigation — HTML5 - W3C
Each Document object in a browsing context's session history is ... not the same Document as the Document of the specified entry ,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A work around, for anyone who finds this post and looking to get tests working, is using Sinon
spy
and mocking thehistory.pushState
orhistory.replaceState
methods.Here is the workaround I use (TypeScript):