Nuxt/test/utils.test.js

181 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-12-10 09:57:50 +00:00
import test from 'ava'
2017-01-11 21:51:52 +00:00
import ansiHTML from 'ansi-html'
import { Utils } from '..'
2016-12-10 09:57:50 +00:00
test('encodeHtml', t => {
const html = '<h1>Hello</h1>'
2017-06-12 20:15:28 +00:00
t.is(Utils.encodeHtml(html), '&lt;h1&gt;Hello&lt;/h1&gt;')
2016-12-10 09:57:50 +00:00
})
test('getContext', t => {
2017-06-12 20:15:28 +00:00
let ctx = Utils.getContext({ a: 1 }, { b: 2 })
t.is(Utils.getContext.length, 2)
2016-12-10 09:57:50 +00:00
t.is(typeof ctx.req, 'object')
t.is(typeof ctx.res, 'object')
t.is(ctx.req.a, 1)
t.is(ctx.res.b, 2)
})
2016-12-10 11:24:02 +00:00
2016-12-10 11:31:48 +00:00
test('setAnsiColors', t => {
2017-06-12 20:15:28 +00:00
Utils.setAnsiColors(ansiHTML)
2017-04-13 09:32:29 +00:00
t.pass()
2016-12-10 11:31:48 +00:00
})
2018-01-13 05:22:11 +00:00
test('waitFor', async t => {
2016-12-10 11:24:02 +00:00
let s = Date.now()
2017-06-12 20:15:28 +00:00
await Utils.waitFor(100)
2016-12-10 11:24:02 +00:00
t.true(Date.now() - s >= 100)
2017-06-12 20:15:28 +00:00
await Utils.waitFor()
2016-12-10 11:24:02 +00:00
})
test('urlJoin', t => {
2017-06-12 20:15:28 +00:00
t.is(Utils.urlJoin('test', '/about'), 'test/about')
2016-12-10 11:24:02 +00:00
})
2017-03-17 17:52:46 +00:00
test('promisifyRoute (array)', t => {
2016-12-10 11:24:02 +00:00
const array = [1]
2017-06-12 20:15:28 +00:00
const promise = Utils.promisifyRoute(array)
2016-12-10 11:31:48 +00:00
t.is(typeof promise, 'object')
2018-01-13 05:22:11 +00:00
return promise.then(res => {
t.is(res, array)
})
2016-12-10 11:24:02 +00:00
})
2017-03-17 17:52:46 +00:00
test('promisifyRoute (fn => array)', t => {
2016-12-10 11:24:02 +00:00
const array = [1, 2]
const fn = function () {
return array
}
2017-06-12 20:15:28 +00:00
const promise = Utils.promisifyRoute(fn)
2016-12-10 11:31:48 +00:00
t.is(typeof promise, 'object')
2018-01-13 05:22:11 +00:00
return promise.then(res => {
t.is(res, array)
})
2016-12-10 11:24:02 +00:00
})
2017-03-17 17:52:46 +00:00
test('promisifyRoute (fn => promise)', t => {
2016-12-10 11:24:02 +00:00
const array = [1, 2, 3]
const fn = function () {
2018-01-13 05:22:11 +00:00
return new Promise(resolve => {
2016-12-10 11:24:02 +00:00
resolve(array)
})
}
2017-06-12 20:15:28 +00:00
const promise = Utils.promisifyRoute(fn)
2016-12-10 11:31:48 +00:00
t.is(typeof promise, 'object')
2018-01-13 05:22:11 +00:00
return promise.then(res => {
t.is(res, array)
})
2016-12-10 11:24:02 +00:00
})
test('promisifyRoute ((fn(args) => promise))', t => {
const fn = function (array) {
2018-01-13 05:22:11 +00:00
return new Promise(resolve => {
resolve(array)
})
}
const array = [1, 2, 3]
const promise = Utils.promisifyRoute(fn, array)
t.is(typeof promise, 'object')
2018-01-13 05:22:11 +00:00
return promise.then(res => {
t.is(res, array)
})
})
2017-03-17 17:52:46 +00:00
test('promisifyRoute (fn(cb) with error)', t => {
2016-12-10 11:24:02 +00:00
const fn = function (cb) {
2017-03-20 16:52:35 +00:00
cb(new Error('Error here'))
2016-12-10 11:24:02 +00:00
}
2017-06-12 20:15:28 +00:00
const promise = Utils.promisifyRoute(fn)
2016-12-10 11:31:48 +00:00
t.is(typeof promise, 'object')
2018-01-13 05:22:11 +00:00
return promise.catch(e => {
t.is(e.message, 'Error here')
})
2016-12-10 11:24:02 +00:00
})
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')
2018-01-13 05:22:11 +00:00
return promise.catch(e => {
t.is(e.message, 'Error here: ' + array.join())
})
})
2017-03-17 17:52:46 +00:00
test('promisifyRoute (fn(cb) with result)', t => {
2016-12-10 11:24:02 +00:00
const array = [1, 2, 3, 4]
const fn = function (cb) {
cb(null, array)
}
2017-06-12 20:15:28 +00:00
const promise = Utils.promisifyRoute(fn)
2016-12-10 11:31:48 +00:00
t.is(typeof promise, 'object')
2018-01-13 05:22:11 +00:00
return promise.then(res => {
t.is(res, array)
})
2016-12-10 11:24:02 +00:00
})
2017-08-22 22:50:27 +00:00
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')
2018-01-13 05:22:11 +00:00
return promise.then(res => {
t.is(res.array, array)
t.is(res.object, object)
})
})
2017-08-22 22:50:27 +00:00
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)
})