-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.service.spec.ts
More file actions
189 lines (160 loc) · 5.99 KB
/
example.service.spec.ts
File metadata and controls
189 lines (160 loc) · 5.99 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { TestBed } from '@angular/core/testing';
import { anything, deepEqual, instance, mock, spy, verify, when } from 'ts-mockito';
import { BehaviorSubject } from 'rxjs';
import { hot } from 'jasmine-marbles';
import { ExampleService } from './example.service';
import { ProviderOne } from './provider-one.service';
import { ProviderTwo } from './provider-two.service';
describe('ExampleService', () => {
let service: ExampleService;
let serviceSpy: ExampleService;
let providerOneMock: ProviderOne;
let providerTwoMock: ProviderTwo;
const providerOneObservable$ = new BehaviorSubject('mockedProviderOneObservable');
const providerTwoObservable$ = new BehaviorSubject('mockedProviderTwoObservable');
beforeEach(() => {
providerOneMock = mock(ProviderOne);
providerTwoMock = mock(ProviderTwo);
TestBed.configureTestingModule({
providers: [
{ provide: ProviderOne, useValue: instance(providerOneMock) },
{ provide: ProviderTwo, useValue: instance(providerTwoMock) },
],
});
when(providerOneMock.observable$).thenReturn(providerOneObservable$);
when(providerTwoMock.observable$).thenReturn(providerTwoObservable$);
service = TestBed.inject(ExampleService);
serviceSpy = spy(service);
when(serviceSpy.methodOne(anything())).thenReturn({ foo: 'bar' });
});
describe('spying and mocking on service under test', () => {
it('should spy on ExampleService methods', () => {
service.methodTwo({ some: 'value' });
verify(
// ---------- Used to spy on arguments and calls
serviceSpy.methodOne(
deepEqual({
some: 'value',
}),
),
).once();
});
it('should return mocked value', () => {
const expected = service.methodOne({
some: 'value',
});
expect(expected).toEqual({ foo: 'bar' });
});
it('should not mock spied service observable - marbles', () => {
expect(service.observable$).toBeObservable(
hot('a', {
a: 'realObservableOne',
}),
);
});
it('should not mock spied service observable - subscribe', done => {
service.observable$.subscribe(value => {
expect(value).toEqual('realObservableOne');
done();
});
});
});
describe('mocking observables on providers', () => {
describe('first provider', () => {
it('should mock out observable - marbles', () => {
// Access it on the service under test
expect(service.providerOneObservable$).toBeObservable(
hot('a', {
a: 'mockedProviderOneObservable',
}),
);
// Access it on the provider
expect(service['providerOne'].observable$).toBeObservable(
hot('a', {
a: 'mockedProviderOneObservable',
}),
);
})
it('should mock out observable - subscribe', done => {
// Access it on the service under test
service.providerOneObservable$.subscribe(value => {
expect(value).toEqual('mockedProviderOneObservable');
done();
});
});
});
describe('second provider', () => {
it('should mock out observable - marbles', () => {
// Access it on the service under test
expect(service.providerTwoObservable$).toBeObservable(
hot('a', {
a: 'mockedProviderTwoObservable',
}),
);
// Access it on the provider
expect(service['providerTwo'].observable$).toBeObservable(
hot('a', {
a: 'mockedProviderTwoObservable',
}),
);
})
it('should mock out observable - subscribe', done => {
// Access it on the service under test
service.providerTwoObservable$.subscribe(value => {
expect(value).toEqual('mockedProviderTwoObservable');
done();
});
});
});
});
describe('mocking combineLatest observables on service under test', () => {
it('should not mock out rxjs combineLatest', () => {
// Prove that combineLatest is not mocked
const expected = `function combineLatest() {
var observables = [];
for (var _i = 0; _i < arguments.length; _i++) {
observables[_i] = arguments[_i];
}
var resultSelector = undefined;
var scheduler = undefined;
if (isScheduler_1.isScheduler(observables[observables.length - 1])) {
scheduler = observables.pop();
}
if (typeof observables[observables.length - 1] === 'function') {
resultSelector = observables.pop();
}
if (observables.length === 1 && isArray_1.isArray(observables[0])) {
observables = observables[0];
}
return fromArray_1.fromArray(observables, scheduler).lift(new CombineLatestOperator(resultSelector));
}`;
expect(service.whatIsCombineLatest).toEqual(expected);
});
it('should mock out providers with combineLatest - subscribe', done => {
// The providers have mock values set in beforeEach, so the combinedProviderObservable$
// should return those values
service.combinedProviderObservable$.subscribe(value => {
expect(value).toEqual('mockedProviderOneObservable/mockedProviderTwoObservable');
done();
});
});
it('should not mock for local observables in combineLatest - subscribe', done => {
// Spying on the service under test should not replace the
// observable$ and observableTwo$ class fields
// See 'should not mock spied service observable - subscribe', which passes
service.localCombinedObservable$.subscribe(value => {
expect(value).toEqual('realObservableOne/realObservableTwo');
done();
});
});
it('should not mock for local observables in combineLatest with no pipe', () => {
// This test is to determine whether it is combineLatest breaking the test,
// or the pipe(map())
expect(service.localCombinedObservableNoPipe$).toBeObservable(
hot('a', {
a: [ 'realObservableOne', 'realObservableTwo' ],
}),
);
});
});
});