-
Notifications
You must be signed in to change notification settings - Fork 24
/
extras-utils.test.js
73 lines (65 loc) · 2.15 KB
/
extras-utils.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
'use strict'
import test from 'ava'
import {
ObjectID
} from 'mongodb'
import {
hexTime,
surveyorChoices,
createdTimestamp,
timeout,
documentOlderThan,
isYoutubeChannelId,
normalizeChannel
} from './extras-utils'
import dotenv from 'dotenv'
dotenv.config()
const objectId = ObjectID('5b11685dd28b11258d50c1f4')
const objectDate = (new Date('2018-06-01T15:38:05.000Z')).getTime()
test('createdTimestamp', (t) => {
const fromId = createdTimestamp(objectId)
t.is(fromId, objectDate)
})
test('timeout', (t) => {
let bool = false
timeout(495).then(() => {
bool = true
})
const justRight = timeout(500).then(() => {
t.true(bool)
})
const tooLate = timeout(505).then(() => {
throw new Error('bad timeout')
})
return Promise.race([
justRight,
tooLate
])
})
test('documentOlderThan', (t) => {
t.true(documentOlderThan(-1, objectDate, objectId))
t.false(documentOlderThan(1, objectDate, objectId))
// lt not lte
t.false(documentOlderThan(0, objectDate, objectId))
})
test('isYoutubeChannelId', (t) => {
t.true(isYoutubeChannelId('UCFNTTISby1c_H-rm5Ww5rZg'))
t.false(isYoutubeChannelId('UCFNTTISby1c_H-rm5Ww5rZ'))
t.false(isYoutubeChannelId('Brave'))
})
test('normalizeChannel', (t) => {
t.is(normalizeChannel('youtube#channel:UCFNTTISby1c_H-rm5Ww5rZg'), 'youtube#channel:UCFNTTISby1c_H-rm5Ww5rZg')
t.is(normalizeChannel('youtube#channel:Brave'), 'youtube#user:Brave')
t.is(normalizeChannel('twitch#channel:Brave'), 'twitch#author:Brave')
t.is(normalizeChannel('www.brave.com'), 'www.brave.com')
})
test('surveyorChoices', (t) => {
t.deepEqual(surveyorChoices(0.55), [6, 10, 14, 20, 40], 'increment is less than')
t.deepEqual(surveyorChoices(0.5), [6, 10, 14, 20, 40], 'increment is equal to')
t.deepEqual(surveyorChoices(2), [3, 5, 7, 10, 20], 'increment can be above range')
t.deepEqual(surveyorChoices(0.02), [30, 50, 70, 100], 'increment can be below range')
})
test('hexTime', (t) => {
t.is(hexTime(new Date('2019-01-01')), '5c2aad800000000000000000', 'returns hexidecimal version of date')
t.is(hexTime(new Date('2018-01-01')), '5a497a000000000000000000', 'returns hexidecimal version of any date')
})