I couldn't figure out how could I write a Jasmine unit test for AddEventListener? How do I test the following code if the addEventListener is working?
private static disableScrollingOnPageWhenPanelOpen(): void {
window.addEventListener('DOMMouseScroll', PhoneInputComponent.preventDefault, { passive: false });
window.addEventListener('mousewheel', PhoneInputComponent.preventDefault, { passive: false });
window.addEventListener('touchmove', PhoneInputComponent.preventDefault, { passive: false });}
This is what I've tried but it wasn't working.
describe('disableScrollingOnPageWhenPanelOpen', () => {
it('should disable DOMMouseScroll', () => {
const e = new Event('DOMMouseScroll');
PhoneInputComponent['disableScrollingOnPageWhenPanelOpen']();
window.dispatchEvent(e);
spyOn(window, 'addEventListener').and.callThrough();
expect(window).toHaveBeenCalledWith('e',
PhoneInputComponent['preventDefault'], { passive: false });
});
Could anyone help me with this? I was gonna test if the addEventListener has been called but I'm not sure if the method I'm using is the right approach to test the code.
expect(window), this line window is not a spy.
Try changing the line to:
expect(window.addEventListener).not.toHaveBeenCalledWith('e',
PhoneInputComponent['preventDefault'], { passive: false });