forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.js
More file actions
180 lines (163 loc) · 4.49 KB
/
utils.test.js
File metadata and controls
180 lines (163 loc) · 4.49 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
172
173
174
175
176
177
178
179
180
import test from 'ava'
import ansiHTML from 'ansi-html'
import { Utils } from '..'
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)
t.pass()
})
test('waitFor', async t => {
let s = Date.now()
await Utils.waitFor(100)
t.true(Date.now() - s >= 100)
await Utils.waitFor()
})
test('urlJoin', t => {
t.is(Utils.urlJoin('test', '/about'), 'test/about')
})
test('promisifyRoute (array)', t => {
const array = [1]
const promise = Utils.promisifyRoute(array)
t.is(typeof promise, 'object')
return promise.then(res => {
t.is(res, array)
})
})
test('promisifyRoute (fn => array)', t => {
const array = [1, 2]
const fn = function () {
return array
}
const promise = Utils.promisifyRoute(fn)
t.is(typeof promise, 'object')
return promise.then(res => {
t.is(res, array)
})
})
test('promisifyRoute (fn => promise)', t => {
const array = [1, 2, 3]
const fn = function () {
return new Promise(resolve => {
resolve(array)
})
}
const promise = Utils.promisifyRoute(fn)
t.is(typeof promise, 'object')
return promise.then(res => {
t.is(res, array)
})
})
test('promisifyRoute ((fn(args) => promise))', t => {
const fn = function (array) {
return new Promise(resolve => {
resolve(array)
})
}
const array = [1, 2, 3]
const promise = Utils.promisifyRoute(fn, array)
t.is(typeof promise, 'object')
return promise.then(res => {
t.is(res, array)
})
})
test('promisifyRoute (fn(cb) with error)', t => {
const fn = function (cb) {
cb(new Error('Error here'))
}
const promise = Utils.promisifyRoute(fn)
t.is(typeof promise, 'object')
return promise.catch(e => {
t.is(e.message, 'Error here')
})
})
test('promisifyRoute (fn(cb, args) with error)', t => {
const fn = function (cb, array) {
cb(new Error('Error here: ' + array.join()))
}
const array = [1, 2, 3, 4]
const promise = Utils.promisifyRoute(fn, array)
t.is(typeof promise, 'object')
return promise.catch(e => {
t.is(e.message, 'Error here: ' + array.join())
})
})
test('promisifyRoute (fn(cb) with result)', t => {
const array = [1, 2, 3, 4]
const fn = function (cb) {
cb(null, array)
}
const promise = Utils.promisifyRoute(fn)
t.is(typeof promise, 'object')
return promise.then(res => {
t.is(res, array)
})
})
test('promisifyRoute (fn(cb, args) with result)', t => {
const fn = function (cb, array, object) {
cb(null, { array, object })
}
const array = [1, 2, 3, 4]
const object = { a: 1 }
const promise = Utils.promisifyRoute(fn, array, object)
t.is(typeof promise, 'object')
return promise.then(res => {
t.is(res.array, array)
t.is(res.object, object)
})
})
test('chainFn (mutate, mutate)', t => {
// Pass more than one argument to test that they're actually taken into account
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
obj.bar = count + 2
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (mutate, return)', t => {
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (return, mutate)', t => {
const firstFn = function (obj, count) {
return Object.assign({}, obj, { foo: count + 1 })
}
const secondFn = function (obj, count) {
obj.bar = count + 2
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (return, return)', t => {
const firstFn = function (obj, count) {
return Object.assign({}, obj, { foo: count + 1 })
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})