2015-04-17 12:59:56 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
describe,
|
2015-05-29 17:58:41 -04:00
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
ddescribe,
|
|
|
|
expect,
|
|
|
|
inject,
|
|
|
|
beforeEach,
|
|
|
|
SpyObject
|
|
|
|
} from 'angular2/test_lib';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
import {RouteRecognizer} from 'angular2/src/router/route_recognizer';
|
|
|
|
import {ComponentInstruction} from 'angular2/src/router/instruction';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-07-13 19:12:48 -04:00
|
|
|
import {Route, Redirect} from 'angular2/src/router/route_config_decorator';
|
2015-07-17 16:36:53 -04:00
|
|
|
import {parser} from 'angular2/src/router/url_parser';
|
2015-07-13 19:12:48 -04:00
|
|
|
|
2015-04-17 12:59:56 -04:00
|
|
|
export function main() {
|
|
|
|
describe('RouteRecognizer', () => {
|
|
|
|
var recognizer;
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
beforeEach(() => { recognizer = new RouteRecognizer(); });
|
2015-04-17 12:59:56 -04:00
|
|
|
|
|
|
|
|
2015-05-15 05:05:57 -04:00
|
|
|
it('should recognize a static segment', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/test', component: DummyCmpA}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/test');
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
|
2015-05-05 18:19:06 -04:00
|
|
|
|
2015-05-15 05:05:57 -04:00
|
|
|
it('should recognize a single slash', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/', component: DummyCmpA}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/');
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-05-05 18:19:06 -04:00
|
|
|
});
|
|
|
|
|
2015-05-15 05:05:57 -04:00
|
|
|
|
|
|
|
it('should recognize a dynamic segment', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/user/:name', component: DummyCmpA}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/user/brian');
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(solution.params).toEqual({'name': 'brian'});
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
|
2015-05-15 05:05:57 -04:00
|
|
|
|
|
|
|
it('should recognize a star segment', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/first/*rest', component: DummyCmpA}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/first/second/third');
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(solution.params).toEqual({'rest': 'second/third'});
|
2015-05-15 05:05:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw when given two routes that start with the same static segment', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/hello', component: DummyCmpA}));
|
|
|
|
expect(() => recognizer.config(new Route({path: '/hello', component: DummyCmpB})))
|
2015-05-29 17:58:41 -04:00
|
|
|
.toThrowError('Configuration \'/hello\' conflicts with existing route \'/hello\'');
|
2015-05-15 05:05:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw when given two routes that have dynamic segments in the same order', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/hello/:person/how/:doyoudou', component: DummyCmpA}));
|
|
|
|
expect(() => recognizer.config(
|
|
|
|
new Route({path: '/hello/:friend/how/:areyou', component: DummyCmpA})))
|
2015-05-29 17:58:41 -04:00
|
|
|
.toThrowError(
|
|
|
|
'Configuration \'/hello/:friend/how/:areyou\' conflicts with existing route \'/hello/:person/how/:doyoudou\'');
|
2015-05-15 05:05:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should recognize redirects', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/b', component: DummyCmpA}));
|
2015-07-17 16:36:53 -04:00
|
|
|
recognizer.config(new Redirect({path: '/a', redirectTo: 'b'}));
|
|
|
|
var solution = recognize(recognizer, '/a');
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(solution.urlPath).toEqual('b');
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
|
2015-06-15 03:45:02 -04:00
|
|
|
it('should not perform root URL redirect on a non-root route', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Redirect({path: '/', redirectTo: '/foo'}));
|
|
|
|
recognizer.config(new Route({path: '/bar', component: DummyCmpA}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/bar');
|
|
|
|
expect(solution.componentType).toEqual(DummyCmpA);
|
|
|
|
expect(solution.urlPath).toEqual('bar');
|
2015-06-15 03:45:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should perform a root URL redirect only for root routes', () => {
|
|
|
|
recognizer.config(new Redirect({path: '/', redirectTo: '/matias'}));
|
|
|
|
recognizer.config(new Route({path: '/matias', component: DummyCmpA}));
|
|
|
|
recognizer.config(new Route({path: '/fatias', component: DummyCmpA}));
|
2015-06-15 03:45:02 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution;
|
2015-06-15 03:45:02 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
solution = recognize(recognizer, '/');
|
|
|
|
expect(solution.urlPath).toEqual('matias');
|
|
|
|
|
|
|
|
solution = recognize(recognizer, '/fatias');
|
|
|
|
expect(solution.urlPath).toEqual('fatias');
|
|
|
|
|
|
|
|
solution = recognize(recognizer, '');
|
|
|
|
expect(solution.urlPath).toEqual('matias');
|
|
|
|
});
|
2015-06-17 14:57:38 -04:00
|
|
|
|
2015-05-15 05:05:57 -04:00
|
|
|
|
2015-06-30 16:18:51 -04:00
|
|
|
it('should generate URLs with params', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/app/user/:name', component: DummyCmpA, as: 'user'}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction = recognizer.generate('user', {'name': 'misko'});
|
|
|
|
expect(instruction.urlPath).toEqual('app/user/misko');
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
2015-05-06 21:30:37 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
|
2015-07-07 16:05:05 -04:00
|
|
|
it('should generate URLs with numeric params', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/app/page/:number', component: DummyCmpA, as: 'page'}));
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(recognizer.generate('page', {'number': 42}).urlPath).toEqual('app/page/42');
|
2015-07-07 16:05:05 -04:00
|
|
|
});
|
2015-05-15 05:05:57 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
|
2015-05-06 21:30:37 -04:00
|
|
|
it('should throw in the absence of required params URLs', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: 'app/user/:name', component: DummyCmpA, as: 'user'}));
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(() => recognizer.generate('user', {}))
|
2015-05-29 17:58:41 -04:00
|
|
|
.toThrowError('Route generator for \'name\' was not included in parameters passed.');
|
2015-05-06 21:30:37 -04:00
|
|
|
});
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-21 04:26:43 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
describe('params', () => {
|
|
|
|
it('should recognize parameters within the URL path', () => {
|
|
|
|
recognizer.config(new Route({path: 'profile/:name', component: DummyCmpA, as: 'user'}));
|
|
|
|
var solution = recognize(recognizer, '/profile/matsko?comments=all');
|
|
|
|
expect(solution.params).toEqual({'name': 'matsko', 'comments': 'all'});
|
2015-07-21 04:26:43 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
|
2015-07-21 04:26:43 -04:00
|
|
|
it('should generate and populate the given static-based route with querystring params',
|
|
|
|
() => {
|
|
|
|
recognizer.config(
|
|
|
|
new Route({path: 'forum/featured', component: DummyCmpA, as: 'forum-page'}));
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var params = {'start': 10, 'end': 100};
|
2015-07-21 04:26:43 -04:00
|
|
|
|
|
|
|
var result = recognizer.generate('forum-page', params);
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(result.urlPath).toEqual('forum/featured');
|
|
|
|
expect(result.urlParams).toEqual(['start=10', 'end=100']);
|
2015-07-21 04:26:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should prefer positional params over query params', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: 'profile/:name', component: DummyCmpA, as: 'user'}));
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/profile/yegor?name=igor');
|
|
|
|
expect(solution.params).toEqual({'name': 'yegor'});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should ignore matrix params for the top-level component', () => {
|
2015-07-21 04:26:43 -04:00
|
|
|
recognizer.config(new Route({path: '/home/:subject', component: DummyCmpA, as: 'user'}));
|
2015-07-17 16:36:53 -04:00
|
|
|
var solution = recognize(recognizer, '/home;sort=asc/zero;one=1?two=2');
|
|
|
|
expect(solution.params).toEqual({'subject': 'zero', 'two': '2'});
|
2015-07-21 04:26:43 -04:00
|
|
|
});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
}
|
2015-06-30 16:18:51 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
function recognize(recognizer: RouteRecognizer, url: string): ComponentInstruction {
|
|
|
|
return recognizer.recognize(parser.parse(url))[0].instruction;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getComponentType(routeMatch: ComponentInstruction): any {
|
|
|
|
return routeMatch.componentType;
|
2015-06-30 16:18:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class DummyCmpA {}
|
|
|
|
class DummyCmpB {}
|