-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
117 lines (88 loc) · 2.18 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
'use strict'
const test = require('tape')
const retimer = require('./')
test('schedule a callback', function (t) {
t.plan(1)
const start = Date.now()
retimer(function () {
t.ok(Date.now() - start >= 50, 'it was deferred ok!')
}, 50)
})
test('reschedule a callback', function (t) {
t.plan(1)
const start = Date.now()
const timer = retimer(function () {
t.ok(Date.now() - start >= 70, 'it was deferred ok!')
}, 50)
setTimeout(function () {
timer.reschedule(50)
}, 20)
})
test('reschedule multiple times', function (t) {
t.plan(1)
const start = Date.now()
const timer = retimer(function () {
t.ok(Date.now() - start >= 90, 'it was deferred ok!')
}, 50)
setTimeout(function () {
timer.reschedule(50)
setTimeout(function () {
timer.reschedule(50)
}, 20)
}, 20)
})
test('clear a timer', function (t) {
t.plan(1)
const timer = retimer(function () {
t.fail('the timer should never get called')
}, 20)
timer.clear()
setTimeout(function () {
t.pass('nothing happened')
}, 50)
})
test('clear a timer after a reschedule', function (t) {
t.plan(1)
const timer = retimer(function () {
t.fail('the timer should never get called')
}, 20)
setTimeout(function () {
timer.reschedule(50)
setTimeout(function () {
timer.clear()
}, 10)
}, 10)
setTimeout(function () {
t.pass('nothing happened')
}, 50)
})
test('can be rescheduled early', function (t) {
t.plan(1)
const start = Date.now()
const timer = retimer(function () {
t.ok(Date.now() - start <= 500, 'it was rescheduled!')
}, 500)
setTimeout(function () {
timer.reschedule(10)
}, 20)
})
test('can be rescheduled even if the timeout has already triggered', function (t) {
t.plan(2)
const start = Date.now()
let count = 0
const timer = retimer(function () {
count++
if (count === 1) {
t.ok(Date.now() - start >= 20, 'it was triggered!')
timer.reschedule(20)
} else {
t.ok(Date.now() - start >= 40, 'it was rescheduled!')
}
}, 20)
})
test('pass arguments to the callback', function (t) {
t.plan(1)
retimer(function (arg) {
t.equal(arg, 42, 'argument matches')
}, 50, 42)
})