簡體   English   中英

如何在 angular 中為 setTimeout function 和 activeElement 編寫單元測試?

[英]How to write unit test in angular for setTimeout function and activeElement?

我想為下面的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);
 }

我是 angular 單元測試的新手,從未遇到過必須在 function 中測試 setTimeout 的情況。 我努力了:

    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
      })
    }));

你可以使用fakeAsync ,你可以調用tick(500)來模擬時間的異步流逝 更多細節在這里https://angular.io/guide/testing-components-scenarios#async-test-with-fakeasync

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM