Skip to content

Commit 416714c

Browse files
committed
fix stringifyPath not formatting 0 (fix #343)
1 parent d27d32b commit 416714c

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/route-recognizer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ DynamicSegment.prototype = {
5959
},
6060

6161
generate: function(params) {
62-
return params[this.name] || ":" + this.name;
62+
var val = params[this.name];
63+
return val == null ? ":" + this.name : val;
6364
}
6465
};
6566

@@ -74,7 +75,8 @@ StarSegment.prototype = {
7475
},
7576

7677
generate: function(params) {
77-
return params[this.name] || ":" + this.name;
78+
var val = params[this.name];
79+
return val == null ? ":" + this.name : val;
7880
}
7981
};
8082

test/unit/specs/core.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,3 +1108,43 @@ describe('Core', function () {
11081108
target.dispatchEvent(e)
11091109
}
11101110
})
1111+
1112+
describe('Stringify Path', function () {
1113+
1114+
var router
1115+
beforeEach(function () {
1116+
router = new Router({ abstract: true })
1117+
})
1118+
1119+
it('plain string', function () {
1120+
expect(router._stringifyPath('a')).toBe('a')
1121+
})
1122+
1123+
it('object path', function () {
1124+
expect(router._stringifyPath({ path: '/hi' })).toBe('/hi')
1125+
expect(router._stringifyPath({ path: '/hi', query: { a: 1 } })).toBe('/hi?a=1')
1126+
expect(router._stringifyPath({ path: '/hi', query: { a: 1, b: 2 } })).toBe('/hi?a=1&b=2')
1127+
expect(router._stringifyPath({ path: '/hi?c=3', query: { a: 1, b: 2 } })).toBe('/hi?c=3&a=1&b=2')
1128+
})
1129+
1130+
it('named route', function () {
1131+
router.map({
1132+
'/test/:id': {
1133+
name: 'a',
1134+
component: {}
1135+
}
1136+
})
1137+
expect(router._stringifyPath({ name: 'a' })).toBe('/test/:id')
1138+
expect(router._stringifyPath({ name: 'a', params: { id: 0 } })).toBe('/test/0')
1139+
expect(router._stringifyPath({ name: 'a', params: { id: 'hi' } })).toBe('/test/hi')
1140+
})
1141+
1142+
it('named route not found should throw error', function () {
1143+
expect(function () {
1144+
router._stringifyPath({
1145+
name: 'a'
1146+
})
1147+
}).toThrow()
1148+
})
1149+
1150+
})

0 commit comments

Comments
 (0)