forked from mathiasvr/tiny-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
162 lines (132 loc) · 3.56 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
const test = require('tape')
const Timer = require('.')
test('countdown ticks', { timeout: 500 }, function (t) {
const timer = new Timer({ interval: 10 })
let lastms = 51
timer.on('tick', (ms) => {
if (lastms === 51) t.equal(ms, 50, 'first update should be 50')
t.ok(ms < lastms, 'time decreasing')
lastms = ms
})
timer.on('done', () => {
t.equal(lastms, 0, 'last update should be 0')
t.end()
})
timer.start(50)
})
test('stopwatch ticks', { timeout: 500 }, function (t) {
const timer = new Timer({ interval: 10, stopwatch: true })
let lastms = -1
timer.on('tick', (ms) => {
if (lastms === -1) t.equal(ms, 0, 'first update should be 0')
t.ok(ms > lastms, 'time increasing')
lastms = ms
})
timer.on('done', () => {
t.equal(lastms, 50, 'last update should be 50')
t.end()
})
timer.start(50)
})
test('stop', function (t) {
const timer = new Timer({ interval: 10 })
timer.on('done', () => t.fail())
setTimeout(() => {
t.equal(timer.status, 'stopped')
t.end()
}, 100)
timer.start(50)
t.equal(timer.status, 'running')
timer.stop()
})
test('pause and resume', function (t) {
const timer = new Timer({ interval: 10 })
const startTime = Date.now()
timer.on('done', () => {
t.ok(Date.now() - startTime > 100, 'paused for at least 100ms')
t.end()
})
setTimeout(() => {
t.equal(timer.status, 'paused')
timer.resume()
}, 100)
timer.start(50)
t.equal(timer.status, 'running')
timer.pause()
})
test('state transition', function (t) {
const timer = new Timer({ interval: 10 })
t.equal(timer.status, 'stopped')
timer.stop()
t.equal(timer.status, 'stopped')
timer.pause()
t.equal(timer.status, 'stopped')
timer.resume()
t.equal(timer.status, 'stopped')
timer.start(20)
t.equal(timer.status, 'running')
timer.pause()
t.equal(timer.status, 'paused')
timer.resume()
t.equal(timer.status, 'running')
timer.stop()
t.equal(timer.status, 'stopped')
timer.start(20)
t.equal(timer.status, 'running')
timer.pause()
timer.stop()
t.equal(timer.status, 'stopped')
timer.start(20)
timer.on('done', () => {
t.equal(timer.status, 'stopped')
t.end()
})
})
test('duration property', function (t) {
const timer = new Timer({ interval: 10 })
timer.on('done', () => {
t.equal(timer.duration, 50, 'correct last duration')
t.end()
})
timer.start(50)
t.equal(timer.duration, 50, 'correct duration')
})
test('time property', function (t) {
const run = function (stopwatch) {
const timer = new Timer({ interval: 10, stopwatch: stopwatch })
timer.on('tick', (ms) => {
const time = timer.time
// TODO: last ms and time is not equal in stopwatch mode
// because we stop the timer before calling tick to ensure
// that .time won't be less than 0 or greater than duration
if (stopwatch && ms === 50) return
t.ok(time > ms - 5 && time < ms + 5, 'time should be around the ms param')
})
timer.on('done', () => {
t.equal(timer.time, 0)
if (stopwatch) t.end()
})
t.equal(timer.time, 0)
timer.start(50)
const rtime = timer.time
timer.pause()
const ptime = timer.time
t.equal(rtime, ptime)
setTimeout(() => {
t.equal(ptime, timer.time)
timer.resume()
}, 20)
}
run(false)
run(true)
})
test('override interval', function (t) {
const timer = new Timer({ interval: 1000 })
let ticks = 0
timer.on('tick', (ms) => ticks++)
timer.on('done', (ms) => {
t.ok(ticks > 5, 'should do extra tick events')
t.end()
})
timer.start(100, 10)
})