Unit testing @HostListener method in Angular throwing 'some of your test did a full page reload error'

Viewed 213

I have the below code in one of the component in the Angular app (v13) and while unit testing the same in the below way throwing 'some of your test did a full page reload error'. Im using the below versions in the project

Angular - 13.1.1
Karma - 6.3.17
Karma-jasmine - 4.0.0

test.component.ts code

@HostListener("window:beforeunload", ["$event"]) unloadHandler(event: Event) {
    if (this.result) {
        event.returnValue = false;
    }
}

test.component.spec.ts code

it('test @HostListener event', () => {
 const event = window.dispatchEvent(new Event('beforeunload'));
 component.result = true;
 expect(event).tobe(false);
});

Kindly help to get rid of this error. I didn't get any proper solution for this till now.

0 Answers
Related