Skip to content

Commit

Permalink
test: unit tests for @nuxt/util (#4880)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo authored and pi0 committed Jan 29, 2019
1 parent 5101dc6 commit 96bab9f
Show file tree
Hide file tree
Showing 16 changed files with 929 additions and 331 deletions.
2 changes: 1 addition & 1 deletion packages/utils/src/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const encodeHtml = function encodeHtml(str) {

export const isString = obj => typeof obj === 'string' || obj instanceof String

export const isNonEmptyString = obj => obj && isString(obj)
export const isNonEmptyString = obj => Boolean(obj && isString(obj))

export const isPureObject = function isPureObject(o) {
return !Array.isArray(o) && typeof o === 'object'
Expand Down
4 changes: 1 addition & 3 deletions packages/utils/src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ export const startsWithRootAlias = startsWithAlias(['@@', '~~'])
export const isWindows = /^win/.test(process.platform)

export const wp = function wp(p = '') {
/* istanbul ignore if */
if (isWindows) {
return p.replace(/\\/g, '\\\\')
}
return p
}

export const wChunk = function wChunk(p = '') {
/* istanbul ignore if */
if (isWindows) {
return p.replace(/\//g, '_')
}
Expand Down Expand Up @@ -62,7 +60,7 @@ export const relativeTo = function relativeTo() {
// Make correct relative path
let rp = path.relative(dir, _path)
if (rp[0] !== '.') {
rp = './' + rp
rp = '.' + path.sep + rp
}

return wp(rp)
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const flatRoutes = function flatRoutes(router, _path = '', routes = []) {
if ([':', '*'].some(c => r.path.includes(c))) {
return
}
/* istanbul ignore if */
if (r.children) {
if (_path === '' && r.path === '/') {
routes.push('/')
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/serialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import serialize from 'serialize-javascript'

export function serializeFunction(func) {
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const parallel = function parallel(tasks, fn) {
}

export const chainFn = function chainFn(base, fn) {
/* istanbul ignore if */
if (typeof fn !== 'function') {
return base
}
Expand Down
121 changes: 121 additions & 0 deletions packages/utils/test/__snapshots__/route.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`util: route util: route create createRoutes should allow snake case routes in posix system 1`] = `
Array [
Object {
"chunkName": "pages/parent/index",
"component": "/some/nuxt/app/pages/parent/index.vue",
"name": "parent",
"path": "/parent",
},
Object {
"chunkName": "pages/snake_case_route",
"component": "/some/nuxt/app/pages/snake_case_route.vue",
"name": "snake_case_route",
"path": "/snake_case_route",
},
Object {
"chunkName": "pages/parent/child/index",
"component": "/some/nuxt/app/pages/parent/child/index.vue",
"name": "parent-child",
"path": "/parent/child",
},
Object {
"chunkName": "pages/parent/child/test",
"component": "/some/nuxt/app/pages/parent/child/test.vue",
"name": "parent-child-test",
"path": "/parent/child/test",
},
Object {
"children": Array [
Object {
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
"name": "another_route-id",
"path": "",
},
],
"chunkName": "pages/another_route/_id",
"component": "/some/nuxt/app/pages/another_route/_id.vue",
"path": "/another_route/:id?",
},
Object {
"chunkName": "pages/subpage/_param",
"component": "/some/nuxt/app/pages/subpage/_param.vue",
"name": "subpage-param",
"path": "/subpage/:param?",
},
Object {
"chunkName": "pages/index",
"component": "/some/nuxt/app/pages/index.vue",
"name": "index",
"path": "/",
},
Object {
"chunkName": "pages/_param",
"component": "/some/nuxt/app/pages/_param.vue",
"name": "param",
"path": "/:param",
},
]
`;

exports[`util: route util: route create createRoutes should allow snake case routes in windows system 1`] = `
Array [
Object {
"chunkName": "pages/parent/index",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\parent\\\\\\\\index.vue",
"name": "parent",
"path": "/parent",
},
Object {
"chunkName": "pages/snake_case_route",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\snake_case_route.vue",
"name": "snake_case_route",
"path": "/snake_case_route",
},
Object {
"chunkName": "pages/parent/child/index",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\parent\\\\\\\\child\\\\\\\\index.vue",
"name": "parent-child",
"path": "/parent/child",
},
Object {
"chunkName": "pages/parent/child/test",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\parent\\\\\\\\child\\\\\\\\test.vue",
"name": "parent-child-test",
"path": "/parent/child/test",
},
Object {
"children": Array [
Object {
"chunkName": "pages/another_route/_id",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\another_route\\\\\\\\_id.vue",
"name": "another_route-id",
"path": "",
},
],
"chunkName": "pages/another_route/_id",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\another_route\\\\\\\\_id.vue",
"path": "/another_route/:id?",
},
Object {
"chunkName": "pages/subpage/_param",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\subpage\\\\\\\\_param.vue",
"name": "subpage-param",
"path": "/subpage/:param?",
},
Object {
"chunkName": "pages/index",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\index.vue",
"name": "index",
"path": "/",
},
Object {
"chunkName": "pages/_param",
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\_param.vue",
"name": "param",
"path": "/:param",
},
]
`;
24 changes: 24 additions & 0 deletions packages/utils/test/context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getContext, determineGlobals } from '../src/context'

describe('util: context', () => {
test('should get context with req and res', () => {
const ctx = getContext({ a: 1 }, { b: 2 })

expect(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)
})

test('should get correct globals', () => {
const globals = {
foo: name => `${name}: foo`,
bar: name => `${name}: bar`,
baz: 'baz'
}
const result = determineGlobals('global', globals)

expect(result).toEqual({ bar: 'global: bar', foo: 'global: foo', baz: 'baz' })
})
})
22 changes: 22 additions & 0 deletions packages/utils/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as Util from '../src'
import * as context from '../src/context'
import * as lang from '../src/lang'
import * as resolve from '../src/resolve'
import * as route from '../src/route'
import * as serialize from '../src/serialize'
import * as task from '../src/task'
import * as timer from '../src/timer'

describe('util: entry', () => {
test('should export all methods from utils folder', () => {
expect(Util).toEqual({
...context,
...lang,
...resolve,
...route,
...serialize,
...task,
...timer
})
})
})
55 changes: 55 additions & 0 deletions packages/utils/test/lang.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
encodeHtml, isString, isNonEmptyString,
isPureObject, isUrl, urlJoin, wrapArray, stripWhitespace
} from '../src/lang'

describe('util: lang', () => {
test('should check if given argument is string', () => {
expect(isString('str')).toEqual(true)
expect(isString(String(100))).toEqual(true)
expect(isString(100)).toEqual(false)
expect(isString([])).toEqual(false)
})

test('should check if given argument is empty string', () => {
expect(isNonEmptyString('str')).toEqual(true)
expect(isNonEmptyString([])).toEqual(false)
expect(isNonEmptyString('')).toEqual(false)
})

test('should check if given argument is pure object', () => {
expect(isPureObject({})).toEqual(true)
expect(isPureObject([])).toEqual(false)
expect(isPureObject(Number('1'))).toEqual(false)
})

test('should check if given argument is url', () => {
expect(isUrl('http://localhost')).toEqual(true)
expect(isUrl('https://localhost')).toEqual(true)
expect(isUrl('//localhost')).toEqual(true)
expect(isUrl('localhost')).toEqual(false)
})

test('should wrap given argument with array', () => {
expect(wrapArray([ 'array' ])).toEqual([ 'array' ])
expect(wrapArray('str')).toEqual([ 'str' ])
})

test('should strip white spaces in given argument', () => {
expect(stripWhitespace('foo')).toEqual('foo')
expect(stripWhitespace('foo\t\r\f\n')).toEqual('foo\n')
expect(stripWhitespace('foo{\n\n\n')).toEqual('foo{\n')
expect(stripWhitespace('\n\n\n\f\r\f}')).toEqual('\n\f\r\f}')
expect(stripWhitespace('foo\n\n\nbar')).toEqual('foo\n\nbar')
expect(stripWhitespace('foo\n\n\n')).toEqual('foo\n')
})

test('should encode html', () => {
const html = '<h1>Hello</h1>'
expect(encodeHtml(html)).toEqual('&lt;h1&gt;Hello&lt;/h1&gt;')
})

test('should join url', () => {
expect(urlJoin('test', '/about')).toEqual('test/about')
})
})
85 changes: 85 additions & 0 deletions packages/utils/test/resolve.posix.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import consola from 'consola'

import {
startsWithAlias, startsWithSrcAlias, wp, wChunk,
relativeTo, defineAlias, isIndexFileAndFolder, getMainModule
} from '../src/resolve'

describe.posix('util: resolve', () => {
test('should check if path starts with alias', () => {
expect(startsWithAlias(['/var'])('/var/nuxt/src')).toEqual(true)
})

test('should check if path starts with root alias', () => {
expect(startsWithSrcAlias('@/assets')).toEqual(true)
expect(startsWithSrcAlias('~/pages')).toEqual(true)
})

test('should check if path starts with src alias', () => {
expect(startsWithSrcAlias('@@/src/assets')).toEqual(true)
expect(startsWithSrcAlias('~~/src/pages')).toEqual(true)
})

test('should return same path in linux', () => {
expect(wp('/var/nuxt\\ src/')).toEqual('/var/nuxt\\ src/')
})

test('should return same path in linux', () => {
expect(wChunk('nuxt/layout/test')).toEqual('nuxt/layout/test')
})

test('should define alias', () => {
const nuxt = {}
const server = {
name: 'nuxt',
bound: () => 'bound fn',
test: () => 'test defineAlias'
}

defineAlias(nuxt, server, ['name', 'bound'])
defineAlias(nuxt, server, ['test'], { bind: false, warn: true })

expect(nuxt.name).toEqual(server.name)
expect(nuxt.bound).not.toBe(server.bound)
expect(nuxt.bound()).toEqual('bound fn')
expect(nuxt.test).toBe(server.test)
expect(nuxt.test()).toEqual('test defineAlias')
expect(consola.warn).toBeCalledTimes(1)
expect(consola.warn).toBeCalledWith({
message: `'test' is deprecated'`,
additional: expect.any(String)
})
})

test('should check if given argument is index file or folder', () => {
expect(isIndexFileAndFolder(['/var/nuxt/plugins/test'])).toEqual(false)
expect(isIndexFileAndFolder(['/var/nuxt/plugins/test/index.js'])).toEqual(false)
expect(isIndexFileAndFolder(['/var/nuxt/plugins/test', '/var/nuxt/plugins/test/index.js'])).toEqual(true)
})

test('should return main module', () => {
expect(getMainModule()).toHaveProperty('children', 'exports', 'filename', 'path')
})

describe('relativeTo', () => {
const path1 = '@/foo'
const path2 = '@/bar'

test('should resolve alias path', () => {
expect(relativeTo(path1, path2)).toBe('@/bar')
})

test('should keep webpack inline loaders prepended', () => {
expect(relativeTo(path1, `loader1!loader2!${path2}`))
.toEqual('loader1!loader2!@/bar')
})

test('should check path which is not started with alias', () => {
expect(relativeTo('/var/nuxt/foo/bar', '/var/nuxt/foo/baz')).toBe('../baz')
})

test('should check path which is not started with alias ', () => {
expect(relativeTo('/var/nuxt/foo', '/var/nuxt/foo/bar')).toBe('./bar')
})
})
})

0 comments on commit 96bab9f

Please sign in to comment.