mardi 25 août 2020

how can i do unit test for setInterval in ngoninit?

I have a refresh() function which executes the updated data scenario, here what i'm trying to achieve is for every 1min it has to refresh automatically without user interaction, so where i'm using setinterval in ngoninit so that i can executes for every 60 sec and i'm clearing those interval in ngDestroy;
Here is the component.ts

ngOnInit(): any {
 this.refresh();
 this.interval = setInterval(() => {
                      if (this.hideLog) {
                        this.updateWorkflow();
                      } else {
                        this.refresh();
                      }
                  },                            60000); 

}



ngOnDestroy(): void {
  this.clearInt();
}
clearInt(): void {
  if (this.interval) {
    clearInterval(this.interval);
  }
}

How can i add test for this in my spec.ts file so that ngonint test describe should pass

Error while running my test case:

Error: 1 periodic timer(s) still in the queue.

can anyone help me on this

Aucun commentaire:

Enregistrer un commentaire