HeroService returns an empty Observable, if the requested hero can't be found
[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 { Hero } from './hero';
6 import {map} from 'rxjs/operators';
7
8 describe('HeroService', () => {
9   let service: HeroService;
10   let scheduler: TestScheduler;
11
12   beforeEach(() =>  {
13     TestBed.configureTestingModule({});
14     service = TestBed.inject(HeroService);
15     scheduler = new TestScheduler(((actual, expected) => {
16       expect(actual).toEqual(expected);
17     }));
18   });
19
20   it('should be created', () => {
21     expect(service).toBeTruthy();
22   });
23
24   it('get for id=0 should return an empty Observable', () =>
25     scheduler.run(({expectObservable}) => {
26       expectObservable(service.getHero(0)).toBe('(|)');
27     })
28   );
29
30   it('get for id=11 should return an Observable, that contains the hero with id 11', () =>
31     scheduler.run(({expectObservable}) => {
32       expectObservable(service.getHero(11)).toBe('(a|)', { a: { id: 11, name: 'Dr Nice' }});
33     })
34   );
35 });