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

456
Views
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
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!