forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.service.spec.ts
118 lines (88 loc) · 3.08 KB
/
api.service.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
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Injector } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { Logger } from 'app/shared/logger.service';
import { ApiService } from './api.service';
describe('ApiService', () => {
let injector: Injector;
let service: ApiService;
let httpMock: HttpTestingController;
beforeEach(() => {
injector = TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
ApiService,
{ provide: Logger, useClass: TestLogger }
]
});
service = injector.get(ApiService);
httpMock = injector.get(HttpTestingController);
});
afterEach(() => httpMock.verify());
it('should not immediately connect to the server', () => {
httpMock.expectNone({});
});
it('subscribers should be completed/unsubscribed when service destroyed', () => {
let completed = false;
service.sections.subscribe(
undefined,
undefined,
() => completed = true
);
service.ngOnDestroy();
expect(completed).toBe(true);
// Stop `httpMock.verify()` from complaining.
httpMock.expectOne({});
});
describe('#sections', () => {
it('first subscriber should fetch sections', done => {
const data = [{name: 'a', title: 'A', items: []}, {name: 'b', title: 'B', items: []}];
service.sections.subscribe(sections => {
expect(sections).toEqual(data);
done();
});
httpMock.expectOne({}).flush(data);
});
it('second subscriber should get previous sections and NOT trigger refetch', done => {
const data = [{name: 'a', title: 'A', items: []}, {name: 'b', title: 'B', items: []}];
let subscriptions = 0;
service.sections.subscribe(sections => {
subscriptions++;
expect(sections).toEqual(data);
});
service.sections.subscribe(sections => {
subscriptions++;
expect(sections).toEqual(data);
expect(subscriptions).toBe(2);
done();
});
httpMock.expectOne({}).flush(data);
});
});
describe('#fetchSections', () => {
it('should connect to the server w/ expected URL', () => {
service.fetchSections();
httpMock.expectOne('generated/docs/api/api-list.json');
});
it('should refresh the #sections observable w/ new content on second call', () => {
let call = 0;
let data = [{name: 'a', title: 'A', items: []}, {name: 'b', title: 'B', items: []}];
service.sections.subscribe(sections => {
// called twice during this test
// (1) during subscribe
// (2) after refresh
expect(sections).toEqual(data, 'call ' + call++);
});
httpMock.expectOne({}).flush(data);
// refresh/refetch
data = [{name: 'c', title: 'C', items: []}];
service.fetchSections();
httpMock.expectOne({}).flush(data);
expect(call).toBe(2, 'should be called twice');
});
});
});
class TestLogger {
log = jasmine.createSpy('log');
error = jasmine.createSpy('error');
}