-
Notifications
You must be signed in to change notification settings - Fork 14
/
format-duration.test.js
72 lines (67 loc) · 4.77 KB
/
format-duration.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
const test = require('tape')
const f = require('./format-duration')
test('it works', function (t) {
t.equal(f(999), '0:00', 'anything under a second is 0:00')
t.equal(f(1000), '0:01', 'check 1000 milliseconds is a second')
t.equal(f(1000 * 2 - 1), '0:01', 'rounds 1999 down to 0:01')
t.equal(f(1000 * 60), '1:00', 'check 60 seconds is a minute')
t.equal(f(1000 * 60 - 1), '0:59', 'check 59 seconds looks ok')
t.equal(f(1000 * 60 * 60), '1:00:00', 'check 60 minutes is an hour')
t.equal(f(1000 * 60 * 60 - 1), '59:59', 'check 59 minutes looks ok')
t.equal(f(1000 * 60 * 60 * 24), '1:00:00:00', 'check 24 hours is a day')
t.equal(f(1000 * 60 * 60 * 24 - 1), '23:59:59', 'check 23 hours looks okay')
t.equal(f(1000 * 60 * 60 * 24 * 365), '365:00:00:00', 'check 365 days is too long to care')
t.end()
})
test('it works with negative durations', function (t) {
t.equal(f(-999), '0:00', 'anything under a second is 0:00')
t.equal(f(-1000), '-0:01', 'check -1000 milliseconds is a second')
t.equal(f(-1000 * 2 + 1), '-0:01', 'rounds -1999 up to -0:01')
t.equal(f(-1000 * 60), '-1:00', 'check -60 seconds is a minute')
t.equal(f(-1000 * 60 + 1), '-0:59', 'check -59 seconds looks ok')
t.equal(f(-1000 * 60 * 60), '-1:00:00', 'check -60 minutes is an hour')
t.equal(f(-1000 * 60 * 60 + 1), '-59:59', 'check -59 minutes looks ok')
t.equal(f(-1000 * 60 * 60 * 24), '-1:00:00:00', 'check -24 hours is a day')
t.equal(f(-1000 * 60 * 60 * 24 + 1), '-23:59:59', 'check -23 hours looks okay')
t.equal(f(-1000 * 60 * 60 * 24 * 365), '-365:00:00:00', 'check -365 days is too long to care')
t.end()
})
test('it works with leading zeros', function (t) {
t.equal(f(999, { leading: true }), '00:00', 'anything under a second is 00:00')
t.equal(f(1000, { leading: true }), '00:01', 'check 1000 milliseconds is a second')
t.equal(f(1000 * 2 - 1, { leading: true }), '00:01', 'rounds 1999 down to 00:01')
t.equal(f(1000 * 60, { leading: true }), '01:00', 'check 60 seconds is a minute')
t.equal(f(1000 * 60 - 1, { leading: true }), '00:59', 'check 59 seconds looks ok')
t.equal(f(1000 * 60 * 60, { leading: true }), '01:00:00', 'check 60 minutes is an hour')
t.equal(f(1000 * 60 * 60 - 1, { leading: true }), '59:59', 'check 59 minutes looks ok')
t.equal(f(1000 * 60 * 60 * 24, { leading: true }), '1:00:00:00', 'check 24 hours is a day')
t.equal(f(1000 * 60 * 60 * 24 - 1, { leading: true }), '23:59:59', 'check 23 hours looks okay')
t.equal(f(1000 * 60 * 60 * 24 * 365, { leading: true }), '365:00:00:00', 'check 365 days is too long to care')
t.end()
})
test('it works with leading zeros and milliseconds', function (t) {
t.equal(f(999, { leading: true, ms: true }), '00:00.999', 'anything under a second is correctly displayed')
t.equal(f(1000, { leading: true, ms: true }), '00:01.000', 'check 1000 milliseconds is a second')
t.equal(f(1000 * 2 - 1, { leading: true, ms: true }), '00:01.999', 'displays 1999 as 00:01.999')
t.equal(f(1000 * 60, { leading: true, ms: true }), '01:00.000', 'check 60 seconds is a minute')
t.equal(f(1000 * 60 - 1, { leading: true, ms: true }), '00:59.999', 'check 59.999 seconds looks ok')
t.equal(f(1000 * 60 * 60, { leading: true, ms: true }), '01:00:00.000', 'check 60 minutes is an hour')
t.equal(f(1000 * 60 * 60 - 1, { leading: true, ms: true }), '59:59.999', 'check 59 minutes with 999 milliseconds looks ok')
t.equal(f(1000 * 60 * 60 * 24, { leading: true, ms: true }), '1:00:00:00.000', 'check 24 hours is a day')
t.equal(f(1000 * 60 * 60 * 24 - 1, { leading: true, ms: true }), '23:59:59.999', 'check 23 hours with 999 milliseconds looks okay')
t.equal(f(1000 * 60 * 60 * 24 * 365, { leading: true, ms: true }), '365:00:00:00.000', 'check 365 days is too long to care')
t.end()
})
test('it works with negative durations and milliseconds', function (t) {
t.equal(f(-999, { ms: true }), '-0:00.999', 'anything under a second is correctly displayed with negative sign')
t.equal(f(-1000, { ms: true }), '-0:01.000', 'check -1000 milliseconds is a second')
t.equal(f(-1000 * 2 + 1, { ms: true }), '-0:01.999', 'displays -1999 as -00:01.999')
t.equal(f(-1000 * 60, { ms: true }), '-1:00.000', 'check -60 seconds is a minute')
t.equal(f(-1000 * 60 + 1, { ms: true }), '-0:59.999', 'check -59.999 seconds looks ok')
t.equal(f(-1000 * 60 * 60, { ms: true }), '-1:00:00.000', 'check -60 minutes is an hour')
t.equal(f(-1000 * 60 * 60 + 1, { ms: true }), '-59:59.999', 'check -59 minutes with 999 milliseconds looks ok')
t.equal(f(-1000 * 60 * 60 * 24, { ms: true }), '-1:00:00:00.000', 'check -24 hours is a day')
t.equal(f(-1000 * 60 * 60 * 24 + 1, { ms: true }), '-23:59:59.999', 'check -23 hours with 999 milliseconds looks okay')
t.equal(f(-1000 * 60 * 60 * 24 * 365, { ms: true }), '-365:00:00:00.000', 'check -365 days is too long to care')
t.end()
})