Testing window.onbeforeunload
See original GitHub issueHi guys,
I’m trying to test an event handler for window.onbeforeunload
and getting
some of your tests did a full page reload!
Even though I’m running my tests with
angular.element(window).triggerHandler('beforeunload')
I see the code and it’s actually listening for the same event. Is there any way to temporary disable that error or handle it somehow else?
Issue Analytics
- State:
- Created 9 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Test onbeforeunload in Jest - Stack Overflow
Show activity on this post. You best bet should be dispatching the event yourself. window.dispatchEvent(new Event("beforeunload"));.
Read more >Window: beforeunload event - Web APIs | MDN
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible...
Read more >window.onbeforeunload and Cypress - Gleb Bahmutov
The application code uses window.onbeforeunload callback to ask the user to confirm before navigating away from the page. public/app.js ...
Read more >onBeforeUnload test - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor. ... window.onbeforeunload = function(e) {. 2. e = e || window.event;....
Read more >unload And beforeunload Events And How To Debug Them ...
log("second event to fire (2)"); }; // top window's unload event would fire after all beforeunload events have been fired window.onunload = ...
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
Setting onbeforeunload to null helps here:
Still curious if you have some other suggestions,
Thanks
For reference, this worked for me. Angular service:
And onto unit testing with Jasmine: