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();
})})