【发布时间】:2017-06-05 23:50:52
【问题描述】:
我的组件中有一个 close 函数,其中包含一个 setTimeout(),以便让动画有时间完成。
public close() {
this.animate = "inactive"
setTimeout(() => {
this.show = false
}, 250)
}
this.show 绑定到 ngIf。
this.animate 绑定到动画。
我有一个测试需要测试这个功能
it("tests the exit button click", () => {
comp.close()
fixture.detectChanges()
//verifies the element is no longer in the DOM
const popUpWindow = fixture.debugElement.query(By.css("#popup-window"))
expect(popUpWindow).toEqual(null)
})
当有setTimeout()时如何正确测试这个函数?
我使用的是jasmine.clock().tick(251),但窗口永远不会消失。对此也有什么想法吗?
【问题讨论】:
-
您是否尝试过使用
done? -
@jonrsharpe 究竟如何
-
那么我建议你从那里开始你的研究;它用于测试异步进程。
-
不是您最初要求的,而是使用 Angular 触发的
done事件而不是使用超时。在您的模板中,您可以使用<my-tag [@myAnimation]="animate" (@myAnimation.done)="show=false">--.done事件代码将在您的动画完成后立即运行。
标签: angular testing jasmine settimeout karma-jasmine