Skip to content

Commit cef85ce

Browse files
committed
unit tests
1 parent 1b0ab07 commit cef85ce

File tree

10 files changed

+299
-7
lines changed

10 files changed

+299
-7
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"build": "rollup -c && uglifyjs dist/vue-router.js -c -m -o dist/vue-router.min.js",
2222
"lint": "eslint src examples",
2323
"test": "npm run lint && flow check && npm run test:e2e",
24+
"test:unit": "jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
2425
"test:e2e": "node test/e2e/runner.js"
2526
},
2627
"devDependencies": {
@@ -40,6 +41,7 @@
4041
"express": "^4.14.0",
4142
"express-urlrewrite": "^1.2.0",
4243
"flow-bin": "^0.29.0",
44+
"jasmine": "^2.4.1",
4345
"nightwatch": "^0.9.5",
4446
"nightwatch-helpers": "^1.0.0",
4547
"path-to-regexp": "^1.5.3",

src/util/location.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ export function normalizeLocation (
1414
}
1515

1616
const parsedPath = parsePath(next.path || '')
17-
const path = resolvePath(parsedPath.path, current && current.path, append)
17+
const path = parsedPath.path
18+
? resolvePath(parsedPath.path, current && current.path, append)
19+
: (current && current.path) || '/'
1820
const query = resolveQuery(parsedPath.query, next.query)
19-
const hash = parsedPath.hash
21+
let hash = next.hash || parsedPath.hash
22+
if (hash && hash.charAt(0) !== '#') {
23+
hash = `#${hash}`
24+
}
2025

2126
return {
2227
_normalized: true,

src/util/path.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function resolvePath (
99
return relative
1010
}
1111

12-
if (relative.charAt(0) === '?') {
12+
if (relative.charAt(0) === '?' || relative.charAt(0) === '#') {
1313
return base + relative
1414
}
1515

@@ -44,9 +44,9 @@ export function resolvePath (
4444
}
4545

4646
export function parsePath (path: string): {
47-
path: string,
48-
query: string,
49-
hash: string
47+
path: string;
48+
query: string;
49+
hash: string;
5050
} {
5151
let hash = ''
5252
let query = ''

src/util/query.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* @flow */
22

3+
import { warn } from './warn'
4+
35
const encode = encodeURIComponent
46
const decode = decodeURIComponent
57

@@ -12,7 +14,7 @@ export function resolveQuery (
1214
try {
1315
parsedQuery = parseQuery(query)
1416
} catch (e) {
15-
console.error(e)
17+
warn(false, e.message)
1618
parsedQuery = {}
1719
}
1820
for (const key in extraQuery) {

test/unit/jasmine.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"spec_dir": "test/unit/specs",
3+
"spec_files": [
4+
"*.spec.js"
5+
],
6+
"helpers": [
7+
"../../../node_modules/babel-register/lib/node.js"
8+
]
9+
}

test/unit/specs/async.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { runQueue } from '../../../src/util/async'
2+
3+
describe('Async utils', () => {
4+
describe('runQueue', () => {
5+
it('should work', done => {
6+
const calls = []
7+
const queue = [1, 2, 3, 4, 5].map(i => next => {
8+
calls.push(i)
9+
setTimeout(done, 0)
10+
})
11+
runQueue(queue, (fn, next) => fn(next), () => {
12+
expect(calls).toEqual([1, 2, 3, 4, 5])
13+
done()
14+
})
15+
})
16+
})
17+
})

test/unit/specs/location.spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { normalizeLocation } from '../../../src/util/location'
2+
3+
describe('Location utils', () => {
4+
describe('normalizeLocation', () => {
5+
it('string', () => {
6+
const loc = normalizeLocation('/abc?foo=bar&baz=qux#hello')
7+
expect(loc._normalized).toBe(true)
8+
expect(loc.path).toBe('/abc')
9+
expect(loc.hash).toBe('#hello')
10+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
11+
foo: 'bar',
12+
baz: 'qux'
13+
}))
14+
})
15+
16+
it('relative', () => {
17+
const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', {
18+
path: '/root/next'
19+
})
20+
expect(loc._normalized).toBe(true)
21+
expect(loc.path).toBe('/root/abc')
22+
expect(loc.hash).toBe('#hello')
23+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
24+
foo: 'bar',
25+
baz: 'qux'
26+
}))
27+
})
28+
29+
it('relative append', () => {
30+
const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', {
31+
path: '/root/next'
32+
}, true)
33+
expect(loc._normalized).toBe(true)
34+
expect(loc.path).toBe('/root/next/abc')
35+
expect(loc.hash).toBe('#hello')
36+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
37+
foo: 'bar',
38+
baz: 'qux'
39+
}))
40+
})
41+
42+
it('relative query & hash', () => {
43+
const loc = normalizeLocation('?foo=bar&baz=qux#hello', {
44+
path: '/root/next'
45+
})
46+
expect(loc._normalized).toBe(true)
47+
expect(loc.path).toBe('/root/next')
48+
expect(loc.hash).toBe('#hello')
49+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
50+
foo: 'bar',
51+
baz: 'qux'
52+
}))
53+
})
54+
55+
it('object', () => {
56+
const loc = normalizeLocation({
57+
path: '/abc?foo=bar#hello',
58+
query: { baz: 'qux' },
59+
hash: 'lol'
60+
})
61+
expect(loc._normalized).toBe(true)
62+
expect(loc.path).toBe('/abc')
63+
expect(loc.hash).toBe('#lol')
64+
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
65+
foo: 'bar',
66+
baz: 'qux'
67+
}))
68+
})
69+
70+
it('skip normalized', () => {
71+
const loc1 = {
72+
_normalized: true,
73+
path: '/abc?foo=bar#hello',
74+
query: { baz: 'qux' },
75+
hash: 'lol'
76+
}
77+
const loc2 = normalizeLocation(loc1)
78+
expect(loc1).toBe(loc2)
79+
})
80+
})
81+
})

