Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...