-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
59 lines (47 loc) · 1.46 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
const test = require('tape')
const { TimeoutController } = require('./')
const delay = require('delay')
const callCounter = () => {
let count = 0
const counter = function () {
count++
}
counter.getCount = () => count
return counter
}
test('aborts when the timer expires', async t => {
const timeoutController = new TimeoutController(50)
const counter = callCounter()
timeoutController.signal.addEventListener('abort', counter)
await delay(70)
t.equal(timeoutController.signal.aborted, true)
t.equal(counter.getCount(), 1)
t.end()
})
test('can be manually aborted', async t => {
const timeoutController = new TimeoutController(50)
const counter = callCounter()
timeoutController.signal.addEventListener('abort', counter)
timeoutController.abort()
await delay(70)
t.equal(timeoutController.signal.aborted, true)
t.equal(counter.getCount(), 1)
t.end()
})
test('can clear the timeout', async t => {
const timeoutController = new TimeoutController(50)
timeoutController.clear()
await delay(70)
t.equal(timeoutController.signal.aborted, false)
t.end()
})
test('can reset the timeout', async t => {
const timeoutController = new TimeoutController(50)
await delay(30)
timeoutController.reset() // now expires at 80
await delay(30)
t.equal(timeoutController.signal.aborted, false) // should not have expired at 60
await delay(30)
t.equal(timeoutController.signal.aborted, true) // should have now expired at 90
t.end()
})