2015-07-08 13:57:38 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
describe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
ddescribe,
|
|
|
|
expect,
|
|
|
|
inject,
|
|
|
|
beforeEach,
|
|
|
|
SpyObject
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
|
|
|
import {PathRecognizer} from 'angular2/src/router/path_recognizer';
|
2015-07-17 16:36:53 -04:00
|
|
|
import {parser, Url, RootUrl} from 'angular2/src/router/url_parser';
|
2015-07-08 13:57:38 -04:00
|
|
|
import {SyncRouteHandler} from 'angular2/src/router/sync_route_handler';
|
|
|
|
|
|
|
|
class DummyClass {
|
|
|
|
constructor() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
var mockRouteHandler = new SyncRouteHandler(DummyClass);
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('PathRecognizer', () => {
|
2015-07-17 05:01:34 -04:00
|
|
|
|
|
|
|
it('should throw when given an invalid path', () => {
|
|
|
|
expect(() => new PathRecognizer('/hi#', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "/hi#" should not include "#". Use "HashLocationStrategy" instead.`);
|
|
|
|
expect(() => new PathRecognizer('hi?', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "hi?" contains "?" which is not allowed in a route config.`);
|
|
|
|
expect(() => new PathRecognizer('hi;', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "hi;" contains ";" which is not allowed in a route config.`);
|
|
|
|
expect(() => new PathRecognizer('hi=', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "hi=" contains "=" which is not allowed in a route config.`);
|
|
|
|
expect(() => new PathRecognizer('hi(', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "hi(" contains "(" which is not allowed in a route config.`);
|
|
|
|
expect(() => new PathRecognizer('hi)', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "hi)" contains ")" which is not allowed in a route config.`);
|
|
|
|
expect(() => new PathRecognizer('hi//there', mockRouteHandler))
|
|
|
|
.toThrowError(`Path "hi//there" contains "//" which is not allowed in a route config.`);
|
|
|
|
});
|
|
|
|
|
2015-07-21 04:26:43 -04:00
|
|
|
describe('querystring params', () => {
|
|
|
|
it('should parse querystring params so long as the recognizer is a root', () => {
|
2015-07-17 16:36:53 -04:00
|
|
|
var rec = new PathRecognizer('/hello/there', mockRouteHandler);
|
|
|
|
var url = parser.parse('/hello/there?name=igor');
|
|
|
|
var match = rec.recognize(url);
|
|
|
|
expect(match.instruction.params).toEqual({'name': 'igor'});
|
2015-07-21 04:26:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a combined map of parameters with the param expected in the URL path',
|
|
|
|
() => {
|
2015-07-17 16:36:53 -04:00
|
|
|
var rec = new PathRecognizer('/hello/:name', mockRouteHandler);
|
|
|
|
var url = parser.parse('/hello/paul?topic=success');
|
|
|
|
var match = rec.recognize(url);
|
|
|
|
expect(match.instruction.params).toEqual({'name': 'paul', 'topic': 'success'});
|
2015-07-21 04:26:43 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-08 13:57:38 -04:00
|
|
|
describe('matrix params', () => {
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should be parsed along with dynamic paths', () => {
|
|
|
|
var rec = new PathRecognizer('/hello/:id', mockRouteHandler);
|
|
|
|
var url = new Url('hello', new Url('matias', null, null, {'key': 'value'}));
|
|
|
|
var match = rec.recognize(url);
|
|
|
|
expect(match.instruction.params).toEqual({'id': 'matias', 'key': 'value'});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should be parsed on a static path', () => {
|
|
|
|
var rec = new PathRecognizer('/person', mockRouteHandler);
|
|
|
|
var url = new Url('person', null, null, {'name': 'dave'});
|
|
|
|
var match = rec.recognize(url);
|
|
|
|
expect(match.instruction.params).toEqual({'name': 'dave'});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should be ignored on a wildcard segment', () => {
|
2015-07-08 13:57:38 -04:00
|
|
|
var rec = new PathRecognizer('/wild/*everything', mockRouteHandler);
|
2015-07-17 16:36:53 -04:00
|
|
|
var url = parser.parse('/wild/super;variable=value');
|
|
|
|
var match = rec.recognize(url);
|
|
|
|
expect(match.instruction.params).toEqual({'everything': 'super;variable=value'});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should set matrix param values to true when no value is present', () => {
|
|
|
|
var rec = new PathRecognizer('/path', mockRouteHandler);
|
|
|
|
var url = new Url('path', null, null, {'one': true, 'two': true, 'three': '3'});
|
|
|
|
var match = rec.recognize(url);
|
|
|
|
expect(match.instruction.params).toEqual({'one': true, 'two': true, 'three': '3'});
|
|
|
|
});
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should be parsed on the final segment of the path', () => {
|
|
|
|
var rec = new PathRecognizer('/one/two/three', mockRouteHandler);
|
|
|
|
|
|
|
|
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.recognize(one);
|
|
|
|
expect(match.instruction.params).toEqual({'c': '3'});
|
|
|
|
});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|