Nuxt/test/unit/utils.test.js

332 lines
9.2 KiB
JavaScript
Raw Normal View History

import path from 'path'
import { Utils, waitUntil } from '../utils'
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', () => {
2018-08-08 10:54:05 +00:00
const ctx = Utils.getContext({ a: 1 }, { b: 2 })
2018-03-18 19:31:32 +00:00
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
test('waitFor', async () => {
const delay = 100
const s = process.hrtime()
await Utils.waitFor(delay)
const t = process.hrtime(s)
// Node.js makes no guarantees about the exact timing of when callbacks will fire
// HTML5 specifies a minimum delay of 4ms for timeouts
// although arbitrary, use this value to determine our lower limit
expect((t[0] * 1e9 + t[1]) / 1e6).not.toBeLessThan(delay - 4)
await Utils.waitFor()
})
2016-12-10 11:24:02 +00:00
test('waitUntil', async () => {
expect(await waitUntil(() => true, 0.1, 100)).toBe(false)
expect(await waitUntil(() => false, 0.1, 100)).toBe(true)
})
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) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
resolve(array)
})
}
const promise = Utils.promisifyRoute(fn)
expect(typeof promise).toBe('object')
return promise.then((res) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
resolve(array)
})
}
const array = [1, 2, 3]
const promise = Utils.promisifyRoute(fn, array)
expect(typeof promise).toBe('object')
return promise.then((res) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
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) => {
2018-03-18 19:31:32 +00:00
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 })
})
2018-04-03 13:44:59 +00:00
test('flatRoutes', () => {
const routes = Utils.flatRoutes([
{ name: 'login', path: '/login' },
{ name: 'about', path: '/about' },
{ name: 'posts',
path: '',
children: [
{ name: 'posts-list',
path: ''
},
{ name: 'posts-create',
path: 'post'
}
]
}
])
expect(routes).toMatchObject([ '/login', '/about', '', '/post' ])
})
describe('relativeTo', () => {
const path1 = path.join(path.sep, 'foo', 'bar')
const path2 = path.join(path.sep, 'foo', 'baz')
test('makes path relative to dir', () => {
expect(Utils.relativeTo(path1, path2)).toBe(Utils.wp(`..${path.sep}baz`))
})
test('keeps webpack inline loaders prepended', () => {
expect(Utils.relativeTo(path1, `loader1!loader2!${path2}`))
.toBe(Utils.wp(`loader1!loader2!..${path.sep}baz`))
})
})
describe('guardDir', () => {
test('Parent dir is guarded', () => {
expect(() => {
Utils.guardDir({
dir1: '/root/parent',
dir2: '/root'
}, 'dir1', 'dir2')
}).toThrow()
})
test('Same dir is guarded', () => {
expect(() => {
Utils.guardDir({
dir1: '/root/parent',
dir2: '/root/parent'
}, 'dir1', 'dir2')
}).toThrow()
})
test('Same level dir is not guarded', () => {
expect(() => {
Utils.guardDir({
dir1: '/root/parent-next',
dir2: '/root/parent'
}, 'dir1', 'dir2')
}).not.toThrow()
})
test('Same level dir is not guarded 2', () => {
expect(() => {
Utils.guardDir({
dir1: '/root/parent',
dir2: '/root/parent-next'
}, 'dir1', 'dir2')
}).not.toThrow()
})
test('Child dir is not guarded', () => {
expect(() => {
Utils.guardDir({
dir1: '/root/parent',
dir2: '/root/parent/child'
}, 'dir1', 'dir2')
}).not.toThrow()
})
})
2017-08-22 22:50:27 +00:00
})
2018-03-19 18:48:43 +00:00
test('createRoutes should allow snake case routes', () => {
const files = [
'pages/_param.vue',
'pages/subpage/_param.vue',
'pages/snake_case_route.vue',
'pages/another_route/_id.vue'
]
const srcDir = '/some/nuxt/app'
const pagesDir = 'pages'
const routesResult = Utils.createRoutes(files, srcDir, pagesDir)
const expectedResult = [
{
name: 'snake_case_route',
path: '/snake_case_route',
component: Utils.r('/some/nuxt/app/pages/snake_case_route.vue'),
chunkName: 'pages/snake_case_route'
},
{
name: 'another_route-id',
path: '/another_route/:id?',
component: Utils.r('/some/nuxt/app/pages/another_route/_id.vue'),
chunkName: 'pages/another_route/_id'
},
{
name: 'subpage-param',
path: '/subpage/:param?',
component: Utils.r('/some/nuxt/app/pages/subpage/_param.vue'),
chunkName: 'pages/subpage/_param'
},
{
name: 'param',
path: '/:param?',
component: Utils.r('/some/nuxt/app/pages/_param.vue'),
chunkName: 'pages/_param'
}
]
expect(routesResult).toEqual(expectedResult)
})