@@ -39,9 +39,10 @@ describe('Scheduler.asap', () => {
39
39
40
40
it ( 'should cancel asap actions when delay > 0' , ( ) => {
41
41
testScheduler . run ( ( { cold, expectObservable, flush, time } ) => {
42
- const setImmediateSpy = sinon . spy ( immediateProvider , 'setImmediate' ) ;
43
- const setSpy = sinon . spy ( intervalProvider , 'setInterval' ) ;
44
- const clearSpy = sinon . spy ( intervalProvider , 'clearInterval' ) ;
42
+ const sandbox = sinon . createSandbox ( ) ;
43
+ const setImmediateSpy = sandbox . spy ( immediateProvider , 'setImmediate' ) ;
44
+ const setSpy = sandbox . spy ( intervalProvider , 'setInterval' ) ;
45
+ const clearSpy = sandbox . spy ( intervalProvider , 'clearInterval' ) ;
45
46
46
47
const a = cold ( ' a ' ) ;
47
48
const ta = time ( ' ----| ' ) ;
@@ -57,17 +58,15 @@ describe('Scheduler.asap', () => {
57
58
expect ( setImmediateSpy ) . to . have . not . been . called ;
58
59
expect ( setSpy ) . to . have . been . calledOnce ;
59
60
expect ( clearSpy ) . to . have . been . calledOnce ;
60
- setImmediateSpy . restore ( ) ;
61
- setSpy . restore ( ) ;
62
- clearSpy . restore ( ) ;
61
+ sandbox . restore ( ) ;
63
62
} ) ;
64
63
} ) ;
65
64
66
65
it ( 'should reuse the interval for recursively scheduled actions with the same delay' , ( ) => {
67
66
const sandbox = sinon . createSandbox ( ) ;
68
67
const fakeTimer = sandbox . useFakeTimers ( ) ;
69
68
// callThrough is missing from the declarations installed by the typings tool in stable
70
- const stubSetInterval = ( < any > sinon . stub ( global , 'setInterval' ) ) . callThrough ( ) ;
69
+ const stubSetInterval = ( < any > sandbox . stub ( global , 'setInterval' ) ) . callThrough ( ) ;
71
70
const period = 50 ;
72
71
const state = { index : 0 , period } ;
73
72
type State = typeof state ;
@@ -86,15 +85,14 @@ describe('Scheduler.asap', () => {
86
85
fakeTimer . tick ( period ) ;
87
86
expect ( state ) . to . have . property ( 'index' , 2 ) ;
88
87
expect ( stubSetInterval ) . to . have . property ( 'callCount' , 1 ) ;
89
- stubSetInterval . restore ( ) ;
90
88
sandbox . restore ( ) ;
91
89
} ) ;
92
90
93
91
it ( 'should not reuse the interval for recursively scheduled actions with a different delay' , ( ) => {
94
92
const sandbox = sinon . createSandbox ( ) ;
95
93
const fakeTimer = sandbox . useFakeTimers ( ) ;
96
94
// callThrough is missing from the declarations installed by the typings tool in stable
97
- const stubSetInterval = ( < any > sinon . stub ( global , 'setInterval' ) ) . callThrough ( ) ;
95
+ const stubSetInterval = ( < any > sandbox . stub ( global , 'setInterval' ) ) . callThrough ( ) ;
98
96
const period = 50 ;
99
97
const state = { index : 0 , period } ;
100
98
type State = typeof state ;
@@ -114,7 +112,6 @@ describe('Scheduler.asap', () => {
114
112
fakeTimer . tick ( period ) ;
115
113
expect ( state ) . to . have . property ( 'index' , 2 ) ;
116
114
expect ( stubSetInterval ) . to . have . property ( 'callCount' , 3 ) ;
117
- stubSetInterval . restore ( ) ;
118
115
sandbox . restore ( ) ;
119
116
} ) ;
120
117
@@ -221,4 +218,74 @@ describe('Scheduler.asap', () => {
221
218
} , 0 , 0 ) ;
222
219
scheduledIndices . push ( 0 ) ;
223
220
} ) ;
221
+
222
+ it ( 'should execute actions scheduled when flushing in a subsequent flush' , ( done ) => {
223
+ const sandbox = sinon . createSandbox ( ) ;
224
+ const stubFlush = ( sandbox . stub ( asapScheduler , 'flush' ) ) . callThrough ( ) ;
225
+
226
+ let a : Subscription ;
227
+ let b : Subscription ;
228
+ let c : Subscription ;
229
+
230
+ a = asapScheduler . schedule ( ( ) => {
231
+ expect ( stubFlush ) . to . have . callCount ( 1 ) ;
232
+ c = asapScheduler . schedule ( ( ) => {
233
+ expect ( stubFlush ) . to . have . callCount ( 2 ) ;
234
+ sandbox . restore ( ) ;
235
+ done ( ) ;
236
+ } ) ;
237
+ } ) ;
238
+ b = asapScheduler . schedule ( ( ) => {
239
+ expect ( stubFlush ) . to . have . callCount ( 1 ) ;
240
+ } ) ;
241
+ } ) ;
242
+
243
+ it ( 'should execute actions scheduled when flushing in a subsequent flush when some actions are unsubscribed' , ( done ) => {
244
+ const sandbox = sinon . createSandbox ( ) ;
245
+ const stubFlush = ( sandbox . stub ( asapScheduler , 'flush' ) ) . callThrough ( ) ;
246
+
247
+ let a : Subscription ;
248
+ let b : Subscription ;
249
+ let c : Subscription ;
250
+
251
+ a = asapScheduler . schedule ( ( ) => {
252
+ expect ( stubFlush ) . to . have . callCount ( 1 ) ;
253
+ c = asapScheduler . schedule ( ( ) => {
254
+ expect ( stubFlush ) . to . have . callCount ( 2 ) ;
255
+ sandbox . restore ( ) ;
256
+ done ( ) ;
257
+ } ) ;
258
+ b . unsubscribe ( ) ;
259
+ } ) ;
260
+ b = asapScheduler . schedule ( ( ) => {
261
+ done ( new Error ( 'Unexpected execution of b' ) ) ;
262
+ } ) ;
263
+ } ) ;
264
+
265
+ it ( 'should properly cancel an unnecessary flush' , ( done ) => {
266
+ const sandbox = sinon . createSandbox ( ) ;
267
+ const clearImmediateStub = sandbox . stub ( immediateProvider , 'clearImmediate' ) . callThrough ( ) ;
268
+
269
+ let a : Subscription ;
270
+ let b : Subscription ;
271
+ let c : Subscription ;
272
+
273
+ a = asapScheduler . schedule ( ( ) => {
274
+ expect ( asapScheduler . actions ) . to . have . length ( 1 ) ;
275
+ c = asapScheduler . schedule ( ( ) => {
276
+ done ( new Error ( 'Unexpected execution of c' ) ) ;
277
+ } ) ;
278
+ expect ( asapScheduler . actions ) . to . have . length ( 2 ) ;
279
+ // What we're testing here is that the unsubscription of action c effects
280
+ // the cancellation of the microtask in a scenario in which the actions
281
+ // queue is not empty - it contains action b.
282
+ c . unsubscribe ( ) ;
283
+ expect ( asapScheduler . actions ) . to . have . length ( 1 ) ;
284
+ expect ( clearImmediateStub ) . to . have . callCount ( 1 ) ;
285
+ } ) ;
286
+ b = asapScheduler . schedule ( ( ) => {
287
+ sandbox . restore ( ) ;
288
+ done ( ) ;
289
+ } ) ;
290
+ } ) ;
224
291
} ) ;
0 commit comments