Skip to content

Commit ef54dce

Browse files
committed
test: add tests for utils
1 parent 70ed9cc commit ef54dce

File tree

10 files changed

+62
-117
lines changed

10 files changed

+62
-117
lines changed

src/util.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,32 +134,19 @@ export function resolveAsyncComponent (handler, cb) {
134134
* @param {Object} query
135135
*/
136136

137-
export function mapParams (path, params, query) {
138-
for (let key in params) {
139-
path = replaceParam(path, params, key)
140-
}
137+
export function mapParams (path, params = {}, query) {
138+
path = path.replace(/:([^\/]+)/g, (_, key) => {
139+
let val = params[key]
140+
if (!val) {
141+
warn(
142+
'param "' + key + '" not found when generating ' +
143+
'path for "' + path + '" with params ' + JSON.stringify(params)
144+
)
145+
}
146+
return val || ''
147+
})
141148
if (query) {
142149
path += genQuery(query)
143150
}
144151
return path
145152
}
146-
147-
/**
148-
* Replace a param segment with real value in a matched
149-
* path.
150-
*
151-
* @param {String} path
152-
* @param {Object} params
153-
* @param {String} key
154-
* @return {String}
155-
*/
156-
157-
function replaceParam (path, params, key) {
158-
let regex = new RegExp(':' + key + '(\\/|$)')
159-
let value = params[key]
160-
return path.replace(regex, m =>
161-
m.charAt(m.length - 1) === '/'
162-
? value + '/'
163-
: value
164-
)
165-
}

test/unit/specs/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Vue = require('vue')
1111
var Router = require('../../../src')
1212
Vue.use(Router)
1313

14+
require('./util')
1415
require('./core')
1516

1617
describe('Pipeline', function () {

test/unit/specs/pipeline/activate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44
var routerUtil = require('../../../../src/util')

test/unit/specs/pipeline/can-activate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44

test/unit/specs/pipeline/can-deactivate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44

test/unit/specs/pipeline/can-reuse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44

test/unit/specs/pipeline/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44
var routerUtil = require('../../../../src/util')

test/unit/specs/pipeline/deactivate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44

test/unit/specs/pipeline/full.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var testUtils = require('../util')
1+
var testUtils = require('../../lib/pipeline-test-util')
22
var test = testUtils.test
33
var assertCalls = testUtils.assertCalls
44
var util = require('../../../../src/util')

test/unit/specs/util.js

Lines changed: 43 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,51 @@
1-
var Vue = require('vue')
2-
var Router = require('../../../src')
3-
var Emitter = require('events').EventEmitter
1+
var util = require('../../../src/util')
42

5-
/**
6-
* Setup a router app for testing with two nested routes:
7-
*
8-
* - /a/b
9-
* - /c/d
10-
*
11-
* @param {Object} configs - an object that contains the
12-
* route configs for each component.
13-
* @param {Function} cb(router, calls, emitter)
14-
*/
3+
describe('Util', function () {
154

16-
exports.test = function (configs, cb) {
17-
var emitter = new Emitter()
18-
var router = new Router({ abstract: true })
19-
var el = document.createElement('div')
20-
var App = Vue.extend({ template: '<div><router-view></router-view></div>' })
21-
var calls = []
22-
// wrap hooks
23-
Object.keys(configs).forEach(function (route) {
24-
var config = configs[route]
25-
Object.keys(config).forEach(function (hook) {
26-
var fn = config[hook]
27-
config[hook] = function (transition) {
28-
var event = route + '.' + hook
29-
calls.push(event)
30-
var res = typeof fn === 'function'
31-
? fn(transition)
32-
: fn
33-
emitter.emit(event)
34-
return res
35-
}
36-
})
5+
it('resolvePath', function () {
6+
expect(util.resolvePath('/a', 'b')).toBe('/b')
7+
expect(util.resolvePath('/a/', 'b')).toBe('/a/b')
8+
expect(util.resolvePath('/a', '/b')).toBe('/b')
9+
expect(util.resolvePath('/a/', '/b')).toBe('/a/b')
10+
// append mode
11+
expect(util.resolvePath('/a', 'b', true)).toBe('/a/b')
12+
expect(util.resolvePath('/a/', 'b', true)).toBe('/a/b')
13+
expect(util.resolvePath('/a', '/b', true)).toBe('/a/b')
14+
expect(util.resolvePath('/a/', '/b', true)).toBe('/a/b')
15+
// relative query
16+
expect(util.resolvePath('/a', '?b=1')).toBe('/a?b=1')
17+
expect(util.resolvePath('/a/', '?b=1')).toBe('/a/?b=1')
3718
})
38-
router.map({
39-
'/a': {
40-
component: {
41-
template: 'A <router-view></router-view>',
42-
route: configs.a
43-
},
44-
subRoutes: {
45-
'/b': {
46-
component: {
47-
template: 'B',
48-
route: configs.b
49-
}
50-
},
51-
'/e': {
52-
component: {
53-
template: 'E'
54-
}
55-
}
56-
}
57-
},
58-
'/c': {
59-
component: {
60-
template: 'C <router-view></router-view>',
61-
route: configs.c
62-
},
63-
subRoutes: {
64-
'/d': {
65-
component: {
66-
template: 'D',
67-
route: configs.d
68-
}
69-
}
70-
}
71-
},
72-
'/data/:msg': {
73-
component: {
74-
route: configs.data,
75-
template:
76-
'<span v-if="$loadingRouteData">loading...</span>' +
77-
'<span v-if="!$loadingRouteData">{{msg}}</span>',
78-
data: function () {
79-
return {
80-
msg: 'default'
81-
}
82-
}
19+
20+
it('mapParams', function () {
21+
expect(util.mapParams('/a/:id', { id: 'b' })).toBe('/a/b')
22+
expect(util.mapParams('/a/:id/', { id: 'b' })).toBe('/a/b/')
23+
expect(util.mapParams('/a/:id/c/:d', { id: 'b', d: 'd' })).toBe('/a/b/c/d')
24+
expect(util.mapParams('/a/:id/c/:d', { id: 'b', d: 'd' }, { e: 123 })).toBe('/a/b/c/d?e=123')
25+
})
26+
27+
it('resolveAsyncComponent', function (done) {
28+
var handler = {
29+
component: function (resolve) {
30+
setTimeout(function () {
31+
resolve({
32+
template: 'hi'
33+
})
34+
}, 0)
8335
}
8436
}
37+
util.resolveAsyncComponent(handler, function (Component) {
38+
expect(Component.options.template).toBe('hi')
39+
done()
40+
})
8541
})
86-
router.start(App, el)
87-
cb(router, calls, emitter)
88-
}
8942

90-
exports.assertCalls = function (calls, expects) {
91-
expects.forEach(function (e, i) {
92-
expect(calls[i]).toBe(e)
43+
it('getRouteConfig', function () {
44+
expect(util.getRouteConfig({}, 'data')).toBeUndefined()
45+
expect(util.getRouteConfig({ options: { route: {}}}, 'data')).toBeUndefined()
46+
expect(util.getRouteConfig({ options: { route: { data: 1 }}}, 'data')).toBe(1)
47+
expect(util.getRouteConfig({ $options: { route: {}}}, 'data')).toBeUndefined()
48+
expect(util.getRouteConfig({ $options: { route: { data: 1 }}}, 'data')).toBe(1)
9349
})
94-
}
50+
51+
})

0 commit comments

Comments
 (0)