@@ -331,42 +331,102 @@ describe('function helpers', () => {
331
331
332
332
describe ( 'poll()' , ( ) => {
333
333
it ( 'should poll indefinitely' , async ( ) => {
334
+ jest . useRealTimers ( ) ;
334
335
const timesToRun = Math . floor ( Math . random ( ) * 10 ) + 1 ;
335
336
let timesRan = 0 ;
336
337
337
- const asyncFunc = jest . fn ( async ( ) => new Promise ( resolve => {
338
+ const asyncFunc = async ( ) => new Promise ( resolve => {
338
339
// eslint-disable-next-line jest/no-conditional-in-test
339
340
if ( timesRan === timesToRun ) {
340
341
throw new Error ( 'Done' ) ;
341
342
}
342
343
343
344
timesRan ++ ;
345
+ resolve ( 'Not yet' ) ;
346
+ } ) ;
347
+
348
+ await func . poll ( asyncFunc ) . catch ( ( ) => { } ) ;
349
+
350
+ expect ( timesRan ) . toBe ( timesToRun ) ;
351
+ } ) ;
352
+
353
+ it ( 'should reject if any attempts fail' , async ( ) => {
354
+ jest . useRealTimers ( ) ;
355
+ const asyncFunc = jest . fn ( async ( ) => new Promise ( ( ) => {
356
+ throw new Error ( 'Done' ) ;
357
+ } ) ) ;
358
+
359
+ await expect ( func . poll ( asyncFunc ) ) . rejects . toThrow ( 'Done' ) ;
360
+ } ) ;
361
+
362
+ it ( 'should wait the specified number of milliseconds' , async ( ) => {
363
+ const start = jest . useFakeTimers ( { advanceTimers : true } ) . now ( ) ;
364
+ let timesRan = 0 ;
365
+ const asyncFunc = jest . fn ( async ( ) => new Promise ( resolve => {
366
+ timesRan ++ ;
367
+
368
+ // eslint-disable-next-line jest/no-conditional-in-test
369
+ if ( timesRan === 2 ) {
370
+ throw new Error ( 'Done' ) ;
371
+ }
372
+
344
373
resolve ( 'Not yet' ) ;
345
374
} ) ) ;
346
375
347
- await func . poll ( asyncFunc , 10 ) ;
348
-
349
- expect ( asyncFunc ) . toHaveBeenCalledTimes ( timesToRun ) ;
350
- } ) ;
351
-
352
- // it('should reject if any attempts fail', () => {
353
- //
354
- // });
355
- //
356
- // it('should wait the specified number of seconds', () => {
357
- //
358
- // });
359
- //
360
- // it('should wait the specified number of seconds returned from the wait function', () => {
361
- //
362
- // });
363
- //
364
- // it('should poll until the given date', () => {
365
- //
366
- // });
367
- //
368
- // it('should poll until the until argument returns true', () => {
369
- //
370
- // });
376
+ await func . poll ( asyncFunc , 100 ) . catch ( ( ) => { } ) ;
377
+
378
+ expect ( asyncFunc ) . toHaveBeenCalledTimes ( 2 ) ;
379
+ expect ( jest . useFakeTimers ( ) . now ( ) - start ) . toBeGreaterThanOrEqual ( 100 ) ;
380
+ } ) ;
381
+
382
+ it ( 'should wait the specified number of seconds returned from the wait function' , async ( ) => {
383
+ const start = jest . useFakeTimers ( { advanceTimers : true } ) . now ( ) ;
384
+ let timesRan = 0 ;
385
+ const asyncFunc = jest . fn ( async ( ) => new Promise ( resolve => {
386
+ timesRan ++ ;
387
+
388
+ // eslint-disable-next-line jest/no-conditional-in-test
389
+ if ( timesRan === 3 ) {
390
+ throw new Error ( 'Done' ) ;
391
+ }
392
+
393
+ resolve ( 'Not yet' ) ;
394
+ } ) ) ;
395
+
396
+ await func . poll (
397
+ asyncFunc ,
398
+ // wait for 100ms on the first attempt, 200ms on second and the 3rd attempt exits
399
+ ( _result , attempts ) => attempts * 100
400
+ ) . catch ( ( ) => { } ) ;
401
+
402
+ expect ( asyncFunc ) . toHaveBeenCalledTimes ( 3 ) ;
403
+ expect ( jest . useFakeTimers ( ) . now ( ) - start ) . toBeGreaterThanOrEqual ( 300 ) ;
404
+ } ) ;
405
+
406
+ it ( 'should poll until the given date' , async ( ) => {
407
+ const start = jest . useFakeTimers ( { advanceTimers : true } ) . now ( ) ;
408
+ const asyncFunc = jest . fn ( async ( ) => new Promise ( resolve => resolve ( 'Not yet' ) ) ) ;
409
+
410
+ await func . poll (
411
+ asyncFunc ,
412
+ 0 ,
413
+ new Date ( start + 100 )
414
+ ) . catch ( ( ) => { } ) ;
415
+
416
+ expect ( jest . useFakeTimers ( ) . now ( ) - start ) . toBeGreaterThanOrEqual ( 100 ) ;
417
+ } ) ;
418
+
419
+ it ( 'should poll until the until argument returns true' , async ( ) => {
420
+ jest . useFakeTimers ( { advanceTimers : true } ) ;
421
+ const asyncFunc = jest . fn ( async ( ) => new Promise ( resolve => resolve ( 'Not yet' ) ) ) ;
422
+
423
+ await func . poll (
424
+ asyncFunc ,
425
+ 0 ,
426
+ ( _result , attempts ) => attempts === 3
427
+ ) . catch ( ( ) => { } ) ;
428
+
429
+ expect ( asyncFunc ) . toHaveBeenCalledTimes ( 3 ) ;
430
+ } ) ;
371
431
} ) ;
372
432
} ) ;
0 commit comments