Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

458
Vistas
how do I write a unit test for a function?

how do I write a unit test for the scroll function? I don't know how to write tests when there are events, I wrote the first test which is easy. Please help

init(){
   if (this.isMobile) {
            this.scroll(div, scrollDown, scrollUp)
        }
} 

scroll(div: HTMLElement, scrollDown: string | null, scrollUp: string | null) {
        let prevAnimationAPosY: number = window.scrollY;
        let prevPosY: number = window.scrollY;

        window.addEventListener('scroll', function () {
            const currentPosition: string = prevPosY >= window.scrollY ? 'top : 'bottom;

            if (currentPosition === 'bottom' && prevPosY > Number(scrollDown)) {
                div.classList.add('start');
                prevAnimationAPosY = window.scrollY;
            } else if (currentPosition === 'top && prevPosY < (prevAnimationAPosY - Number(scrollUp))) {
                div.classList.remove('start');
            }

            prevPosY = window.scrollY;
        }, { passive: true });
    }

my test:

test("should be started", () => {
const hat = new Hat(true),
hatSpy = jest.spyOn(hat, "init");
hat.init();
expect(hatSpy).toHaveBeenCalled();
})})
about 4 years ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda