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-08 13:57:38 -04:00
|
|
|
import {Map, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
|
2015-06-30 16:18:51 -04:00
|
|
|
import {RouteRecognizer, RouteMatch} from 'angular2/src/router/route_recognizer';
|
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-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-06-30 16:18:51 -04:00
|
|
|
var solution = recognizer.recognize('/test')[0];
|
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-05-15 05:05:57 -04:00
|
|
|
var solution = recognizer.recognize('/')[0];
|
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-05-15 05:05:57 -04:00
|
|
|
var solution = recognizer.recognize('/user/brian')[0];
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-06-30 16:18:51 -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-05-15 05:05:57 -04:00
|
|
|
var solution = recognizer.recognize('/first/second/third')[0];
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-06-30 16:18:51 -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 Redirect({path: '/a', redirectTo: '/b'}));
|
|
|
|
recognizer.config(new Route({path: '/b', component: DummyCmpA}));
|
2015-04-17 12:59:56 -04:00
|
|
|
var solutions = recognizer.recognize('/a');
|
|
|
|
expect(solutions.length).toBe(1);
|
2015-05-15 05:05:57 -04:00
|
|
|
|
|
|
|
var solution = solutions[0];
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-05-15 05:05:57 -04:00
|
|
|
expect(solution.matchedUrl).toEqual('/b');
|
2015-04-17 12:59:56 -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-06-15 03:45:02 -04:00
|
|
|
var solutions = recognizer.recognize('/bar');
|
|
|
|
expect(solutions.length).toBe(1);
|
|
|
|
|
|
|
|
var solution = solutions[0];
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(DummyCmpA);
|
2015-06-15 03:45:02 -04:00
|
|
|
expect(solution.matchedUrl).toEqual('/bar');
|
|
|
|
});
|
|
|
|
|
2015-06-17 14:57:38 -04:00
|
|
|
it('should perform a root URL redirect when only a slash or an empty string is being processed',
|
|
|
|
() => {
|
2015-07-13 19:12:48 -04:00
|
|
|
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-06-17 14:57:38 -04:00
|
|
|
var solutions;
|
2015-06-15 03:45:02 -04:00
|
|
|
|
2015-06-17 14:57:38 -04:00
|
|
|
solutions = recognizer.recognize('/');
|
|
|
|
expect(solutions[0].matchedUrl).toBe('/matias');
|
2015-06-15 03:45:02 -04:00
|
|
|
|
2015-06-17 14:57:38 -04:00
|
|
|
solutions = recognizer.recognize('/fatias');
|
|
|
|
expect(solutions[0].matchedUrl).toBe('/fatias');
|
|
|
|
|
|
|
|
solutions = recognizer.recognize('');
|
|
|
|
expect(solutions[0].matchedUrl).toBe('/matias');
|
|
|
|
});
|
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-06-30 16:18:51 -04:00
|
|
|
expect(recognizer.generate('user', {'name': 'misko'})['url']).toEqual('app/user/misko');
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
2015-05-06 21:30:37 -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-07 16:05:05 -04:00
|
|
|
expect(recognizer.generate('page', {'number': 42})['url']).toEqual('app/page/42');
|
|
|
|
});
|
2015-05-15 05:05:57 -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-06-30 16:18:51 -04:00
|
|
|
expect(() => recognizer.generate('user', {})['url'])
|
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
|
|
|
|
|
|
|
describe('matrix params', () => {
|
|
|
|
it('should recognize matrix parameters within the URL path', () => {
|
|
|
|
var recognizer = new RouteRecognizer();
|
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
|
|
|
|
|
|
|
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();
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/profile/hello', component: DummyCmpA, as: 'user'}));
|
2015-07-08 13:57:38 -04:00
|
|
|
|
|
|
|
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();
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(new Route({path: '/profile/hi/:name', component: DummyCmpA, as: 'user'}));
|
2015-07-08 13:57:38 -04:00
|
|
|
|
|
|
|
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();
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(
|
|
|
|
new Route({path: 'forum/featured', component: DummyCmpA, as: 'forum-page'}));
|
2015-07-08 13:57:38 -04:00
|
|
|
|
|
|
|
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();
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(
|
|
|
|
new Route({path: 'forum/:topic', component: DummyCmpA, as: 'forum-page'}));
|
2015-07-08 13:57:38 -04:00
|
|
|
|
|
|
|
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();
|
2015-07-13 19:12:48 -04:00
|
|
|
recognizer.config(
|
|
|
|
new Route({path: 'hello/:name', component: DummyCmpA, as: 'profile-page'}));
|
2015-07-08 13:57:38 -04:00
|
|
|
|
|
|
|
var params = StringMapWrapper.create();
|
|
|
|
params['name'] = 'matsko';
|
|
|
|
|
|
|
|
var result = recognizer.generate('profile-page', params);
|
|
|
|
expect(result['url']).toEqual('hello/matsko');
|
|
|
|
});
|
|
|
|
});
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
}
|
2015-06-30 16:18:51 -04:00
|
|
|
|
|
|
|
function getComponentType(routeMatch: RouteMatch): any {
|
|
|
|
return routeMatch.recognizer.handler.componentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DummyCmpA {}
|
|
|
|
class DummyCmpB {}
|