forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async_pipe_spec.ts
253 lines (211 loc) · 7.76 KB
/
async_pipe_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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AsyncPipe, ɵgetDOM as getDOM} from '@angular/common';
import {ChangeDetectorRef, EventEmitter} from '@angular/core';
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
import {Subscribable, Unsubscribable} from 'rxjs';
{
describe('AsyncPipe', () => {
function getChangeDetectorRefSpy() {
return jasmine.createSpyObj('ChangeDetectorRef', ['markForCheck', 'detectChanges']);
}
describe('Observable', () => {
// only expose methods from the Subscribable interface, to ensure that
// the implementation does not rely on other methods:
const wrapSubscribable = <T>(input: Subscribable<T>): Subscribable<T> => ({
subscribe(...args: any): Unsubscribable {
const subscription = input.subscribe.apply(input, args);
return {
unsubscribe() {
subscription.unsubscribe();
}
};
}
});
let emitter: EventEmitter<any>;
let subscribable: Subscribable<any>;
let pipe: AsyncPipe;
let ref: ChangeDetectorRef&jasmine.SpyObj<ChangeDetectorRef>;
const message = {};
beforeEach(() => {
emitter = new EventEmitter();
subscribable = wrapSubscribable(emitter);
ref = getChangeDetectorRefSpy();
pipe = new AsyncPipe(ref);
});
describe('transform', () => {
it('should return null when subscribing to an observable', () => {
expect(pipe.transform(subscribable)).toBe(null);
});
it('should return the latest available value', done => {
pipe.transform(subscribable);
emitter.emit(message);
setTimeout(() => {
expect(pipe.transform(subscribable)).toEqual(message);
done();
}, 0);
});
it('should return same value when nothing has changed since the last call', done => {
pipe.transform(subscribable);
emitter.emit(message);
setTimeout(() => {
pipe.transform(subscribable);
expect(pipe.transform(subscribable)).toBe(message);
done();
}, 0);
});
it('should dispose of the existing subscription when subscribing to a new observable',
done => {
pipe.transform(subscribable);
const newEmitter = new EventEmitter();
const newSubscribable = wrapSubscribable(newEmitter);
expect(pipe.transform(newSubscribable)).toBe(null);
emitter.emit(message);
// this should not affect the pipe
setTimeout(() => {
expect(pipe.transform(newSubscribable)).toBe(null);
done();
}, 0);
});
it('should request a change detection check upon receiving a new value', done => {
pipe.transform(subscribable);
emitter.emit(message);
setTimeout(() => {
expect(ref.markForCheck).toHaveBeenCalled();
done();
}, 10);
});
it('should return value for unchanged NaN', () => {
emitter.emit(null);
pipe.transform(subscribable);
emitter.next(NaN);
const firstResult = pipe.transform(subscribable);
const secondResult = pipe.transform(subscribable);
expect(firstResult).toBeNaN();
expect(secondResult).toBeNaN();
});
});
describe('ngOnDestroy', () => {
it('should do nothing when no subscription', () => {
expect(() => pipe.ngOnDestroy()).not.toThrow();
});
it('should dispose of the existing subscription', done => {
pipe.transform(subscribable);
pipe.ngOnDestroy();
emitter.emit(message);
setTimeout(() => {
expect(pipe.transform(subscribable)).toBe(null);
done();
}, 0);
});
});
});
describe('Subscribable', () => {
it('should infer the type from the subscribable', () => {
const ref = getChangeDetectorRefSpy();
const pipe = new AsyncPipe(ref);
const emitter = new EventEmitter<{name: 'T'}>();
// The following line will fail to compile if the type cannot be inferred.
const name = pipe.transform(emitter)?.name;
});
});
describe('Promise', () => {
const message = {};
let pipe: AsyncPipe;
let resolve: (result: any) => void;
let reject: (error: any) => void;
let promise: Promise<any>;
let ref: any;
// adds longer timers for passing tests in IE
const timer = (getDOM() && browserDetection.isIE) ? 50 : 10;
beforeEach(() => {
promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
ref = getChangeDetectorRefSpy();
pipe = new AsyncPipe(ref);
});
describe('transform', () => {
it('should return null when subscribing to a promise', () => {
expect(pipe.transform(promise)).toBe(null);
});
it('should return the latest available value', done => {
pipe.transform(promise);
resolve(message);
setTimeout(() => {
expect(pipe.transform(promise)).toEqual(message);
done();
}, timer);
});
it('should return value when nothing has changed since the last call', done => {
pipe.transform(promise);
resolve(message);
setTimeout(() => {
pipe.transform(promise);
expect(pipe.transform(promise)).toBe(message);
done();
}, timer);
});
it('should dispose of the existing subscription when subscribing to a new promise',
done => {
pipe.transform(promise);
promise = new Promise<any>(() => {});
expect(pipe.transform(promise)).toBe(null);
resolve(message);
setTimeout(() => {
expect(pipe.transform(promise)).toBe(null);
done();
}, timer);
});
it('should request a change detection check upon receiving a new value', done => {
pipe.transform(promise);
resolve(message);
setTimeout(() => {
expect(ref.markForCheck).toHaveBeenCalled();
done();
}, timer);
});
describe('ngOnDestroy', () => {
it('should do nothing when no source', () => {
expect(() => pipe.ngOnDestroy()).not.toThrow();
});
it('should dispose of the existing source', done => {
pipe.transform(promise);
expect(pipe.transform(promise)).toBe(null);
resolve(message);
setTimeout(() => {
expect(pipe.transform(promise)).toEqual(message);
pipe.ngOnDestroy();
expect(pipe.transform(promise)).toBe(null);
done();
}, timer);
});
});
});
});
describe('null', () => {
it('should return null when given null', () => {
const pipe = new AsyncPipe(null as any);
expect(pipe.transform(null)).toEqual(null);
});
});
describe('undefined', () => {
it('should return null when given undefined', () => {
const pipe = new AsyncPipe(null as any);
expect(pipe.transform(undefined)).toEqual(null);
});
});
describe('other types', () => {
it('should throw when given an invalid object', () => {
const pipe = new AsyncPipe(null as any);
expect(() => pipe.transform('some bogus object' as any)).toThrowError();
});
});
});
}