-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
172 lines (131 loc) · 4.1 KB
/
index.test.ts
File metadata and controls
172 lines (131 loc) · 4.1 KB
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
import { describe, it } from 'node:test';
import { strict as assert } from 'node:assert';
import { Timer } from './index.js';
const STARTED = 1702006671868;
const ENDED = 1702006671900;
const DELTA = 5; // b/c js timers (O_o)
describe('@bust/timer', () => {
it('Initializes', () => {
const timer = new Timer();
assert(timer.hasOwnProperty('started'));
assert(timer.hasOwnProperty('ended'));
assert.equal(typeof timer.isRunning, 'function');
assert.equal(timer.isRunning(), false);
assert.equal(typeof timer.isFinished, 'function');
assert.equal(timer.isFinished(), false);
assert.equal(typeof timer.elapsed, 'function');
assert.equal(timer.elapsed(), 0);
assert.equal(typeof timer.now, 'function');
assert(timer.now() > STARTED);
assert.equal(typeof timer.start, 'function');
assert.equal(timer.start(), timer);
assert.equal(typeof timer.end, 'function');
assert.equal(timer.end(), timer);
assert.equal(typeof timer.mark, 'function');
assert.equal(timer.mark(), timer);
});
it('Captures start time', () => {
const timer = new Timer();
timer.start();
assert(timer.started);
assert(timer.now() - timer.started < DELTA);
assert.equal(timer.ended, 0);
assert.equal(timer.isRunning(), true);
assert.equal(timer.isFinished(), false);
});
it('Accepts a start time if provided', () => {
const timer = new Timer();
timer.start(STARTED);
assert(timer.started);
assert.equal(timer.started, STARTED);
assert.equal(timer.ended, 0);
assert.equal(timer.isRunning(), true);
assert.equal(timer.isFinished(), false);
});
it('Captures end time', (ctx, done) => {
const timer = new Timer();
const delay = 50;
const runAssertions = () => {
timer.end();
assert(timer.started > 0);
assert(timer.ended > 0);
assert(timer.now() - timer.ended < delay + DELTA);
assert.equal(timer.isRunning(), false);
assert.equal(timer.isFinished(), true);
done();
};
timer.start();
setTimeout(runAssertions, delay);
});
it('Accepts an end time if provided', () => {
const timer = new Timer();
timer.start(STARTED);
timer.end(ENDED);
assert.equal(timer.started, STARTED);
assert.equal(timer.ended, ENDED);
assert.equal(timer.isRunning(), false);
assert.equal(timer.isFinished(), true);
});
it('Marks start & end time', () => {
const timer = new Timer();
const now = timer.now();
timer.mark();
assert(timer.started > 0);
assert(now - timer.started < DELTA);
assert(timer.ended > 0);
assert(now - timer.ended < DELTA);
assert.equal(timer.isRunning(), false);
assert.equal(timer.isFinished(), true);
});
it('Accepts a mark time if provided', () => {
const timer = new Timer();
timer.mark(ENDED);
assert.equal(timer.started, ENDED);
assert.equal(timer.ended, ENDED);
assert.equal(timer.isRunning(), false);
assert.equal(timer.isFinished(), true);
});
it('Calculates elapsed time', () => {
const timer = new Timer();
timer.start(STARTED);
timer.end(ENDED);
assert.equal(timer.elapsed(), 32);
});
it('Calculates elapsed time when timer is still running', (ctx, done) => {
const timer = new Timer();
const delay = 50;
const runAssertions = () => {
assert(timer.elapsed() < delay + DELTA);
assert.equal(timer.ended, 0);
assert.equal(timer.isRunning(), true);
assert.equal(timer.isFinished(), false);
done();
};
timer.start();
setTimeout(runAssertions, delay);
});
it('Determines if the timer is running', () => {
const timer = new Timer();
assert.equal(timer.isRunning(), false);
timer.start();
assert.equal(timer.isRunning(), true);
timer.end();
assert.equal(timer.isRunning(), false);
});
it('Determine if the timer is finished', () => {
const timer = new Timer();
assert.equal(timer.isFinished(), false);
timer.start();
assert.equal(timer.isFinished(), false);
timer.end();
assert.equal(timer.isFinished(), true);
});
it('Should not error if called derpily', () => {
const timer = new Timer();
const input = [undefined, null, '', false, 0];
assert.doesNotThrow(() => {
// @ts-expect-error - testing error cases
input.forEach((x) => timer.start(x).end(x));
});
});
});