-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
50 lines (39 loc) · 1007 Bytes
/
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
'use strict'
const test = require('tape')
const timer = require('./lib')
test('exports fn', (t) => {
t.equals(typeof timer, 'function')
t.end()
})
test('timer returns fn', (t) => {
t.equals(typeof timer(), 'function')
t.end()
})
test('no suffix returns number', (t) => {
t.equals(isNaN(Number(timer()())), false)
t.end()
})
;['ms', 's', 'm', 'h'].forEach((unit) => {
test(`${unit} returns "${unit}" suffix`, (t) => {
const res = timer(unit, true)()
t.equals(res.indexOf(unit) !== -1, true)
t.end()
})
})
test('counts ok', (t) => {
const start = Date.now()
const ms = timer('ms')
const s = timer('s')
const m = timer('m')
const h = timer('s')
const end = start + (1000 * 60 * 60)
const expected = end - start
Date = function () {
this.valueOf = () => end
}
t.equals(ms() >= expected, true)
t.equals(s() >= expected / 1000, true)
t.equals(m() >= expected / (1000 * 60), true)
t.equals(h() >= expected / (1000 * 60 * 60), true)
t.end()
})