forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.test.js
94 lines (84 loc) · 2.01 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import test from 'ava'
const utils = require('../lib/utils')
const ansiHTML = require('ansi-html')
test('encodeHtml', t => {
const html = '<h1>Hello</h1>'
t.is(utils.encodeHtml(html), '<h1>Hello</h1>')
})
test('getContext', t => {
let ctx = utils.getContext({ a: 1 }, { b: 2 })
t.is(utils.getContext.length, 2)
t.is(typeof ctx.req, 'object')
t.is(typeof ctx.res, 'object')
t.is(ctx.req.a, 1)
t.is(ctx.res.b, 2)
})
test('setAnsiColors', t => {
utils.setAnsiColors(ansiHTML)
})
test('waitFor', function * (t) {
let s = Date.now()
yield utils.waitFor(100)
t.true(Date.now() - s >= 100)
})
test('urlJoin', t => {
t.is(utils.urlJoin('test', '/about'), 'test/about')
})
test('promisifyRouteParams (array)', t => {
const array = [1]
const promise = utils.promisifyRouteParams(array)
t.is(typeof promise, 'object')
return promise
.then((res) => {
t.is(res, array)
})
})
test('promisifyRouteParams (fn => array)', t => {
const array = [1, 2]
const fn = function () {
return array
}
const promise = utils.promisifyRouteParams(fn)
t.is(typeof promise, 'object')
return promise
.then((res) => {
t.is(res, array)
})
})
test('promisifyRouteParams (fn => promise)', t => {
const array = [1, 2, 3]
const fn = function () {
return new Promise((resolve) => {
resolve(array)
})
}
const promise = utils.promisifyRouteParams(fn)
t.is(typeof promise, 'object')
return promise
.then((res) => {
t.is(res, array)
})
})
test('promisifyRouteParams (fn(cb) with error)', t => {
const fn = function (cb) {
cb('Error here')
}
const promise = utils.promisifyRouteParams(fn)
t.is(typeof promise, 'object')
return promise
.catch((e) => {
t.is(e, 'Error here')
})
})
test('promisifyRouteParams (fn(cb) with result)', t => {
const array = [1, 2, 3, 4]
const fn = function (cb) {
cb(null, array)
}
const promise = utils.promisifyRouteParams(fn)
t.is(typeof promise, 'object')
return promise
.then((res) => {
t.is(res, array)
})
})