Last active
October 10, 2022 13:45
-
-
Save izifortune/192cf86f7b28b48c56be3d57462185eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { TestBed, inject } from '@angular/core/testing'; | |
import { | |
HttpClientTestingModule, | |
HttpTestingController | |
} from '@angular/common/http/testing'; | |
import { | |
HttpClient, | |
} from '@angular/common/http'; | |
import { SampleService } from './sample.service'; | |
const mockAirports = { | |
DUB: { name: 'Dublin' }, | |
WRO: { name: 'Wroclaw' }, | |
MAD: { name: 'Madrid' } | |
}; | |
describe('Service: SampleService', () => { | |
let httpMock: HttpTestingController; | |
let service: SampleService; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
imports: [ HttpClientTestingModule ], | |
providers: [ | |
SampleService | |
] | |
}); | |
}); | |
beforeEach( | |
inject([SampleService, HttpTestingController], (_service, _httpMock) => { | |
service = _service; | |
httpMock = _httpMock; | |
})); | |
it('fetchAll$: should return a sorted list', () => { | |
service.fetchAll$().subscribe(airports => { | |
expect(airports.length).toBe(3); | |
expect(airports[2][0]).toBe('WRO'); | |
}); | |
const req = httpMock.expectOne('https://foo.bar.com/airports'); | |
req.flush(mockAirports); | |
httpMock.verify(); | |
}); | |
it('fetchByIATA$: should the selected airport', () => { | |
service.fetchByIATA$('MAD').subscribe(airport => { | |
expect(airport.name).toBe('Madrid'); | |
}); | |
const req = httpMock.expectOne('https://foo.bar.com/airports'); | |
req.flush(mockAirports); | |
httpMock.verify(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great! How to create a testbed for a component with multiple service dependencies?