test/unit/specs/path.spec.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { resolvePath, parsePath, cleanPath } from '../../../src/util/path'
2+
3+
describe('Path utils', () => {
4+
describe('resolvePath', () => {
5+
it('absolute', () => {
6+
const path = resolvePath('/a', '/b')
7+
expect(path).toBe('/a')
8+
})
9+
10+
it('relative', () => {
11+
const path = resolvePath('c/d', '/b')
12+
expect(path).toBe('/c/d')
13+
})
14+
15+
it('relative with append', () => {
16+
const path = resolvePath('c/d', '/b', true)
17+
expect(path).toBe('/b/c/d')
18+
})
19+
20+
it('relative parent', () => {
21+
const path = resolvePath('../d', '/a/b/c')
22+
expect(path).toBe('/a/d')
23+
})
24+
25+
it('relative parent with append', () => {
26+
const path = resolvePath('../d', '/a/b/c', true)
27+
expect(path).toBe('/a/b/d')
28+
})
29+
30+
it('relative query', () => {
31+
const path = resolvePath('?foo=bar', '/a/b')
32+
expect(path).toBe('/a/b?foo=bar')
33+
})
34+
35+
it('relative hash', () => {
36+
const path = resolvePath('#hi', '/a/b')
37+
expect(path).toBe('/a/b#hi')
38+
})
39+
})
40+
41+
describe('parsePath', () => {
42+
it('plain', () => {
43+
const res = parsePath('/a')
44+
expect(res.path).toBe('/a')
45+
expect(res.hash).toBe('')
46+
expect(res.query).toBe('')
47+
})
48+
49+
it('query', () => {
50+
const res = parsePath('/a?foo=bar???')
51+
expect(res.path).toBe('/a')
52+
expect(res.hash).toBe('')
53+
expect(res.query).toBe('foo=bar???')
54+
})
55+
56+
it('hash', () => {
57+
const res = parsePath('/a#haha#hoho')
58+
expect(res.path).toBe('/a')
59+
expect(res.hash).toBe('#haha#hoho')
60+
expect(res.query).toBe('')
61+
})
62+
63+
it('both', () => {
64+
const res = parsePath('/a?foo=bar#ok?baz=qux')
65+
expect(res.path).toBe('/a')
66+
expect(res.hash).toBe('#ok?baz=qux')
67+
expect(res.query).toBe('foo=bar')
68+
})
69+
})
70+
71+
describe('cleanPath', () => {
72+
it('should work', () => {
73+
const path = cleanPath('//a//b//d/')
74+
expect(path).toBe('/a/b/d/')
75+
})
76+
})
77+
})

test/unit/specs/query.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { resolveQuery, stringifyQuery } from '../../../src/util/query'
2+
3+
describe('Query utils', () => {
4+
describe('resolveQuery', () => {
5+
it('should work', () => {
6+
const query = resolveQuery('foo=bar&foo=k', { baz: 'qux' })
7+
expect(JSON.stringify(query)).toBe(JSON.stringify({
8+
foo: ['bar', 'k'],
9+
baz: 'qux'
10+
}))
11+
})
12+
})
13+
14+
describe('stringifyQuery', () => {
15+
it('should work', () => {
16+
expect(stringifyQuery({
17+
foo: 'bar',
18+
baz: 'qux',
19+
arr: [1, 2]
20+
})).toBe('?arr=1&arr=2&baz=qux&foo=bar') // sorted alphabetically
21+
})
22+
})
23+
})

test/unit/specs/route.spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { isSameRoute, isIncludedRoute } from '../../../src/util/route'
2+
3+
describe('Route utils', () => {
4+
describe('isSameRoute', () => {
5+
it('path', () => {
6+
const a = {
7+
path: '/a',
8+
hash: '#hi',
9+
query: { foo: 'bar', arr: [1, 2] }
10+
}
11+
const b = {
12+
path: '/a',
13+
hash: '#hi',
14+
query: { arr: ['1', '2'], foo: 'bar' }
15+
}
16+
expect(isSameRoute(a, b)).toBe(true)
17+
})
18+
19+
it('name', () => {
20+
const a = {
21+
path: '/abc',
22+
name: 'a',
23+
hash: '#hi',
24+
query: { foo: 'bar', arr: [1, 2] }
25+
}
26+
const b = {
27+
name: 'a',
28+
hash: '#hi',
29+
query: { arr: ['1', '2'], foo: 'bar' }
30+
}
31+
expect(isSameRoute(a, b)).toBe(true)
32+
})
33+
})
34+
35+
describe('isIncludedRoute', () => {
36+
it('path', () => {
37+
const a = { path: '/a/b' }
38+
const b = { path: '/a' }
39+
const c = { path: '/a/b/c' }
40+
expect(isIncludedRoute(a, b)).toBe(true)
41+
expect(isIncludedRoute(a, c)).toBe(false)
42+
})
43+
44+
it('with hash', () => {
45+
const a = { path: '/a/b', hash: '#a' }
46+
const b = { path: '/a' }
47+
const c = { path: '/a', hash: '#a' }
48+
const d = { path: '/a', hash: '#b' }
49+
expect(isIncludedRoute(a, b)).toBe(true)
50+
expect(isIncludedRoute(a, c)).toBe(true)
51+
expect(isIncludedRoute(a, d)).toBe(false)
52+
})
53+
54+
it('with query', () => {
55+
const a = { path: '/a/b', query: { foo: 'bar', baz: 'qux' }}
56+
const b = { path: '/a', query: {}}
57+
const c = { path: '/a', query: { foo: 'bar' }}
58+
const d = { path: '/a', query: { foo: 'bar', a: 'b' }}
59+
expect(isIncludedRoute(a, b)).toBe(true)
60+
expect(isIncludedRoute(a, c)).toBe(true)
61+
expect(isIncludedRoute(a, d)).toBe(false)
62+
})
63+
64+
it('with both', () => {
65+
const a = { path: '/a/b', query: { foo: 'bar', baz: 'qux' }, hash: '#a' }
66+
const b = { path: '/a', query: {}}
67+
const c = { path: '/a', query: { foo: 'bar' }}
68+
const d = { path: '/a', query: { foo: 'bar' }, hash: '#b' }
69+
const e = { path: '/a', query: { a: 'b' }, hash: '#a' }
70+
expect(isIncludedRoute(a, b)).toBe(true)
71+
expect(isIncludedRoute(a, c)).toBe(true)
72+
expect(isIncludedRoute(a, d)).toBe(false)
73+
expect(isIncludedRoute(a, e)).toBe(false)
74+
})
75+
})
76+
})

0 commit comments

Comments
 (0)