Skip to content

Commit 843acc4

Browse files
committed
test exposing route config custom fields
1 parent cd27d50 commit 843acc4

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

src/route.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,30 @@ export default class Route {
1111

1212
constructor (path, router) {
1313
let matched = router._recognizer.recognize(path)
14-
15-
// copy all custom fields from route configs
1614
if (matched) {
15+
// copy all custom fields from route configs
1716
[].forEach.call(matched, match => {
1817
for (let key in match.handler) {
1918
if (!internalKeysRE.test(key)) {
2019
this[key] = match.handler[key]
2120
}
2221
}
2322
})
23+
// set query and params
24+
this.query = matched.queryParams
25+
this.params = [].reduce.call(matched, (prev, cur) => {
26+
if (cur.params) {
27+
for (let key in cur.params) {
28+
prev[key] = cur.params[key]
29+
}
30+
}
31+
return prev
32+
}, {})
2433
}
25-
34+
// expose path and router
2635
this.path = path
2736
this.router = router
28-
this.query = matched
29-
? matched.queryParams
30-
: {}
31-
this.params = matched
32-
? [].reduce.call(matched, (prev, cur) => {
33-
if (cur.params) {
34-
for (let key in cur.params) {
35-
prev[key] = cur.params[key]
36-
}
37-
}
38-
return prev
39-
}, {})
40-
: {}
41-
42-
// private stuff
37+
// for internal use
4338
this._matched = matched || router._notFoundHandler
4439
}
4540
}

test/unit/specs/core.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ describe('Core', function () {
9292
router = new Router({ abstract: true })
9393
router.map({
9494
'/a/:id': {
95+
customField: 'custom',
9596
component: {
96-
template: '{{$route.path}},{{$route.params.id}},{{$route.query.id}}|'
97+
template: '{{$route.path}},{{$route.params.id}},{{$route.query.id}},{{$route.customField}}|'
9798
}
9899
}
99100
})
@@ -107,20 +108,20 @@ describe('Core', function () {
107108
'</div>',
108109
components: {
109110
'view-b': {
110-
template: '{{$route.path}},{{$route.params.id}},{{$route.query.id}}'
111+
template: '{{$route.path}},{{$route.params.id}},{{$route.query.id}},{{$route.customField}}'
111112
}
112113
}
113114
})
114115
router.start(App, el)
115116
assertRoutes([
116117
// no param, no match (only view-b)
117-
['/a', '/a,,'],
118+
['/a', '/a,,,'],
118119
// params only
119-
['/a/123', '/a/123,123,|/a/123,123,'],
120+
['/a/123', '/a/123,123,,custom|/a/123,123,,custom'],
120121
// params + query
121-
['/a/123?id=234', '/a/123?id=234,123,234|/a/123?id=234,123,234'],
122+
['/a/123?id=234', '/a/123?id=234,123,234,custom|/a/123?id=234,123,234,custom'],
122123
// relative query
123-
['?id=345', '/a/123?id=345,123,345|/a/123?id=345,123,345']
124+
['?id=345', '/a/123?id=345,123,345,custom|/a/123?id=345,123,345,custom']
124125
], done)
125126
})
126127

0 commit comments

Comments
 (0)