如何使用 Angular 测试来自 window 事件的滚动?
How test a scroll from window event with Angular?
我正在尝试测试这个组件:
export class HeaderComponent implements OnInit {
sticky: boolean = false;
@HostListener('window:scroll', ["$event"]) onScroll(event: Event) {
let window = event.currentTarget as Window;
console.log(window.scrollY);
this.sticky = window.scrollY >= 30;
}
constructor() {
}
ngOnInit(): void {
}
}
还有测试:
it("onScroll_Down_StickyIsTrue", () => {
window.dispatchEvent(new Event("scroll"));
window.scrollTo(0 , 50);
expect(component.sticky).toBeTrue();
});
而且我不知道为什么 window.scrollY 仍然是 0。
有人可以帮助我吗?
试试这个:
it("onScroll_Down_StickyIsTrue", () => {
component.onScroll({
currentTarget: {
scrollY: 31
}
} as any);
expect(component.sticky).toBeTrue();
});
我正在尝试测试这个组件:
export class HeaderComponent implements OnInit {
sticky: boolean = false;
@HostListener('window:scroll', ["$event"]) onScroll(event: Event) {
let window = event.currentTarget as Window;
console.log(window.scrollY);
this.sticky = window.scrollY >= 30;
}
constructor() {
}
ngOnInit(): void {
}
}
还有测试:
it("onScroll_Down_StickyIsTrue", () => {
window.dispatchEvent(new Event("scroll"));
window.scrollTo(0 , 50);
expect(component.sticky).toBeTrue();
});
而且我不知道为什么 window.scrollY 仍然是 0。
有人可以帮助我吗?
试试这个:
it("onScroll_Down_StickyIsTrue", () => {
component.onScroll({
currentTarget: {
scrollY: 31
}
} as any);
expect(component.sticky).toBeTrue();
});