forked from sindresorhus/debounce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
170 lines (110 loc) · 3.79 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var debounce = require('.')
var sinon = require('sinon')
describe('housekeeping', function() {
it('should be defined as a function', function() {
expect(typeof debounce).toEqual('function')
})
})
describe('catch issue #3 - Debounced function executing early?', function() {
// use sinon to control the clock
var clock
beforeEach(function(){
clock = sinon.useFakeTimers()
})
afterEach(function(){
clock.restore()
})
it('should debounce with fast timeout', function() {
var callback = sinon.spy()
// set up debounced function with wait of 100
var fn = debounce(callback, 100)
// call debounced function at interval of 50
setTimeout(fn, 100)
setTimeout(fn, 150)
setTimeout(fn, 200)
setTimeout(fn, 250)
// set the clock to 100 (period of the wait) ticks after the last debounced call
clock.tick(350)
// the callback should have been triggered once
expect(callback.callCount).toEqual(1)
})
})
describe('forcing execution', function() {
// use sinon to control the clock
var clock
beforeEach(function(){
clock = sinon.useFakeTimers()
})
afterEach(function(){
clock.restore()
})
it('should not execute prior to timeout', function() {
var callback = sinon.spy()
// set up debounced function with wait of 100
var fn = debounce(callback, 100)
// call debounced function at interval of 50
setTimeout(fn, 100)
setTimeout(fn, 150)
// set the clock to 25 (period of the wait) ticks after the last debounced call
clock.tick(175)
// the callback should not have been called yet
expect(callback.callCount).toEqual(0)
})
it('should execute prior to timeout when flushed', function() {
var callback = sinon.spy()
// set up debounced function with wait of 100
var fn = debounce(callback, 100)
// call debounced function at interval of 50
setTimeout(fn, 100)
setTimeout(fn, 150)
// set the clock to 25 (period of the wait) ticks after the last debounced call
clock.tick(175)
fn.flush()
// the callback has been called
expect(callback.callCount).toEqual(1)
})
it('should not execute again after timeout when flushed before the timeout', function() {
var callback = sinon.spy()
// set up debounced function with wait of 100
var fn = debounce(callback, 100)
// call debounced function at interval of 50
setTimeout(fn, 100)
setTimeout(fn, 150)
// set the clock to 25 (period of the wait) ticks after the last debounced call
clock.tick(175)
fn.flush()
// the callback has been called here
expect(callback.callCount).toEqual(1)
// move to past the timeout
clock.tick(225)
// the callback should have only been called once
expect(callback.callCount).toEqual(1)
})
it('should not execute on a timer after being flushed', function() {
var callback = sinon.spy()
// set up debounced function with wait of 100
var fn = debounce(callback, 100)
// call debounced function at interval of 50
setTimeout(fn, 100)
setTimeout(fn, 150)
// set the clock to 25 (period of the wait) ticks after the last debounced call
clock.tick(175)
fn.flush()
// the callback has been called here
expect(callback.callCount).toEqual(1)
// schedule again
setTimeout(fn, 250)
// move to past the new timeout
clock.tick(400)
// the callback should have been called again
expect(callback.callCount).toEqual(2)
})
it('should not execute when flushed if nothing was scheduled', function() {
var callback = sinon.spy()
// set up debounced function with wait of 100
var fn = debounce(callback, 100)
fn.flush()
// the callback should not have been called
expect(callback.callCount).toEqual(0)
})
})