简体   繁体   中英

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

I want to write a unit test case for the following 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM