@@ -8,22 +8,29 @@ import * as FakeTimers from '@sinonjs/fake-timers'
8
8
import * as timeoutUtils from '../../../shared/utilities/timeoutUtils'
9
9
import { installFakeClock , tickPromise } from '../../../test/testUtil'
10
10
import { sleep } from '../../../shared/utilities/timeoutUtils'
11
+ import { SinonStub , SinonSandbox , createSandbox } from 'sinon'
11
12
12
13
// We export this describe() so it can be used in the web tests as well
13
14
export const timeoutUtilsDescribe = describe ( 'timeoutUtils' , async function ( ) {
14
15
let clock : FakeTimers . InstalledClock
16
+ let sandbox : SinonSandbox
15
17
16
18
before ( function ( ) {
17
19
clock = installFakeClock ( )
18
20
} )
19
21
22
+ beforeEach ( function ( ) {
23
+ sandbox = createSandbox ( )
24
+ } )
25
+
20
26
after ( function ( ) {
21
27
clock . uninstall ( )
22
28
} )
23
29
24
30
afterEach ( function ( ) {
25
31
clock . reset ( )
26
32
this . timer ?. dispose ( )
33
+ sandbox . restore ( )
27
34
} )
28
35
29
36
describe ( 'Timeout' , async function ( ) {
@@ -192,6 +199,53 @@ export const timeoutUtilsDescribe = describe('timeoutUtils', async function () {
192
199
} )
193
200
} )
194
201
202
+ describe ( 'Interval' , async function ( ) {
203
+ let interval : timeoutUtils . Interval
204
+ let onCompletionStub : SinonStub
205
+
206
+ beforeEach ( async function ( ) {
207
+ onCompletionStub = sandbox . stub ( )
208
+ interval = new timeoutUtils . Interval ( 1000 , onCompletionStub )
209
+ } )
210
+
211
+ afterEach ( async function ( ) {
212
+ interval ?. dispose ( )
213
+ } )
214
+
215
+ it ( 'Executes the callback on an interval' , async function ( ) {
216
+ await clock . tickAsync ( 999 )
217
+ assert . strictEqual ( onCompletionStub . callCount , 0 )
218
+ await clock . tickAsync ( 1 )
219
+ assert . strictEqual ( onCompletionStub . callCount , 1 )
220
+
221
+ await clock . tickAsync ( 500 )
222
+ assert . strictEqual ( onCompletionStub . callCount , 1 )
223
+ await clock . tickAsync ( 500 )
224
+ assert . strictEqual ( onCompletionStub . callCount , 2 )
225
+
226
+ await clock . tickAsync ( 1000 )
227
+ assert . strictEqual ( onCompletionStub . callCount , 3 )
228
+ } )
229
+
230
+ it ( 'allows to wait for next completion' , async function ( ) {
231
+ clock . uninstall ( )
232
+
233
+ let curr = 'Did Not Change'
234
+
235
+ const realInterval = new timeoutUtils . Interval ( 50 , async ( ) => {
236
+ await sleep ( 50 )
237
+ curr = 'Did Change'
238
+ } )
239
+
240
+ const withoutWait = curr
241
+ await realInterval . nextCompletion ( )
242
+ const withWait = curr
243
+
244
+ assert . strictEqual ( withoutWait , 'Did Not Change' )
245
+ assert . strictEqual ( withWait , 'Did Change' )
246
+ } )
247
+ } )
248
+
195
249
describe ( 'waitUntil' , async function ( ) {
196
250
const testSettings = {
197
251
callCounter : 0 ,
0 commit comments