I have the following component which utilizes ResizeObserver
(via https://www.npmjs.com/package/@juggle/resize-observer)
export class ResizeTestComponent implements OnInit {
constructor(public hostRef: ElementRef) {}
ngOnInit() {
const resizeObserver = new ResizeObserver(() => this.resized());
resizeObserver.observe(this.hostRef.nativeElement);
}
resized() {
console.log('resized');
}
}
If I change the window width manually, the component width changes and resized
is output to the console via the resized
function, as expected.
I want to write a Jasmine test to check if the resized
function is called when the element is resized.
How do I trigger the resizeObserver
?
I have tried sending a resize
event but the resized
function is not called:
it('resized function is called when element is resized', async(() => {
const fixture = TestBed.createComponent(ResizeTestComponent);
fixture.detectChanges();
const resizedSpy = spyOn(fixture.componentInstance, 'resized');
window.dispatchEvent(new Event('resize'));
expect(resizedSpy).toHaveBeenCalled();
}));
Stackblitz: https://stackblitz.com/edit/angular-testing-with-jasmine-f3vgbg?file=app%2Fresize-test.component.spec.ts