Testing Asynchronous Actions

    1. import { QuoteService } from './quote.service';
    2. import { QuoteComponent } from './quote.component';
    3. import { provide } from '@angular/core';
    4. import {
    5. async,
    6. TestBed,
    7. fakeAsync,
    8. tick,
    9. } from '@angular/core/testing';
    10. class MockQuoteService {
    11. public quote: string = 'Test quote';
    12. return Promise.resolve(this.quote);
    13. }
    14. }
    15. describe('Testing Quote Component', () => {
    16. let fixture;
    17. beforeEach(() => {
    18. TestBed.configureTestingModule({
    19. declarations: [
    20. QuoteComponent
    21. ],
    22. { provide: QuoteService, useClass: MockQuoteService }
    23. ]
    24. fixture = TestBed.createComponent(QuoteComponent);
    25. fixture.detectChanges();
    26. });
    27. it('Should get quote', fakeAsync(() => {
    28. fixture.componentInstance.getQuote();
    29. tick();
    30. fixture.detectChanges();
    31. const compiled = fixture.debugElement.nativeElement;
    32. expect(compiled.querySelector('div').innerText).toEqual('Test quote');
    33. });