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'
|
|
|
|
|
|
|
|
let utils
|
|
|
|
// Init nuxt.js and create server listening on localhost:4000
|
|
|
|
test.before('Init Nuxt.js', async t => {
|
|
|
|
const Nuxt = require('../')
|
|
|
|
let nuxt = new Nuxt({ dev: false })
|
|
|
|
utils = nuxt.utils
|
|
|
|
})
|
2016-12-10 09:57:50 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2016-12-10 11:24:02 +00:00
|
|
|
|
2016-12-10 11:31:48 +00:00
|
|
|
test('setAnsiColors', t => {
|
|
|
|
utils.setAnsiColors(ansiHTML)
|
|
|
|
})
|
|
|
|
|
2016-12-10 11:24:02 +00:00
|
|
|
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)
|
2016-12-10 11:31:48 +00:00
|
|
|
t.is(typeof promise, 'object')
|
2016-12-10 11:24:02 +00:00
|
|
|
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)
|
2016-12-10 11:31:48 +00:00
|
|
|
t.is(typeof promise, 'object')
|
2016-12-10 11:24:02 +00:00
|
|
|
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)
|
2016-12-10 11:31:48 +00:00
|
|
|
t.is(typeof promise, 'object')
|
2016-12-10 11:24:02 +00:00
|
|
|
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)
|
2016-12-10 11:31:48 +00:00
|
|
|
t.is(typeof promise, 'object')
|
2016-12-10 11:24:02 +00:00
|
|
|
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)
|
2016-12-10 11:31:48 +00:00
|
|
|
t.is(typeof promise, 'object')
|
2016-12-10 11:24:02 +00:00
|
|
|
return promise
|
|
|
|
.then((res) => {
|
|
|
|
t.is(res, array)
|
|
|
|
})
|
|
|
|
})
|