-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-basic.ts
77 lines (59 loc) · 1.68 KB
/
test-basic.ts
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
import 'source-map-support/register'
import * as test from 'purple-tape'
import { setRandomizedInterval } from '../index'
test('shall fire a reasonable number of times', function*(t) {
let times = 0
let interval = 200
let time = 5000
let i1 = setRandomizedInterval(() => {
times++
}, interval)
yield wait(time)
t.ok(times < time / (interval * 0.5), 'shall not fire too many times')
t.ok(times > time / (interval * 1.5), 'shall not fire too few times')
times = 0
i1.clear()
yield wait(2000)
t.equal(times, 0, 'shall stop when told to')
})
test('shall include extra arguments to callback', function*(t) {
let lastArg
let times = 0
let i1 = setRandomizedInterval(
arg => {
times++
lastArg = arg
},
1000,
'myarg'
)
while (times === 0) {
yield wait(100)
}
t.equal(lastArg, 'myarg', 'shall pass argument first time callback fires')
lastArg = undefined
while (times === 1) {
yield wait(100)
}
t.equal(lastArg, 'myarg', 'shall pass argument second time callback fires')
i1.clear()
})
test('unref()', function*(t) {
let t1 = setRandomizedInterval(() => {}, 100)
let t2 = setRandomizedInterval(() => {}, 100)
t1.unref()
yield wait(400)
t1.ref()
t1.unref()
t2.unref()
t.pass('if the test exits, the unref() was successful')
})
test('setRandomizedInterval clear()', function(t) {
let t1 = setRandomizedInterval(() => {
t1.clear()
}, 100)
t.pass('if the test exits, the clear() was successful')
})
function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}