fix(router): sort possible routes by cost

This commit is contained in:
Brian Ford
2015-05-12 14:53:13 -07:00
parent 8b6fa1cf19
commit 17392f663f
6 changed files with 78 additions and 33 deletions
+4
View File
@@ -23,6 +23,7 @@ export function main() {
recognizer.addConfig('/test', handler);
expect(recognizer.recognize('/test')[0]).toEqual({
'cost' : 1,
'handler': { 'components': { 'a': 'b' } },
'params': {},
'matchedUrl': '/test',
@@ -34,6 +35,7 @@ export function main() {
recognizer.addConfig('/', handler);
expect(recognizer.recognize('/')[0]).toEqual({
'cost': 0,
'handler': { 'components': { 'a': 'b' } },
'params': {},
'matchedUrl': '/',
@@ -44,6 +46,7 @@ export function main() {
it('should work with a dynamic segment', () => {
recognizer.addConfig('/user/:name', handler);
expect(recognizer.recognize('/user/brian')[0]).toEqual({
'cost': 101,
'handler': handler,
'params': { 'name': 'brian' },
'matchedUrl': '/user/brian',
@@ -57,6 +60,7 @@ export function main() {
var solutions = recognizer.recognize('/a');
expect(solutions.length).toBe(1);
expect(solutions[0]).toEqual({
'cost': 1,
'handler': handler,
'params': {},
'matchedUrl': '/b',
+18
View File
@@ -27,6 +27,24 @@ export function main() {
expect(instruction.getChildInstruction('default').component).toBe(DummyCompB);
});
it('should prefer static segments to dynamic', () => {
registry.config(rootHostComponent, {'path': '/:site', 'component': DummyCompB});
registry.config(rootHostComponent, {'path': '/home', 'component': DummyCompA});
var instruction = registry.recognize('/home', rootHostComponent);
expect(instruction.getChildInstruction('default').component).toBe(DummyCompA);
});
it('should prefer dynamic segments to star', () => {
registry.config(rootHostComponent, {'path': '/:site', 'component': DummyCompA});
registry.config(rootHostComponent, {'path': '/*site', 'component': DummyCompB});
var instruction = registry.recognize('/home', rootHostComponent);
expect(instruction.getChildInstruction('default').component).toBe(DummyCompA);
});
it('should match the full URL recursively', () => {
registry.config(rootHostComponent, {'path': '/first', 'component': DummyParentComp});