Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

i'm achieving bellow error with following code after my expect of the test case ???

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

 spyOn(window, 'setInterval').and.callThrough();

 flush();
 flushMicrotasks();
 discardPeriodicTasks();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...