Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

415
Views
How to write a Jasmine Unit Test for addEventListener?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

expect(window), this line window is not a spy.

Try changing the line to:

expect(window.addEventListener).not.toHaveBeenCalledWith('e',
    PhoneInputComponent['preventDefault'], { passive: false });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!