test(utils): use Nuxt.utils

This commit is contained in:
Pooya Parsa 2017-06-13 00:06:52 +04:30
parent e753f93f97
commit 84350e59c5

View File

@ -1,22 +1,15 @@
import test from 'ava' import test from 'ava'
import ansiHTML from 'ansi-html' import ansiHTML from 'ansi-html'
import Nuxt from '..'
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
})
test('encodeHtml', t => { test('encodeHtml', t => {
const html = '<h1>Hello</h1>' const html = '<h1>Hello</h1>'
t.is(utils.encodeHtml(html), '&lt;h1&gt;Hello&lt;/h1&gt;') t.is(Nuxt.utils.encodeHtml(html), '&lt;h1&gt;Hello&lt;/h1&gt;')
}) })
test('getContext', t => { test('getContext', t => {
let ctx = utils.getContext({ a: 1 }, { b: 2 }) let ctx = Nuxt.utils.getContext({ a: 1 }, { b: 2 })
t.is(utils.getContext.length, 2) t.is(Nuxt.utils.getContext.length, 2)
t.is(typeof ctx.req, 'object') t.is(typeof ctx.req, 'object')
t.is(typeof ctx.res, 'object') t.is(typeof ctx.res, 'object')
t.is(ctx.req.a, 1) t.is(ctx.req.a, 1)
@ -24,24 +17,24 @@ test('getContext', t => {
}) })
test('setAnsiColors', t => { test('setAnsiColors', t => {
utils.setAnsiColors(ansiHTML) Nuxt.utils.setAnsiColors(ansiHTML)
t.pass() t.pass()
}) })
test('waitFor', async (t) => { test('waitFor', async (t) => {
let s = Date.now() let s = Date.now()
await utils.waitFor(100) await Nuxt.utils.waitFor(100)
t.true(Date.now() - s >= 100) t.true(Date.now() - s >= 100)
await utils.waitFor() await Nuxt.utils.waitFor()
}) })
test('urlJoin', t => { test('urlJoin', t => {
t.is(utils.urlJoin('test', '/about'), 'test/about') t.is(Nuxt.utils.urlJoin('test', '/about'), 'test/about')
}) })
test('promisifyRoute (array)', t => { test('promisifyRoute (array)', t => {
const array = [1] const array = [1]
const promise = utils.promisifyRoute(array) const promise = Nuxt.utils.promisifyRoute(array)
t.is(typeof promise, 'object') t.is(typeof promise, 'object')
return promise return promise
.then((res) => { .then((res) => {
@ -54,7 +47,7 @@ test('promisifyRoute (fn => array)', t => {
const fn = function () { const fn = function () {
return array return array
} }
const promise = utils.promisifyRoute(fn) const promise = Nuxt.utils.promisifyRoute(fn)
t.is(typeof promise, 'object') t.is(typeof promise, 'object')
return promise return promise
.then((res) => { .then((res) => {
@ -69,7 +62,7 @@ test('promisifyRoute (fn => promise)', t => {
resolve(array) resolve(array)
}) })
} }
const promise = utils.promisifyRoute(fn) const promise = Nuxt.utils.promisifyRoute(fn)
t.is(typeof promise, 'object') t.is(typeof promise, 'object')
return promise return promise
.then((res) => { .then((res) => {
@ -81,7 +74,7 @@ test('promisifyRoute (fn(cb) with error)', t => {
const fn = function (cb) { const fn = function (cb) {
cb(new Error('Error here')) cb(new Error('Error here'))
} }
const promise = utils.promisifyRoute(fn) const promise = Nuxt.utils.promisifyRoute(fn)
t.is(typeof promise, 'object') t.is(typeof promise, 'object')
return promise return promise
.catch((e) => { .catch((e) => {
@ -94,7 +87,7 @@ test('promisifyRoute (fn(cb) with result)', t => {
const fn = function (cb) { const fn = function (cb) {
cb(null, array) cb(null, array)
} }
const promise = utils.promisifyRoute(fn) const promise = Nuxt.utils.promisifyRoute(fn)
t.is(typeof promise, 'object') t.is(typeof promise, 'object')
return promise return promise
.then((res) => { .then((res) => {