-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit tests for @nuxt/util (#4880)
- Loading branch information
Showing
16 changed files
with
929 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
import serialize from 'serialize-javascript' | ||
|
||
export function serializeFunction(func) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('<h1>Hello</h1>') | ||
}) | ||
|
||
test('should join url', () => { | ||
expect(urlJoin('test', '/about')).toEqual('test/about') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.