Added more tests for HeroService
[examples/angular-tour-of-heroes] / src / app / hero.service.spec.ts
1 import { TestBed} from '@angular/core/testing';
2 import { TestScheduler } from 'rxjs/testing';
3
4 import { HeroService } from './hero.service';
5 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
6 import { Hero } from './hero';
7
8 describe('HeroService', () => {
9   let service: HeroService;
10   let httpTestingController: HttpTestingController;
11   let scheduler: TestScheduler;
12
13   beforeEach(() =>  {
14     TestBed.configureTestingModule({
15       imports: [ HttpClientTestingModule ]
16     });
17     service = TestBed.inject(HeroService);
18     httpTestingController = TestBed.inject(HttpTestingController);
19     scheduler = new TestScheduler(((actual, expected) => {
20       expect(actual).toEqual(expected);
21     }));
22   });
23
24   afterEach(() => {
25     // After every test, assert that there are no more pending requests.
26     httpTestingController.verify();
27   });
28
29   it('should be created', () => {
30     expect(service).toBeTruthy();
31   });
32
33   describe('#getHeroes', () => {
34
35     it('should return the expected heroes on success', () => {
36
37       const expectedHeroes: Hero[] = [{id: 11, name: 'Dr Nice'}];
38
39       service.getHeroes().subscribe(
40         heroes => expect(heroes).toEqual(expectedHeroes, 'should return expected heroes'),
41         fail
42       );
43
44       const req = httpTestingController.expectOne(service.heroesUrl);
45       expect(req.request.method).toEqual('GET');
46
47       req.flush(expectedHeroes);
48     });
49
50     it('should return an empty list if the remote server fails', () => {
51
52       service.getHeroes().subscribe(
53         heroes => expect(heroes).toEqual([], 'should return an empty list'),
54         fail
55       );
56
57       const req = httpTestingController.expectOne(service.heroesUrl);
58       expect(req.request.method).toEqual('GET');
59
60       // Respond with the mock heroes
61       req.flush('deliberate 500 error', {status: 500, statusText: 'Server Error'});
62     });
63   });
64
65   describe('#getHero', () => {
66
67     it('should return an empty observable for an invalid id', () => {
68
69       service.getHero(0).subscribe(fail, fail);
70
71       const req = httpTestingController.expectOne(`${service.heroesUrl}/0`);
72       expect(req.request.method).toEqual('GET');
73
74       req.flush('deliberate 404 error', {status: 404, statusText: 'Not Found'});
75     });
76
77     it('should return an observable with the requested hero for a valid id', () => {
78
79       const expectedHero: Hero = {id: 11, name: 'Dr Nice'};
80
81       service.getHero(11).subscribe(
82         hero => expect(hero).toEqual(expectedHero, 'should return expected heroes'),
83         fail
84       );
85
86       const req = httpTestingController.expectOne(`${service.heroesUrl}/11`);
87       expect(req.request.method).toEqual('GET');
88
89       req.flush(expectedHero);
90     });
91   });
92 });