Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

426
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda