Nuxt/test/utils.test.js

206 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-01-11 21:51:52 +00:00
import ansiHTML from 'ansi-html'
import { Utils } from '..'
2016-12-10 09:57:50 +00:00
2018-03-18 19:31:32 +00:00
describe('utils', () => {
test('encodeHtml', () => {
const html = '<h1>Hello</h1>'
expect(Utils.encodeHtml(html)).toBe('&lt;h1&gt;Hello&lt;/h1&gt;')
})
2016-12-10 09:57:50 +00:00
2018-03-18 19:31:32 +00:00
test('getContext', () => {
let ctx = Utils.getContext({ a: 1 }, { b: 2 })
expect(Utils.getContext.length).toBe(2)
expect(typeof ctx.req).toBe('object')
expect(typeof ctx.res).toBe('object')
expect(ctx.req.a).toBe(1)
expect(ctx.res.b).toBe(2)
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('setAnsiColors', () => {
Utils.setAnsiColors(ansiHTML)
})
2016-12-10 11:31:48 +00:00
2018-03-18 19:31:32 +00:00
test('waitFor', async () => {
let s = Date.now()
await Utils.waitFor(100)
expect(Date.now() - s >= 100).toBe(true)
await Utils.waitFor()
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('timeout (promise)', async () => {
const result = await Utils.timeout(Promise.resolve('time not run out'), 100)
expect(result).toBe('time not run out')
})
2018-03-18 19:31:32 +00:00
test('timeout (async function)', async () => {
const result = await Utils.timeout(async () => {
await Utils.waitFor(10)
return 'time not run out'
}, 100)
expect(result).toBe('time not run out')
})
2018-03-18 19:31:32 +00:00
test('timeout (timeout in 100ms)', async () => {
const timeout = Utils.timeout(Utils.waitFor(200), 100, 'timeout test 100ms')
await expect(timeout).rejects.toThrow('timeout test 100ms')
})
2018-03-18 19:31:32 +00:00
test('timeout (async timeout in 100ms)', async () => {
const timeout = Utils.timeout(async () => {
await Utils.waitFor(500)
}, 100, 'timeout test 100ms')
await expect(timeout).rejects.toThrow('timeout test 100ms')
})
2018-03-18 19:31:32 +00:00
test('urlJoin', () => {
expect(Utils.urlJoin('test', '/about')).toBe('test/about')
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('promisifyRoute (array)', () => {
const array = [1]
const promise = Utils.promisifyRoute(array)
expect(typeof promise).toBe('object')
return promise.then(res => {
expect(res).toBe(array)
})
})
test('promisifyRoute (fn => array)', () => {
const array = [1, 2]
const fn = function () {
return array
}
const promise = Utils.promisifyRoute(fn)
expect(typeof promise).toBe('object')
return promise.then(res => {
expect(res).toBe(array)
})
2018-01-13 05:22:11 +00:00
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('promisifyRoute (fn => promise)', () => {
const array = [1, 2, 3]
const fn = function () {
return new Promise(resolve => {
resolve(array)
})
}
const promise = Utils.promisifyRoute(fn)
expect(typeof promise).toBe('object')
return promise.then(res => {
expect(res).toBe(array)
})
2018-01-13 05:22:11 +00:00
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('promisifyRoute ((fn(args) => promise))', () => {
const fn = function (array) {
return new Promise(resolve => {
resolve(array)
})
}
const array = [1, 2, 3]
const promise = Utils.promisifyRoute(fn, array)
expect(typeof promise).toBe('object')
return promise.then(res => {
expect(res).toBe(array)
2016-12-10 11:24:02 +00:00
})
2018-01-13 05:22:11 +00:00
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('promisifyRoute (fn(cb) with error)', () => {
const fn = function (cb) {
cb(new Error('Error here'))
}
const promise = Utils.promisifyRoute(fn)
expect(typeof promise).toBe('object')
return promise.catch(e => {
expect(e.message).toBe('Error here')
})
2018-01-13 05:22:11 +00:00
})
2018-03-18 19:31:32 +00:00
test('promisifyRoute (fn(cb, args) with error)', () => {
const fn = function (cb, array) {
cb(new Error('Error here: ' + array.join()))
}
const array = [1, 2, 3, 4]
const promise = Utils.promisifyRoute(fn, array)
expect(typeof promise).toBe('object')
return promise.catch(e => {
expect(e.message).toBe('Error here: ' + array.join())
})
2018-01-13 05:22:11 +00:00
})
2016-12-10 11:24:02 +00:00
2018-03-18 19:31:32 +00:00
test('promisifyRoute (fn(cb) with result)', () => {
const array = [1, 2, 3, 4]
const fn = function (cb) {
cb(null, array)
}
const promise = Utils.promisifyRoute(fn)
expect(typeof promise).toBe('object')
return promise.then(res => {
expect(res).toBe(array)
})
2018-01-13 05:22:11 +00:00
})
2018-03-18 19:31:32 +00:00
test('promisifyRoute (fn(cb, args) with result)', () => {
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)
expect(typeof promise).toBe('object')
return promise.then(res => {
expect(res.array).toBe(array)
expect(res.object).toBe(object)
})
2018-01-13 05:22:11 +00:00
})
2017-08-22 22:50:27 +00:00
2018-03-18 19:31:32 +00:00
test('chainFn (mutate, mutate)', () => {
// 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)
expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 })
2018-01-13 05:22:11 +00:00
})
2018-03-18 19:31:32 +00:00
test('chainFn (mutate, return)', () => {
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
2017-08-22 22:50:27 +00:00
2018-03-18 19:31:32 +00:00
const chainedFn = Utils.chainFn(firstFn, secondFn)
expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 })
})
2017-08-22 22:50:27 +00:00
2018-03-18 19:31:32 +00:00
test('chainFn (return, mutate)', () => {
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)
expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 })
})
2017-08-22 22:50:27 +00:00
2018-03-18 19:31:32 +00:00
test('chainFn (return, return)', () => {
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)
expect(chainedFn({}, 10)).toEqual({ foo: 11, bar: 12 })
})
2017-08-22 22:50:27 +00:00
})