/
heroes.container.spec.ts
149 lines (120 loc) · 4.4 KB
/
heroes.container.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { fakeAsync, tick } from '@angular/core/testing';
import { asapScheduler, of as observableOf, Subject, throwError } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { femaleMarvelHeroes } from '../../test/female-marvel-heroes';
import { Hero } from '../hero';
import { HeroService } from '../hero.service';
import { HeroesContainerComponent } from './heroes.container';
describe(HeroesContainerComponent.name, () => {
function createHeroServiceStub(): jasmine.SpyObj<HeroService> {
const stub: jasmine.SpyObj<HeroService> = jasmine.createSpyObj(
HeroService.name,
[
'addHero',
'deleteHero',
'getHeroes',
]);
resetHeroServiceStub(stub);
return stub;
}
function resetHeroServiceStub(stub: jasmine.SpyObj<HeroService>): void {
stub.addHero
.and.callFake(({ name }: Partial<Hero>) => observableOf({
id: 42,
name,
}, asapScheduler))
.calls.reset();
stub.deleteHero
.and.callFake((hero: Hero) => observableOf(hero, asapScheduler))
.calls.reset();
stub.getHeroes
.and.returnValue(observableOf(femaleMarvelHeroes, asapScheduler))
.calls.reset();
}
let container: HeroesContainerComponent;
const destroy: Subject<void> = new Subject();
const heroServiceStub: jasmine.SpyObj<HeroService> = createHeroServiceStub();
const observer: jasmine.Spy = jasmine.createSpy('heroes observer');
beforeEach(fakeAsync(() => {
container = new HeroesContainerComponent(
heroServiceStub as unknown as HeroService);
container.heroes$.pipe(takeUntil(destroy)).subscribe(observer);
tick();
}));
afterEach(() => {
destroy.next();
observer.calls.reset();
resetHeroServiceStub(heroServiceStub);
});
afterAll(() => {
destroy.complete();
});
describe('emits all heroes', () => {
it('all heroes are emitted after subscribing', () => {
expect(observer).toHaveBeenCalledWith(femaleMarvelHeroes);
});
it(`delegates to ${HeroService.name}`, () => {
expect(heroServiceStub.getHeroes).toHaveBeenCalledTimes(1);
});
});
describe('adds a hero', () => {
it('emits the specified hero when server responds', fakeAsync(() => {
const wonderWoman = 'Wonder Woman';
container.add(wonderWoman);
tick();
expect(observer).toHaveBeenCalledWith([
...femaleMarvelHeroes,
{ id: 42, name: wonderWoman },
]);
}));
it(`delegates to ${HeroService.name}`, () => {
const hawkeye = 'Hawkeye (Kate Bishop)';
container.add(hawkeye);
expect(heroServiceStub.addHero).toHaveBeenCalledTimes(1);
expect(heroServiceStub.addHero).toHaveBeenCalledWith({ name: hawkeye });
});
it('does not emit the specified hero when server fails', fakeAsync(() => {
heroServiceStub.addHero.and.returnValue(
throwError(new Error('server error'), asapScheduler));
const scarletWitch = 'Scarlet Witch';
container.add(scarletWitch);
tick();
expect(observer).not.toHaveBeenCalledWith([
...femaleMarvelHeroes,
{ id: 42, name: scarletWitch },
]);
}));
});
describe('deletes a hero', () => {
it(`delegates to ${HeroService.name}`, () => {
const gamora: Hero = femaleMarvelHeroes.find(x => x.name === 'Gamora');
container.delete(gamora);
expect(heroServiceStub.deleteHero).toHaveBeenCalledTimes(1);
expect(heroServiceStub.deleteHero).toHaveBeenCalledWith(gamora);
});
it('emits all other heroes immediately', fakeAsync(() => {
const elektra: Hero = femaleMarvelHeroes.find(x => x.name === 'Elektra');
container.delete(elektra);
tick();
expect(observer).toHaveBeenCalledWith(
femaleMarvelHeroes.filter(x => x.id !== elektra.id));
}));
it('emits the specified hero when server fails', fakeAsync(() => {
function compareIdAscending(a: Hero, b: Hero): number {
return (a.id < b.id)
? -1
: (a.id > b.id)
? 1
: 0;
}
heroServiceStub.deleteHero.and.returnValue(
throwError(new Error('timeout'), asapScheduler));
const storm: Hero = femaleMarvelHeroes.find(x => x.name === 'Storm');
container.delete(storm);
tick();
const emittedHeroes: Hero[] = observer.calls.mostRecent().args[0];
emittedHeroes.sort(compareIdAscending);
expect(emittedHeroes).toEqual(femaleMarvelHeroes);
}));
});
});