typescript - Test Angular Component with setTimeout in method -


i'm trying test method in angular component so:

  answerselect(answer: any): void {     this.selectedanswer = answer;      // submit answer     settimeout(() => {       if (answer.correct) this.submit();       this.selectedanswer = undefined;     }, 500);   } 

this have far:

 describe('answerselect()', () => {        it('should set this.selectedanswer = answer', async(() => {         spyon(instance, 'answerselect').and.callthrough();         instance.selectedanswer = 'nottheanswer';         instance.answerselect(('answer'));          expect(instance.selectedanswer).tobe('answer');       }));        it('should submit answer', async(() => {         spyon(instance, 'answerselect').and.callthrough();         spyon(instance, 'submit');         instance.selectedanswer = 'nottheanswer';         instance.answerselect({correct: true});          expect(instance.submit).tohavebeencalled();         expect(instance.selectedanswer).tobe(undefined);       }));    }); 

the first test (should set this.selectedanswer = answer) works expected.

however, cannot seem second test (should submit answer) working due settimeout() , getting following 2 errors:

1) expected spy submit have been called. this.submit() doesn't called.

and

2) expected object({ correct: true }) undefined. this.selectedanswer = undefined; doesn't called either.

how can ensure both of these functions within settimeout called?

how can ensure both of these functions within settimeout called?

add delay in test , make sure test async.

more

checkout jasmine async support https://jasmine.github.io/2.0/introduction.html#section-asynchronous_support


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -