Answer the question
In order to leave comments, you need to log in
How to simulate delay?
There is a service that contains the state:
@Injectable({
providedIn: 'root'
})
export class WordsService {
words: string[] = [
'qqq',
'www',
'eee',
'rrr',
'ttt',
'yyy',
];
constructor() { }
}
@Component({
selector: 'app-page3',
template: `
<div *ngFor="let word of serviceWords" class="word-el">
{{ word }}
</div>
<button (click)="getWordsFromService()" id="getWordsBtn">get words from service</button>
`,
styleUrls: ['./page3.component.scss']
})
export class Page3Component {
serviceWords: string[] = [];
constructor(private wordsService: WordsService) { }
getWordsFromService() {
this.serviceWords = this.wordsService.words;
}
}
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ Page3Component ]
})
.compileComponents();
});
it('should display list after getWords-button', () => {
fixture.detectChanges();
const compliedComponent = fixture.debugElement.nativeElement;
const btn = compliedComponent.querySelector('#getWordsBtn');
btn.dispatchEvent(new Event('click'));
setTimeout(() => {
expect(compliedComponent.querySelector('.word-el')).toBeTruthy();
}, 1000);
});
tick();
expect(compliedComponent.querySelector('.word-el')).toBeTruthy();
waits();
expect(compliedComponent.querySelector('.word-el')).toBeTruthy();
Answer the question
In order to leave comments, you need to log in
tick must be inside fakeAsync and it takes time too.
You can await fixture.whenStable()
also inside fakeAsync
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question