[英]How to write unit test in angular for setTimeout function and activeElement?
I want to write a unit test case for the following function:我想为下面的function写一个单元测试用例:
onfactorblur():void{
var that=this;
setTimeout(function(){
var target=document.activeElement;
if(target.tagName=="BUTTON"){
if(target.textContent=="CONTINUE"){
that.onContinueClick();
}
else{
that.activeModal.close();
}
}
},.5);
}
I am new to angular unit testing and never faced a scenario where I had to test setTimeout within a function.我是 angular 单元测试的新手,从未遇到过必须在 function 中测试 setTimeout 的情况。 I have tried:
我努力了:
it('Should close the modal if target element is button',fakeAsync(()=>{
spyOn(component,'onContinueClick');
spyOn(component.activeModal,'close');
tick(1);
fixture.detectChanges();
fixture.whenStable().then(()=>{
//What should I write here
})
}));
You can use fakeAsync
, you can call tick(500)
to simulates the asynchronous passage of time more details here https://angular.io/guide/testing-components-scenarios#async-test-with-fakeasync你可以使用
fakeAsync
,你可以调用tick(500)
来模拟时间的异步流逝 更多细节在这里https://angular.io/guide/testing-components-scenarios#async-test-with-fakeasync
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.