feat(router): add regex matchers
@petebacondarwin deserves credit for most of this commit. This allows you to specify a regex and serializer function instead of the path DSL in your route declaration. ``` @RouteConfig([ { regex: '[a-z]+.[0-9]+', serializer: (params) => `{params.a}.params.b}`, component: MyComponent } ]) class Component {} ``` Closes #7325 Closes #7126
This commit is contained in:

committed by
Vikram Subramanian

parent
2548ce86db
commit
75343eb340
@ -0,0 +1,103 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
SpyObject
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {ParamRoutePath} from 'angular2/src/router/rules/route_paths/param_route_path';
|
||||
import {parser, Url} from 'angular2/src/router/url_parser';
|
||||
|
||||
export function main() {
|
||||
describe('PathRecognizer', () => {
|
||||
|
||||
it('should throw when given an invalid path', () => {
|
||||
expect(() => new ParamRoutePath('/hi#'))
|
||||
.toThrowError(`Path "/hi#" should not include "#". Use "HashLocationStrategy" instead.`);
|
||||
expect(() => new ParamRoutePath('hi?'))
|
||||
.toThrowError(`Path "hi?" contains "?" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi;'))
|
||||
.toThrowError(`Path "hi;" contains ";" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi='))
|
||||
.toThrowError(`Path "hi=" contains "=" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi('))
|
||||
.toThrowError(`Path "hi(" contains "(" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi)'))
|
||||
.toThrowError(`Path "hi)" contains ")" which is not allowed in a route config.`);
|
||||
expect(() => new ParamRoutePath('hi//there'))
|
||||
.toThrowError(`Path "hi//there" contains "//" which is not allowed in a route config.`);
|
||||
});
|
||||
|
||||
describe('querystring params', () => {
|
||||
it('should parse querystring params so long as the recognizer is a root', () => {
|
||||
var rec = new ParamRoutePath('/hello/there');
|
||||
var url = parser.parse('/hello/there?name=igor');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'name': 'igor'});
|
||||
});
|
||||
|
||||
it('should return a combined map of parameters with the param expected in the URL path',
|
||||
() => {
|
||||
var rec = new ParamRoutePath('/hello/:name');
|
||||
var url = parser.parse('/hello/paul?topic=success');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'name': 'paul', 'topic': 'success'});
|
||||
});
|
||||
});
|
||||
|
||||
describe('matrix params', () => {
|
||||
it('should be parsed along with dynamic paths', () => {
|
||||
var rec = new ParamRoutePath('/hello/:id');
|
||||
var url = new Url('hello', new Url('matias', null, null, {'key': 'value'}));
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'id': 'matias', 'key': 'value'});
|
||||
});
|
||||
|
||||
it('should be parsed on a static path', () => {
|
||||
var rec = new ParamRoutePath('/person');
|
||||
var url = new Url('person', null, null, {'name': 'dave'});
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'name': 'dave'});
|
||||
});
|
||||
|
||||
it('should be ignored on a wildcard segment', () => {
|
||||
var rec = new ParamRoutePath('/wild/*everything');
|
||||
var url = parser.parse('/wild/super;variable=value');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'everything': 'super;variable=value'});
|
||||
});
|
||||
|
||||
it('should set matrix param values to true when no value is present', () => {
|
||||
var rec = new ParamRoutePath('/path');
|
||||
var url = new Url('path', null, null, {'one': true, 'two': true, 'three': '3'});
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'one': true, 'two': true, 'three': '3'});
|
||||
});
|
||||
|
||||
it('should be parsed on the final segment of the path', () => {
|
||||
var rec = new ParamRoutePath('/one/two/three');
|
||||
|
||||
var three = new Url('three', null, null, {'c': '3'});
|
||||
var two = new Url('two', three, null, {'b': '2'});
|
||||
var one = new Url('one', two, null, {'a': '1'});
|
||||
|
||||
var match = rec.matchUrl(one);
|
||||
expect(match.allParams).toEqual({'c': '3'});
|
||||
});
|
||||
});
|
||||
|
||||
describe('wildcard segment', () => {
|
||||
it('should return a url path which matches the original url path', () => {
|
||||
var rec = new ParamRoutePath('/wild/*everything');
|
||||
var url = parser.parse('/wild/super;variable=value/anotherPartAfterSlash');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.urlPath).toEqual('wild/super;variable=value/anotherPartAfterSlash');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
inject,
|
||||
beforeEach,
|
||||
SpyObject
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {GeneratedUrl} from 'angular2/src/router/rules/route_paths/route_path';
|
||||
import {RegexRoutePath} from 'angular2/src/router/rules/route_paths/regex_route_path';
|
||||
import {parser, Url} from 'angular2/src/router/url_parser';
|
||||
|
||||
function emptySerializer(params) {
|
||||
return new GeneratedUrl('', {});
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('RegexRoutePath', () => {
|
||||
|
||||
it('should throw when given an invalid regex',
|
||||
() => { expect(() => new RegexRoutePath('[abc', emptySerializer)).toThrowError(); });
|
||||
|
||||
it('should parse a single param using capture groups', () => {
|
||||
var rec = new RegexRoutePath('^(.+)$', emptySerializer);
|
||||
var url = parser.parse('hello');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'0': 'hello', '1': 'hello'});
|
||||
});
|
||||
|
||||
it('should parse multiple params using capture groups', () => {
|
||||
var rec = new RegexRoutePath('^(.+)\\.(.+)$', emptySerializer);
|
||||
var url = parser.parse('hello.goodbye');
|
||||
var match = rec.matchUrl(url);
|
||||
expect(match.allParams).toEqual({'0': 'hello.goodbye', '1': 'hello', '2': 'goodbye'});
|
||||
});
|
||||
|
||||
it('should generate a url by calling the provided serializer', () => {
|
||||
function serializer(params) {
|
||||
return new GeneratedUrl(`/a/${params['a']}/b/${params['b']}`, {});
|
||||
}
|
||||
var rec = new RegexRoutePath('/a/(.+)/b/(.+)$', serializer);
|
||||
var params = {a: 'one', b: 'two'};
|
||||
var url = rec.generateUrl(params);
|
||||
expect(url.urlPath).toEqual('/a/one/b/two');
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user