I am attempting to test a function (with Karma) that is triggered by a window resize event. Everything works normally in the real world, but when I try to manually trigger the event in the test the function never gets called. This is resulting in a failed test.
Here is my HTML:
<div id="topnav"
class="navbar navbar-graylight header-width"
role="banner"
(window:resize)="onResize($event)"></div>
Here is my onResize() Function:
@Component({
selector: "main-header",
templateUrl: "main-header.component.html",
})
export class MainHeaderComponent {
public itWasTriggered = false;
public onResize(event) {
this.itWasTriggered = true;
}
}
Here is my Test:
it("Why is onResize() not being ran", () => {
const heroEl = fixture.debugElement.query(By.css(".navbar-graylight"));
heroEl.triggerEventHandler("window:resize", null);
expect(comp.itWasTriggered).toBe(true);
});
This is what shows up in the inspector:
<div _ngcontent-a-1="" class="navbar navbar-graylight header-width" id="topnav" role="banner">
<!--template bindings={}-->
<!--template bindings={}-->
</div>
window.resizeTo
doesn't seem to invoke the handler and you can't setinnerWidth
on the readonlycurrentTarget
. – Lith