Angular: How can I check @HostListener in jasmine?

I have the following code in an angular component:

@HostListener('window:scroll', []) onWindowScroll() {
  this.showScrollToTop = false;
}

How can I test it in jasmine? How to trigger a window scroll event?

+4
source share
2 answers

Testing for window: scrolling:

it('should do something on window scroll', () => {
  window.dispatchEvent(new Event('scroll'));
  expect(...)....
});
+2
source

You can try to do a simple JS scroll by calling a function scrollToin a window.

If you want to do the top of the scroll, this would be:

window.scrollTo(0, 0);

Update

var scrollEvent = document.createEvent('CustomEvent');
scrollEvent.initCustomEvent( 'scroll', false, false, null );

window.dispatchEvent(scrollEvent)
+1
source

Source: https://habr.com/ru/post/1678199/


All Articles