-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathshareReplay-spec.ts
210 lines (180 loc) · 8.6 KB
/
shareReplay-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
import { expect } from 'chai';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { shareReplay, mergeMapTo, retry, take } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { Observable, interval, Operator, Observer } from 'rxjs';
declare function asDiagram(arg: string): Function;
declare const rxTestScheduler: TestScheduler;
/** @test {shareReplay} */
describe('shareReplay operator', () => {
it('should mirror a simple source Observable', () => {
const source = cold('--1-2---3-4--5-|');
const sourceSubs = '^ !';
const published = source.pipe(shareReplay());
const expected = '--1-2---3-4--5-|';
expectObservable(published).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should do nothing if result is not subscribed', () => {
let subscribed = false;
const source = new Observable(() => {
subscribed = true;
});
source.pipe(shareReplay());
expect(subscribed).to.be.false;
});
it('should multicast the same values to multiple observers, bufferSize=1', () => {
const source = cold('-1-2-3----4-|'); const shared = source.pipe(shareReplay(1));
const sourceSubs = '^ !';
const subscriber1 = hot('a| ').pipe(mergeMapTo(shared));
const expected1 = '-1-2-3----4-|';
const subscriber2 = hot(' b| ').pipe(mergeMapTo(shared));
const expected2 = ' 23----4-|';
const subscriber3 = hot(' c| ').pipe(mergeMapTo(shared));
const expected3 = ' 3-4-|';
expectObservable(subscriber1).toBe(expected1);
expectObservable(subscriber2).toBe(expected2);
expectObservable(subscriber3).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should multicast the same values to multiple observers, bufferSize=2', () => {
const source = cold('-1-2-----3------4-|'); const shared = source.pipe(shareReplay(2));
const sourceSubs = '^ !';
const subscriber1 = hot('a| ').pipe(mergeMapTo(shared));
const expected1 = '-1-2-----3------4-|';
const subscriber2 = hot(' b| ').pipe(mergeMapTo(shared));
const expected2 = ' (12)-3------4-|';
const subscriber3 = hot(' c| ').pipe(mergeMapTo(shared));
const expected3 = ' (23)-4-|';
expectObservable(subscriber1).toBe(expected1);
expectObservable(subscriber2).toBe(expected2);
expectObservable(subscriber3).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should multicast an error from the source to multiple observers', () => {
const source = cold('-1-2-3----4-#'); const shared = source.pipe(shareReplay(1));
const sourceSubs = '^ !';
const subscriber1 = hot('a| ').pipe(mergeMapTo(shared));
const expected1 = '-1-2-3----4-#';
const subscriber2 = hot(' b| ').pipe(mergeMapTo(shared));
const expected2 = ' 23----4-#';
const subscriber3 = hot(' c| ').pipe(mergeMapTo(shared));
const expected3 = ' 3-4-#';
expectObservable(subscriber1).toBe(expected1);
expectObservable(subscriber2).toBe(expected2);
expectObservable(subscriber3).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should multicast an empty source', () => {
const source = cold('|');
const sourceSubs = '(^!)';
const shared = source.pipe(shareReplay(1));
const expected = '|';
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should multicast a never source', () => {
const source = cold('-');
const sourceSubs = '^';
const shared = source.pipe(shareReplay(1));
const expected = '-';
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should multicast a throw source', () => {
const source = cold('#');
const sourceSubs = '(^!)';
const shared = source.pipe(shareReplay(1));
const expected = '#';
expectObservable(shared).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should replay results to subsequent subscriptions if source completes, bufferSize=2', () => {
const source = cold('-1-2-----3-| ');
const shared = source.pipe(shareReplay(2));
const sourceSubs = '^ ! ';
const subscriber1 = hot('a| ').pipe(mergeMapTo(shared));
const expected1 = '-1-2-----3-| ';
const subscriber2 = hot(' b| ').pipe(mergeMapTo(shared));
const expected2 = ' (12)-3-| ';
const subscriber3 = hot(' (c|) ').pipe(mergeMapTo(shared));
const expected3 = ' (23|)';
expectObservable(subscriber1).toBe(expected1);
expectObservable(subscriber2).toBe(expected2);
expectObservable(subscriber3).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
});
it('should completely restart for subsequent subscriptions if source errors, bufferSize=2', () => {
const source = cold('-1-2-----3-# ');
const shared = source.pipe(shareReplay(2));
const sourceSubs1 = '^ ! ';
const subscriber1 = hot('a| ').pipe(mergeMapTo(shared));
const expected1 = '-1-2-----3-# ';
const subscriber2 = hot(' b| ').pipe(mergeMapTo(shared));
const expected2 = ' (12)-3-# ';
const subscriber3 = hot(' (c|) ').pipe(mergeMapTo(shared));
const expected3 = ' -1-2-----3-#';
const sourceSubs2 = ' ^ !';
expectObservable(subscriber1).toBe(expected1);
expectObservable(subscriber2).toBe(expected2);
expectObservable(subscriber3).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe([sourceSubs1, sourceSubs2]);
});
it('should be retryable, bufferSize=2', () => {
const subs = [];
const source = cold('-1-2-----3-# ');
const shared = source.pipe(shareReplay(2), retry(1));
subs.push( '^ ! ');
subs.push( ' ^ ! ');
subs.push( ' ^ !');
const subscriber1 = hot('a| ').pipe(mergeMapTo(shared));
const expected1 = '-1-2-----3--1-2-----3-# ';
const subscriber2 = hot(' b| ').pipe(mergeMapTo(shared));
const expected2 = ' (12)-3--1-2-----3-# ';
const subscriber3 = hot(' (c|) ').pipe(mergeMapTo(shared));
const expected3 = ' (12)-3--1-2-----3-#';
expectObservable(subscriber1).toBe(expected1);
expectObservable(subscriber2).toBe(expected2);
expectObservable(subscriber3).toBe(expected3);
expectSubscriptions(source.subscriptions).toBe(subs);
});
it('should not restart if refCount hits 0 due to unsubscriptions', () => {
const results: number[] = [];
const source = interval(10, rxTestScheduler).pipe(
take(10),
shareReplay(1)
);
const subs = source.subscribe(x => results.push(x));
rxTestScheduler.schedule(() => subs.unsubscribe(), 35);
rxTestScheduler.schedule(() => source.subscribe(x => results.push(x)), 54);
rxTestScheduler.flush();
expect(results).to.deep.equal([0, 1, 2, 4, 5, 6, 7, 8, 9]);
});
it('should not break lift() composability', (done: MochaDone) => {
class MyCustomObservable<T> extends Observable<T> {
lift<R>(operator: Operator<T, R>): Observable<R> {
const observable = new MyCustomObservable<R>();
(<any>observable).source = this;
(<any>observable).operator = operator;
return observable;
}
}
const result = new MyCustomObservable((observer: Observer<number>) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
}).pipe(shareReplay());
expect(result instanceof MyCustomObservable).to.be.true;
const expected = [1, 2, 3];
result
.subscribe((n: any) => {
expect(expected.length).to.be.greaterThan(0);
expect(n).to.equal(expected.shift());
}, (x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
});