@@ -3,6 +3,7 @@ const eachAsync = require('../../lib/core/utils').eachAsync;
3
3
const makeInterruptableAsyncInterval = require ( '../../lib/utils' ) . makeInterruptableAsyncInterval ;
4
4
const now = require ( '../../lib/utils' ) . now ;
5
5
const expect = require ( 'chai' ) . expect ;
6
+ const sinon = require ( 'sinon' ) ;
6
7
7
8
describe ( 'utils' , function ( ) {
8
9
context ( 'eachAsync' , function ( ) {
@@ -37,7 +38,13 @@ describe('utils', function() {
37
38
} ) ;
38
39
39
40
context ( 'makeInterruptableAsyncInterval' , function ( ) {
40
- const roundToNearestMultipleOfTen = x => Math . round ( x / 10 ) * 10 ;
41
+ before ( function ( ) {
42
+ this . clock = sinon . useFakeTimers ( ) ;
43
+ } ) ;
44
+
45
+ after ( function ( ) {
46
+ this . clock . restore ( ) ;
47
+ } ) ;
41
48
42
49
it ( 'should execute a method in an repeating interval' , function ( done ) {
43
50
let lastTime = now ( ) ;
@@ -52,11 +59,13 @@ describe('utils', function() {
52
59
) ;
53
60
54
61
setTimeout ( ( ) => {
55
- const roundedMarks = marks . map ( roundToNearestMultipleOfTen ) ;
56
- expect ( roundedMarks . every ( mark => roundedMarks [ 0 ] === mark ) ) . to . be . true ;
62
+ expect ( marks ) . to . eql ( [ 10 , 10 , 10 , 10 , 10 ] ) ;
63
+ expect ( marks . every ( mark => marks [ 0 ] === mark ) ) . to . be . true ;
57
64
executor . stop ( ) ;
58
65
done ( ) ;
59
- } , 50 ) ;
66
+ } , 51 ) ;
67
+
68
+ this . clock . tick ( 51 ) ;
60
69
} ) ;
61
70
62
71
it ( 'should schedule execution sooner if requested within min interval threshold' , function ( done ) {
@@ -75,11 +84,12 @@ describe('utils', function() {
75
84
executor . wake ( ) ;
76
85
77
86
setTimeout ( ( ) => {
78
- const roundedMarks = marks . map ( roundToNearestMultipleOfTen ) ;
79
- expect ( roundedMarks [ 0 ] ) . to . be . lessThan ( 50 ) ;
87
+ expect ( marks ) . to . eql ( [ 10 , 50 ] ) ;
80
88
executor . stop ( ) ;
81
89
done ( ) ;
82
- } , 50 ) ;
90
+ } , 100 ) ;
91
+
92
+ this . clock . tick ( 100 ) ;
83
93
} ) ;
84
94
85
95
it ( 'should debounce multiple requests to wake the interval sooner' , function ( done ) {
@@ -99,12 +109,12 @@ describe('utils', function() {
99
109
}
100
110
101
111
setTimeout ( ( ) => {
102
- const roundedMarks = marks . map ( roundToNearestMultipleOfTen ) ;
103
- expect ( roundedMarks [ 0 ] ) . to . be . lessThan ( 50 ) ;
104
- expect ( roundedMarks . slice ( 1 ) . every ( mark => mark === 50 ) ) . to . be . true ;
112
+ expect ( marks ) . to . eql ( [ 10 , 50 , 50 , 50 , 50 ] ) ;
105
113
executor . stop ( ) ;
106
114
done ( ) ;
107
115
} , 250 ) ;
116
+
117
+ this . clock . tick ( 250 ) ;
108
118
} ) ;
109
119
} ) ;
110
120
} ) ;
0 commit comments