1
+ /** @prettier */
1
2
import { expect } from 'chai' ;
2
- import { cold , expectObservable , expectSubscriptions } from '../helpers/marble-testing' ;
3
3
import { repeat , mergeMap , map , multicast , refCount , take } from 'rxjs/operators' ;
4
4
import { TestScheduler } from 'rxjs/testing' ;
5
- import { of , Subject , Observable } from 'rxjs' ;
6
-
7
- declare const rxTestScheduler : TestScheduler ;
5
+ import { of , Subject , Observable , timer } from 'rxjs' ;
6
+ import { observableMatcher } from '../helpers/observableMatcher' ;
8
7
9
8
/** @test {repeat} */
10
9
describe ( 'repeat operator' , ( ) => {
10
+ let rxTest : TestScheduler ;
11
+
12
+ beforeEach ( ( ) => {
13
+ rxTest = new TestScheduler ( observableMatcher ) ;
14
+ } ) ;
15
+
11
16
it ( 'should resubscribe count number of times' , ( ) => {
12
- const e1 = cold ( '--a--b--| ' ) ;
13
- const subs = [ '^ ! ' ,
14
- ' ^ ! ' ,
15
- ' ^ !' ] ;
16
- const expected = '--a--b----a--b----a--b--|' ;
17
-
18
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
19
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
17
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
18
+ const e1 = cold ( ' --a--b--| ' ) ;
19
+ const subs = [
20
+ ' ^-------! ' , //
21
+ ' --------^-------! ' ,
22
+ ' ----------------^-------!' ,
23
+ ] ;
24
+ const expected = '--a--b----a--b----a--b--|' ;
25
+
26
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
27
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
28
+ } ) ;
20
29
} ) ;
21
30
22
31
it ( 'should resubscribe multiple times' , ( ) => {
23
- const e1 = cold ( '--a--b--| ' ) ;
24
- const subs = [ '^ ! ' ,
25
- ' ^ ! ' ,
26
- ' ^ ! ' ,
27
- ' ^ !' ] ;
28
- const expected = '--a--b----a--b----a--b----a--b--|' ;
29
-
30
- expectObservable ( e1 . pipe ( repeat ( 2 ) , repeat ( 2 ) ) ) . toBe ( expected ) ;
31
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
32
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
33
+ const e1 = cold ( ' --a--b--| ' ) ;
34
+ const subs = [
35
+ ' ^-------! ' ,
36
+ ' --------^-------! ' ,
37
+ ' ----------------^-------! ' ,
38
+ ' ------------------------^-------!' ,
39
+ ] ;
40
+ const expected = '--a--b----a--b----a--b----a--b--|' ;
41
+
42
+ expectObservable ( e1 . pipe ( repeat ( 2 ) , repeat ( 2 ) ) ) . toBe ( expected ) ;
43
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
44
+ } ) ;
32
45
} ) ;
33
46
34
47
it ( 'should complete without emit when count is zero' , ( ) => {
35
- const e1 = cold ( '--a--b--|' ) ;
36
- const subs : string [ ] = [ ] ;
37
- const expected = '|' ;
48
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
49
+ const e1 = cold ( '--a--b--|' ) ;
50
+ const subs : string [ ] = [ ] ;
51
+ const expected = '|' ;
38
52
39
- expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
40
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
53
+ expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
54
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
55
+ } ) ;
41
56
} ) ;
42
57
43
58
it ( 'should emit source once when count is one' , ( ) => {
44
- const e1 = cold ( '--a--b--|' ) ;
45
- const subs = '^ !' ;
46
- const expected = '--a--b--|' ;
59
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
60
+ const e1 = cold ( ' --a--b--|' ) ;
61
+ const subs = ' ^-------!' ;
62
+ const expected = '--a--b--|' ;
47
63
48
- expectObservable ( e1 . pipe ( repeat ( 1 ) ) ) . toBe ( expected ) ;
49
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
64
+ expectObservable ( e1 . pipe ( repeat ( 1 ) ) ) . toBe ( expected ) ;
65
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
66
+ } ) ;
50
67
} ) ;
51
68
52
69
it ( 'should repeat until gets unsubscribed' , ( ) => {
53
- const e1 = cold ( '--a--b--| ' ) ;
54
- const subs = [ '^ ! ' ,
55
- ' ^ !' ] ;
56
- const unsub = ' !' ;
57
- const expected = '--a--b----a--b-' ;
58
-
59
- expectObservable ( e1 . pipe ( repeat ( 10 ) ) , unsub ) . toBe ( expected ) ;
60
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
70
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
71
+ const e1 = cold ( ' --a--b--| ' ) ;
72
+ const subs = [
73
+ ' ^-------! ' , //
74
+ ' --------^------!' ,
75
+ ] ;
76
+ const unsub = ' ---------------!' ;
77
+ const expected = '--a--b----a--b-' ;
78
+
79
+ expectObservable ( e1 . pipe ( repeat ( 10 ) ) , unsub ) . toBe ( expected ) ;
80
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
81
+ } ) ;
61
82
} ) ;
62
83
63
84
it ( 'should be able to repeat indefinitely until unsubscribed' , ( ) => {
64
- const e1 = cold ( '--a--b--| ' ) ;
65
- const subs = [ '^ ! ' ,
66
- ' ^ ! ' ,
67
- ' ^ ! ' ,
68
- ' ^ ! ' ,
69
- ' ^ ! ' ,
70
- ' ^ !' ] ;
71
- const unsub = ' !' ;
72
- const expected = '--a--b----a--b----a--b----a--b----a--b----a--' ;
73
-
74
- expectObservable ( e1 . pipe ( repeat ( ) ) , unsub ) . toBe ( expected ) ;
75
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
85
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
86
+ const e1 = cold ( ' --a--b--| ' ) ;
87
+ const subs = [
88
+ ' ^-------! ' ,
89
+ ' --------^-------! ' ,
90
+ ' ----------------^-------! ' ,
91
+ ' ------------------------^-------! ' ,
92
+ ' --------------------------------^-------! ' ,
93
+ ' ----------------------------------------^---!' ,
94
+ ] ;
95
+ const unsub = ' --------------------------------------------!' ;
96
+ const expected = '--a--b----a--b----a--b----a--b----a--b----a--' ;
97
+
98
+ expectObservable ( e1 . pipe ( repeat ( ) ) , unsub ) . toBe ( expected ) ;
99
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
100
+ } ) ;
76
101
} ) ;
77
102
78
103
it ( 'should not break unsubscription chain when unsubscribed explicitly' , ( ) => {
79
- const e1 = cold ( '--a--b--| ' ) ;
80
- const subs = [ '^ ! ' ,
81
- ' ^ ! ' ,
82
- ' ^ ! ' ,
83
- ' ^ ! ' ,
84
- ' ^ ! ' ,
85
- ' ^ !' ] ;
86
- const unsub = ' !' ;
87
- const expected = '--a--b----a--b----a--b----a--b----a--b----a--' ;
88
-
89
- const result = e1 . pipe (
90
- mergeMap ( ( x : string ) => of ( x ) ) ,
91
- repeat ( ) ,
92
- mergeMap ( ( x : string ) => of ( x ) )
93
- ) ;
94
-
95
- expectObservable ( result , unsub ) . toBe ( expected ) ;
96
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
104
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
105
+ const e1 = cold ( ' --a--b--| ' ) ;
106
+ const subs = [
107
+ ' ^-------! ' ,
108
+ ' --------^-------! ' ,
109
+ ' ----------------^-------! ' ,
110
+ ' ------------------------^-------! ' ,
111
+ ' --------------------------------^-------! ' ,
112
+ ' ----------------------------------------^---!' ,
113
+ ] ;
114
+ const unsub = ' --------------------------------------------!' ;
115
+ const expected = '--a--b----a--b----a--b----a--b----a--b----a--' ;
116
+
117
+ const result = e1 . pipe (
118
+ mergeMap ( ( x : string ) => of ( x ) ) ,
119
+ repeat ( ) ,
120
+ mergeMap ( ( x : string ) => of ( x ) )
121
+ ) ;
122
+
123
+ expectObservable ( result , unsub ) . toBe ( expected ) ;
124
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
125
+ } ) ;
97
126
} ) ;
98
127
99
128
it ( 'should consider negative count as no repeat, and return EMPTY' , ( ) => {
100
- const e1 = cold ( '--a--b--| ' ) ;
101
- const unsub = ' !' ;
102
- const expected = '|' ;
129
+ rxTest . run ( ( { cold, expectObservable , expectSubscriptions } ) => {
130
+ const e1 = cold ( '--a--b--| ' ) ;
131
+ const expected = '|' ;
103
132
104
- expectObservable ( e1 . pipe ( repeat ( - 1 ) ) , unsub ) . toBe ( expected ) ;
105
- expectSubscriptions ( e1 . subscriptions ) . toBe ( [ ] ) ;
133
+ expectObservable ( e1 . pipe ( repeat ( - 1 ) ) ) . toBe ( expected ) ;
134
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( [ ] ) ;
135
+ } ) ;
106
136
} ) ;
107
137
108
138
it ( 'should always teardown before starting the next cycle' , async ( ) => {
109
139
const results : any [ ] = [ ] ;
110
- const source = new Observable < number > ( subscriber => {
140
+ const source = new Observable < number > ( ( subscriber ) => {
111
141
Promise . resolve ( ) . then ( ( ) => {
112
- subscriber . next ( 1 )
142
+ subscriber . next ( 1 ) ;
113
143
Promise . resolve ( ) . then ( ( ) => {
114
144
subscriber . next ( 2 ) ;
115
145
Promise . resolve ( ) . then ( ( ) => {
@@ -119,171 +149,205 @@ describe('repeat operator', () => {
119
149
} ) ;
120
150
return ( ) => {
121
151
results . push ( 'teardown' ) ;
122
- }
152
+ } ;
123
153
} ) ;
124
154
125
- await source . pipe ( repeat ( 3 ) ) . forEach ( value => results . push ( value ) ) ;
126
-
127
- expect ( results ) . to . deep . equal ( [ 1 , 2 , 'teardown' , 1 , 2 , 'teardown' , 1 , 2 , 'teardown' ] )
155
+ await source . pipe ( repeat ( 3 ) ) . forEach ( ( value ) => results . push ( value ) ) ;
156
+
157
+ expect ( results ) . to . deep . equal ( [ 1 , 2 , 'teardown' , 1 , 2 , 'teardown' , 1 , 2 , 'teardown' ] ) ;
128
158
} ) ;
129
159
130
160
it ( 'should always teardown before starting the next cycle, even when synchronous' , ( ) => {
131
161
const results : any [ ] = [ ] ;
132
- const source = new Observable < number > ( subscriber => {
162
+ const source = new Observable < number > ( ( subscriber ) => {
133
163
subscriber . next ( 1 ) ;
134
164
subscriber . next ( 2 ) ;
135
165
subscriber . complete ( ) ;
136
166
return ( ) => {
137
167
results . push ( 'teardown' ) ;
138
- }
168
+ } ;
139
169
} ) ;
140
170
const subscription = source . pipe ( repeat ( 3 ) ) . subscribe ( {
141
- next : value => results . push ( value ) ,
142
- complete : ( ) => results . push ( 'complete' )
171
+ next : ( value ) => results . push ( value ) ,
172
+ complete : ( ) => results . push ( 'complete' ) ,
143
173
} ) ;
144
174
145
175
expect ( subscription . closed ) . to . be . true ;
146
- expect ( results ) . to . deep . equal ( [ 1 , 2 , 'teardown' , 1 , 2 , 'teardown' , 1 , 2 , 'complete' , 'teardown' ] )
176
+ expect ( results ) . to . deep . equal ( [ 1 , 2 , 'teardown' , 1 , 2 , 'teardown' , 1 , 2 , 'complete' , 'teardown' ] ) ;
147
177
} ) ;
148
178
149
179
it ( 'should not complete when source never completes' , ( ) => {
150
- const e1 = cold ( '-' ) ;
151
- const e1subs = '^' ;
152
- const expected = '-' ;
180
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
181
+ const e1 = cold ( '-' ) ;
182
+ const e1subs = '^' ;
183
+ const expected = '-' ;
153
184
154
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
155
- expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
185
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
186
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
187
+ } ) ;
156
188
} ) ;
157
189
158
190
it ( 'should not complete when source does not completes' , ( ) => {
159
- const e1 = cold ( '-' ) ;
160
- const unsub = ' !' ;
161
- const subs = '^ !' ;
162
- const expected = '-' ;
163
-
164
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) , unsub ) . toBe ( expected ) ;
165
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
191
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
192
+ const e1 = cold ( '-' ) ;
193
+ const unsub = '------------------------------!' ;
194
+ const subs = ' ^-----------------------------!' ;
195
+ const expected = '-' ;
196
+
197
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) , unsub ) . toBe ( expected ) ;
198
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
199
+ } ) ;
166
200
} ) ;
167
201
168
202
it ( 'should complete immediately when source does not complete without emit but count is zero' , ( ) => {
169
- const e1 = cold ( '-' ) ;
170
- const subs : string [ ] = [ ] ;
171
- const expected = '|' ;
203
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
204
+ const e1 = cold ( '-' ) ;
205
+ const subs : string [ ] = [ ] ;
206
+ const expected = '|' ;
172
207
173
- expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
174
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
208
+ expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
209
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
210
+ } ) ;
175
211
} ) ;
176
212
177
213
it ( 'should complete immediately when source does not complete but count is zero' , ( ) => {
178
- const e1 = cold ( '--a--b--' ) ;
179
- const subs : string [ ] = [ ] ;
180
- const expected = '|' ;
214
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
215
+ const e1 = cold ( '--a--b--' ) ;
216
+ const subs : string [ ] = [ ] ;
217
+ const expected = '|' ;
181
218
182
- expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
183
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
219
+ expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
220
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
221
+ } ) ;
184
222
} ) ;
185
223
186
224
it ( 'should emit source once and does not complete when source emits but does not complete' , ( ) => {
187
- const e1 = cold ( '--a--b--' ) ;
188
- const subs = [ '^ ' ] ;
189
- const expected = '--a--b--' ;
225
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
226
+ const e1 = cold ( ' --a--b--' ) ;
227
+ const subs = [ ' ^-------' ] ;
228
+ const expected = '--a--b--' ;
190
229
191
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
192
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
230
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
231
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
232
+ } ) ;
193
233
} ) ;
194
234
195
235
it ( 'should complete when source is empty' , ( ) => {
196
- const e1 = cold ( '|' ) ;
197
- const e1subs = [ '(^!)' , '(^!)' , '(^!)' ] ;
198
- const expected = '|' ;
236
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
237
+ const e1 = cold ( '|' ) ;
238
+ const e1subs = [ '(^!)' , '(^!)' , '(^!)' ] ;
239
+ const expected = '|' ;
199
240
200
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
201
- expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
241
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
242
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
243
+ } ) ;
202
244
} ) ;
203
245
204
246
it ( 'should complete when source does not emit' , ( ) => {
205
- const e1 = cold ( '----| ' ) ;
206
- const subs = [ '^ ! ' ,
207
- ' ^ ! ' ,
208
- ' ^ !' ] ;
209
- const expected = '------------|' ;
210
-
211
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
212
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
247
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
248
+ const e1 = cold ( '----| ' ) ;
249
+ const subs = [
250
+ ' ^---! ' , //
251
+ ' ----^---! ' ,
252
+ ' --------^---!' ,
253
+ ] ;
254
+ const expected = '------------|' ;
255
+
256
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
257
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
258
+ } ) ;
213
259
} ) ;
214
260
215
261
it ( 'should complete immediately when source does not emit but count is zero' , ( ) => {
216
- const e1 = cold ( '----|' ) ;
217
- const subs : string [ ] = [ ] ;
218
- const expected = '|' ;
262
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
263
+ const e1 = cold ( '----|' ) ;
264
+ const subs : string [ ] = [ ] ;
265
+ const expected = '|' ;
219
266
220
- expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
221
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
267
+ expectObservable ( e1 . pipe ( repeat ( 0 ) ) ) . toBe ( expected ) ;
268
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
269
+ } ) ;
222
270
} ) ;
223
271
224
272
it ( 'should raise error when source raises error' , ( ) => {
225
- const e1 = cold ( '--a--b--#' ) ;
226
- const subs = '^ !' ;
227
- const expected = '--a--b--#' ;
273
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
274
+ const e1 = cold ( ' --a--b--#' ) ;
275
+ const subs = ' ^-------!' ;
276
+ const expected = '--a--b--#' ;
228
277
229
- expectObservable ( e1 . pipe ( repeat ( 2 ) ) ) . toBe ( expected ) ;
230
- expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
278
+ expectObservable ( e1 . pipe ( repeat ( 2 ) ) ) . toBe ( expected ) ;
279
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
280
+ } ) ;
231
281
} ) ;
232
282
233
283
it ( 'should raises error if source throws' , ( ) => {
234
- const e1 = cold ( '#' ) ;
235
- const e1subs = '(^!)' ;
236
- const expected = '#' ;
284
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
285
+ const e1 = cold ( '#' ) ;
286
+ const e1subs = '(^!)' ;
287
+ const expected = '#' ;
237
288
238
- expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
239
- expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
289
+ expectObservable ( e1 . pipe ( repeat ( 3 ) ) ) . toBe ( expected ) ;
290
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
291
+ } ) ;
240
292
} ) ;
241
293
242
294
it ( 'should raises error if source throws when repeating infinitely' , ( ) => {
243
- const e1 = cold ( '#' ) ;
244
- const e1subs = '(^!)' ;
245
- const expected = '#' ;
295
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
296
+ const e1 = cold ( '#' ) ;
297
+ const e1subs = '(^!)' ;
298
+ const expected = '#' ;
246
299
247
- expectObservable ( e1 . pipe ( repeat ( ) ) ) . toBe ( expected ) ;
248
- expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
300
+ expectObservable ( e1 . pipe ( repeat ( ) ) ) . toBe ( expected ) ;
301
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( e1subs ) ;
302
+ } ) ;
249
303
} ) ;
250
304
251
305
it ( 'should raise error after first emit succeed' , ( ) => {
252
- let repeated = false ;
253
-
254
- const e1 = cold ( '--a--|' ) . pipe ( map ( ( x : string ) => {
255
- if ( repeated ) {
256
- throw 'error' ;
257
- } else {
258
- repeated = true ;
259
- return x ;
260
- }
261
- } ) ) ;
262
- const expected = '--a----#' ;
263
-
264
- expectObservable ( e1 . pipe ( repeat ( 2 ) ) ) . toBe ( expected ) ;
306
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
307
+ let repeated = false ;
308
+
309
+ const e1 = cold ( '--a--|' ) . pipe (
310
+ map ( ( x : string ) => {
311
+ if ( repeated ) {
312
+ throw 'error' ;
313
+ } else {
314
+ repeated = true ;
315
+ return x ;
316
+ }
317
+ } )
318
+ ) ;
319
+ const expected = '--a----#' ;
320
+
321
+ expectObservable ( e1 . pipe ( repeat ( 2 ) ) ) . toBe ( expected ) ;
322
+ } ) ;
265
323
} ) ;
266
324
267
325
it ( 'should repeat a synchronous source (multicasted and refCounted) multiple times' , ( done ) => {
268
326
const expected = [ 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 ] ;
269
327
270
- of ( 1 , 2 , 3 ) . pipe (
271
- multicast ( ( ) => new Subject < number > ( ) ) ,
272
- refCount ( ) ,
273
- repeat ( 5 )
274
- ) . subscribe (
275
- ( x : number ) => { expect ( x ) . to . equal ( expected . shift ( ) ) ; } ,
328
+ of ( 1 , 2 , 3 )
329
+ . pipe (
330
+ multicast ( ( ) => new Subject < number > ( ) ) ,
331
+ refCount ( ) ,
332
+ repeat ( 5 )
333
+ )
334
+ . subscribe (
335
+ ( x : number ) => {
336
+ expect ( x ) . to . equal ( expected . shift ( ) ) ;
337
+ } ,
276
338
( x ) => {
277
339
done ( new Error ( 'should not be called' ) ) ;
278
- } , ( ) => {
340
+ } ,
341
+ ( ) => {
279
342
expect ( expected . length ) . to . equal ( 0 ) ;
280
343
done ( ) ;
281
- } ) ;
344
+ }
345
+ ) ;
282
346
} ) ;
283
347
284
348
it ( 'should stop listening to a synchronous observable when unsubscribed' , ( ) => {
285
349
const sideEffects : number [ ] = [ ] ;
286
- const synchronousObservable = new Observable < number > ( subscriber => {
350
+ const synchronousObservable = new Observable < number > ( ( subscriber ) => {
287
351
// This will check to see if the subscriber was closed on each loop
288
352
// when the unsubscribe hits (from the `take`), it should be closed
289
353
for ( let i = 0 ; ! subscriber . closed && i < 10 ; i ++ ) {
@@ -292,11 +356,98 @@ describe('repeat operator', () => {
292
356
}
293
357
} ) ;
294
358
295
- synchronousObservable . pipe (
296
- repeat ( ) ,
297
- take ( 3 ) ,
298
- ) . subscribe ( ( ) => { /* noop */ } ) ;
359
+ synchronousObservable . pipe ( repeat ( ) , take ( 3 ) ) . subscribe ( ( ) => {
360
+ /* noop */
361
+ } ) ;
299
362
300
363
expect ( sideEffects ) . to . deep . equal ( [ 0 , 1 , 2 ] ) ;
301
364
} ) ;
365
+
366
+ it ( 'should allow count configuration' , ( ) => {
367
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
368
+ const e1 = cold ( ' --a--b--| ' ) ;
369
+ const subs = [
370
+ ' ^-------! ' , //
371
+ ' --------^-------! ' ,
372
+ ' ----------------^-------!' ,
373
+ ] ;
374
+ const expected = '--a--b----a--b----a--b--|' ;
375
+
376
+ expectObservable ( e1 . pipe ( repeat ( { count : 3 } ) ) ) . toBe ( expected ) ;
377
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
378
+ } ) ;
379
+ } ) ;
380
+
381
+ it ( 'should allow delay time configuration' , ( ) => {
382
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
383
+ const e1 = cold ( ' --a--b--| ' ) ;
384
+ const delay = 3 ; // ---| ---|
385
+ const subs = [
386
+ ' ^-------! ' , //
387
+ ' -----------^-------! ' ,
388
+ ' ----------------------^-------!' ,
389
+ ] ;
390
+ const expected = '--a--b-------a--b-------a--b--|' ;
391
+
392
+ expectObservable ( e1 . pipe ( repeat ( { count : 3 , delay } ) ) ) . toBe ( expected ) ;
393
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
394
+ } ) ;
395
+ } ) ;
396
+
397
+ it ( 'should allow delay function configuration' , ( ) => {
398
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
399
+ const expectedCounts = [ 1 , 2 , 3 ] ;
400
+
401
+ const e1 = cold ( ' --a--b--| ' ) ;
402
+ const delay = 3 ; // ---| ---|
403
+ const subs = [
404
+ ' ^-------! ' , //
405
+ ' -----------^-------! ' ,
406
+ ' ----------------------^-------!' ,
407
+ ] ;
408
+ const expected = '--a--b-------a--b-------a--b--|' ;
409
+
410
+ expectObservable (
411
+ e1 . pipe (
412
+ repeat ( {
413
+ count : 3 ,
414
+ delay : ( count ) => {
415
+ expect ( count ) . to . equal ( expectedCounts . shift ( ) ) ;
416
+ return timer ( delay ) ;
417
+ } ,
418
+ } )
419
+ )
420
+ ) . toBe ( expected ) ;
421
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
422
+ } ) ;
423
+ } ) ;
424
+
425
+ it ( 'should handle delay function throwing' , ( ) => {
426
+ rxTest . run ( ( { cold, expectObservable, expectSubscriptions } ) => {
427
+ const expectedCounts = [ 1 , 2 , 3 ] ;
428
+
429
+ const e1 = cold ( ' --a--b--| ' ) ;
430
+ const delay = 3 ; // ---| ---|
431
+ const subs = [
432
+ ' ^-------! ' , //
433
+ ' -----------^-------! ' ,
434
+ ] ;
435
+ const expected = '--a--b-------a--b--#' ;
436
+
437
+ expectObservable (
438
+ e1 . pipe (
439
+ repeat ( {
440
+ count : 3 ,
441
+ delay : ( count ) => {
442
+ if ( count === 2 ) {
443
+ throw 'bad' ;
444
+ }
445
+ return timer ( delay ) ;
446
+ } ,
447
+ } )
448
+ )
449
+ ) . toBe ( expected , undefined , 'bad' ) ;
450
+ expectSubscriptions ( e1 . subscriptions ) . toBe ( subs ) ;
451
+ } ) ;
452
+ } ) ;
302
453
} ) ;
0 commit comments