Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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