【发布时间】:2020-04-23 15:01:13
【问题描述】:
这两天我一直在寻找测试scrollTop 事件的解决方案,但我在任何地方都没有找到解决方案。我所有的尝试都返回了相同的错误...
TypeError: Cannot read property 'scrollTop' of undefined
header.component.ts
@HostListener('window:scroll', ['$event'])
onWindowScroll(): void {
if(document.scrollingElement.scrollTop > 63){
this.headerElement.classList.add('height-63');
}else{
this.headerElement.classList.remove('height-63');
}
}
header.component.spec
it('should test scrollTop', () => {
window.scrollTo(0, 500);
//document.scrollingElement.scrollTop = 500 --> I already tried to set the scrollTop value
fixture.detectChanges();
component.onWindowScroll();
expect(fixture.debugElement.nativeElement.querySelector('.height-63')).toBeTruthy();
});
【问题讨论】:
标签: angular unit-testing jestjs undefined scrolltop