-
Notifications
You must be signed in to change notification settings - Fork 6
/
timer.js
247 lines (187 loc) · 5.89 KB
/
timer.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var should = require('should')
var sinon = require('sinon')
var Timer = require('../lib/timer')
var getTimeMock = require('./mocks/date-get-time')
var timerTimeMock = require('./mocks/timer-time')
describe('A Timer object', function () {
var timer
var start = 100
var end = 200
beforeEach(function () {
timer = new Timer()
getTimeMock.expect(start)
})
afterEach(function () {
getTimeMock.revert()
})
describe('constructor', function () {
it('should initialize a new Timer object', function () {
timer.should.be.an.instanceof(Timer)
})
it('should be stopped unless the first argument is true', function () {
timer.isStopped().should.be.true
})
it('should be started if the first argument is true', function () {
var autoTimer = new Timer(true)
autoTimer.isStarted().should.be.true
})
})
describe('time prototype method', function () {
it('should return 0 if the timer has not been started', function () {
timer.time().should.equal(0)
})
it('should return the time since start if only started once', function () {
timer.start()
getTimeMock.expect(end)
timer.time().should.equal(end - start)
})
it ('should include the total of past start/stops', function () {
timer._total = 47
timer.start()
getTimeMock.expect(end)
timer.time().should.equal(end - start + timer._total)
})
})
describe('emitTime prototype method', function () {
it('should return the result of timer.time', function () {
var expected = {}
timerTimeMock.expect(expected)
timer.emitTime().should.equal(expected)
timerTimeMock.revert()
})
})
describe('timeFromStart prototype method', function () {
it('should return 0 if the timer is stopped', function () {
timer.timeFromStart().should.equal(0)
timer.start()
getTimeMock.expect(end)
timer.stop()
timer.timeFromStart().should.equal(0)
})
it('should return the time since start', function () {
timer.start()
getTimeMock.expect(end)
timer.timeFromStart().should.equal(end - start)
})
it ('should not include the total of past start/stops', function () {
timer._total = 47
timer.start()
getTimeMock.expect(end)
timer.timeFromStart().should.equal(end - start)
})
})
describe('isStarted prototype method', function () {
it('should return false when the timer is stopped', function () {
timer.isStarted().should.be.false
timer.start()
timer.stop()
timer.isStarted().should.be.false
})
it('should return true when the timer is started', function () {
timer.start()
timer.isStarted().should.be.true
})
})
describe('isStopped prototype method', function () {
it('should return true when the timer is stopped', function () {
timer.isStopped().should.be.true
timer.start()
timer.stop()
timer.isStopped().should.be.true
})
it('should return false when the timer is started', function () {
timer.start()
timer.isStopped().should.be.false
})
})
describe('start prototype method', function () {
it('should return true if currently stopped', function () {
timer.start().should.be.true
timer.stop()
timer.start().should.be.true
})
it('should return false if currently started', function () {
timer.start()
timer.start().should.be.false
})
it('should set timer._start to the current time', function () {
timer.start()
timer._start.should.equal(start)
})
})
describe('stop prototype method', function () {
it('should return true if currently started', function () {
timer.start()
timer.stop().should.be.true
})
it('should return false if currently stopped', function () {
timer.stop().should.be.false
timer.start()
timer.stop()
timer.stop().should.be.false
})
it('should set timer._start to null', function () {
timer.start()
timer.stop()
should(timer._start).equal(null)
})
})
describe('stopParallel prototype method', function () {
it('should return true if called equal to start', function () {
timer.start()
timer.start()
timer.stopParallel()
debugger
timer.stopParallel().should.be.true
})
it('should return false if called less than start', function () {
timer.start()
timer.start()
timer.stopParallel().should.be.false
})
it('should set timer._start to null if called equal to start', function () {
timer.start()
timer.start()
timer.stopParallel()
timer.stopParallel()
should(timer._start).equal(null)
})
it('should not set timer._start to null if called less', function () {
timer.start()
timer.start()
timer.stopParallel()
should(timer._start).not.equal(null)
})
})
describe('toggle prototype method', function () {
it('should start the timer when it is stopped', function () {
timer.toggle()
timer.isStarted().should.be.true
})
it('should stop the timer when it is started', function () {
timer.start()
timer.toggle()
timer.isStopped().should.be.true
})
it('should always return true', function () {
timer.toggle().should.be.true
timer.toggle().should.be.true
})
})
describe('toString prototype method', function () {
it('should return a string', function () {
timer.toString().should.be.a.String
})
it('should be a whole number followed by"ms"', function () {
timer.toString().should.match(/\d+ms/)
})
})
describe('valueOf prototype method', function () {
it('should be an alias of time()', function () {
var expected = {}
sinon.stub(timer, 'time').returns(expected)
timer.valueOf().should.equal(expected)
timer.time.restore()
})
})
})