Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: How I can test @HostListener in jasmine?

I have the next code in angular's component:

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

How can I test this in jasmine? How to to initiate window scroll event?

like image 601
Maxim Avatar asked May 30 '17 21:05

Maxim


2 Answers

Testing for window:scroll:

it('should do something on window scroll', () => {
  window.dispatchEvent(new Event('scroll'));
  expect(...)....
});
like image 67
Steve Brush Avatar answered Nov 17 '22 08:11

Steve Brush


You can try to make simple JS scroll by calling scrollTo function on window.

In case you want to make scroll top it will be:

window.scrollTo(0, 0);

Update

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

window.dispatchEvent(scrollEvent)
like image 24
Mikki Kobvel Avatar answered Nov 17 '22 09:11

Mikki Kobvel