feat(router): introduce matrix params

Closes #2774
Closes #2989
This commit is contained in:
Matias Niemelä
2015-07-08 10:57:38 -07:00
parent 97ef1c27df
commit 5677bf73ca
4 changed files with 270 additions and 14 deletions
@@ -0,0 +1,72 @@
import {
AsyncTestCompleter,
describe,
it,
iit,
ddescribe,
expect,
inject,
beforeEach,
SpyObject
} from 'angular2/test_lib';
import {PathRecognizer} from 'angular2/src/router/path_recognizer';
import {SyncRouteHandler} from 'angular2/src/router/sync_route_handler';
class DummyClass {
constructor() {}
}
var mockRouteHandler = new SyncRouteHandler(DummyClass);
export function main() {
describe('PathRecognizer', () => {
describe('matrix params', () => {
it('should recognize a trailing matrix value on a path value and assign it to the params return value',
() => {
var rec = new PathRecognizer('/hello/:id', mockRouteHandler);
var params = rec.parseParams('/hello/matias;key=value');
expect(params['id']).toEqual('matias');
expect(params['key']).toEqual('value');
});
it('should recognize and parse multiple matrix params separated by a colon value', () => {
var rec = new PathRecognizer('/jello/:sid', mockRouteHandler);
var params = rec.parseParams('/jello/man;color=red;height=20');
expect(params['sid']).toEqual('man');
expect(params['color']).toEqual('red');
expect(params['height']).toEqual('20');
});
it('should recognize a matrix param value on a static path value', () => {
var rec = new PathRecognizer('/static/man', mockRouteHandler);
var params = rec.parseParams('/static/man;name=dave');
expect(params['name']).toEqual('dave');
});
it('should not parse matrix params when a wildcard segment is used', () => {
var rec = new PathRecognizer('/wild/*everything', mockRouteHandler);
var params = rec.parseParams('/wild/super;variable=value');
expect(params['everything']).toEqual('super;variable=value');
});
it('should set matrix param values to true when no value is present within the path string',
() => {
var rec = new PathRecognizer('/path', mockRouteHandler);
var params = rec.parseParams('/path;one;two;three=3');
expect(params['one']).toEqual(true);
expect(params['two']).toEqual(true);
expect(params['three']).toEqual('3');
});
it('should ignore earlier instances of matrix params and only consider the ones at the end of the path',
() => {
var rec = new PathRecognizer('/one/two/three', mockRouteHandler);
var params = rec.parseParams('/one;a=1/two;b=2/three;c=3');
expect(params).toEqual({'c': '3'});
});
});
});
}
@@ -10,6 +10,8 @@ import {
SpyObject
} from 'angular2/test_lib';
import {Map, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
import {RouteRecognizer, RouteMatch} from 'angular2/src/router/route_recognizer';
export function main() {
@@ -122,6 +124,79 @@ export function main() {
expect(() => recognizer.generate('user', {})['url'])
.toThrowError('Route generator for \'name\' was not included in parameters passed.');
});
describe('matrix params', () => {
it('should recognize matrix parameters within the URL path', () => {
var recognizer = new RouteRecognizer();
recognizer.addConfig('profile/:name', handler, 'user');
var solution = recognizer.recognize('/profile/matsko;comments=all')[0];
var params = solution.params();
expect(params['name']).toEqual('matsko');
expect(params['comments']).toEqual('all');
});
it('should recognize multiple matrix params and set parameters that contain no value to true',
() => {
var recognizer = new RouteRecognizer();
recognizer.addConfig('/profile/hello', handler, 'user');
var solution =
recognizer.recognize('/profile/hello;modal;showAll=true;hideAll=false')[0];
var params = solution.params();
expect(params['modal']).toEqual(true);
expect(params['showAll']).toEqual('true');
expect(params['hideAll']).toEqual('false');
});
it('should only consider the matrix parameters at the end of the path handler', () => {
var recognizer = new RouteRecognizer();
recognizer.addConfig('/profile/hi/:name', handler, 'user');
var solution = recognizer.recognize('/profile;a=1/hi;b=2;c=3/william;d=4')[0];
var params = solution.params();
expect(params).toEqual({'name': 'william', 'd': '4'});
});
it('should generate and populate the given static-based route with matrix params', () => {
var recognizer = new RouteRecognizer();
recognizer.addConfig('forum/featured', handler, 'forum-page');
var params = StringMapWrapper.create();
params['start'] = 10;
params['end'] = 100;
var result = recognizer.generate('forum-page', params);
expect(result['url']).toEqual('forum/featured;start=10;end=100');
});
it('should generate and populate the given dynamic-based route with matrix params', () => {
var recognizer = new RouteRecognizer();
recognizer.addConfig('forum/:topic', handler, 'forum-page');
var params = StringMapWrapper.create();
params['topic'] = 'crazy';
params['total-posts'] = 100;
params['moreDetail'] = null;
var result = recognizer.generate('forum-page', params);
expect(result['url']).toEqual('forum/crazy;total-posts=100;moreDetail');
});
it('should not apply any matrix params if a dynamic route segment takes up the slot when a path is generated',
() => {
var recognizer = new RouteRecognizer();
recognizer.addConfig('hello/:name', handler, 'profile-page');
var params = StringMapWrapper.create();
params['name'] = 'matsko';
var result = recognizer.generate('profile-page', params);
expect(result['url']).toEqual('hello/matsko');
});
});
});
}
@@ -115,6 +115,26 @@ export function main() {
expect(router.generate(['/firstCmp', 'secondCmp'])).toEqual('/first/second');
expect(router.generate(['/firstCmp/secondCmp'])).toEqual('/first/second');
});
describe('matrix params', () => {
it('should apply inline matrix params for each router path within the generated URL', () => {
router.config({'path': '/first/...', 'component': DummyParentComp, 'as': 'firstCmp'});
var path =
router.generate(['/firstCmp', {'key': 'value'}, 'secondCmp', {'project': 'angular'}]);
expect(path).toEqual('/first;key=value/second;project=angular');
});
it('should apply inline matrix params for each router path within the generated URL and also include named params',
() => {
router.config(
{'path': '/first/:token/...', 'component': DummyParentComp, 'as': 'firstCmp'});
var path =
router.generate(['/firstCmp', {'token': 'min'}, 'secondCmp', {'author': 'max'}]);
expect(path).toEqual('/first/min/second;author=max');
});
});
});
}