I'm working in an Angular 8 project.
I wanted to have the window throw a confirm prompt if the user tried to navigate away from the site, I was able to do this by adding this function to my component.
@HostListener("window:beforeunload", ["$event"])
unloadNotification($event: any) {
if (this.confirmNavigation) {
$event.returnValue = true;
}
}
Now I'm trying to figure out how to unit test this, and I'm not sure how to do it. Would I need to mock the window in order to trigger the event? Would I make some sort of event object and call unloadNotification with it directly (not sure how to verify the result of it though)?
I'm even sure if this is needed in unit testing, would it be more of an integration test?
I also need to make sure this prompt isn't actually triggered when I run the tests in karma (because it'll stop the test). So I need to mock that?
Also, I think I want to destroy this listener in the afterEach of my tests, but not sure how.
Any help would be appreciated, thanks.