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-06-30 16:18:51 -04:00
|
|
|
import {RouteRecognizer, RouteMatch} from 'angular2/src/router/route_recognizer';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('RouteRecognizer', () => {
|
|
|
|
var recognizer;
|
2015-06-30 16:18:51 -04:00
|
|
|
var handler = {'component': DummyCmpA};
|
|
|
|
var handler2 = {'component': DummyCmpB};
|
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', () => {
|
|
|
|
recognizer.addConfig('/test', handler);
|
2015-06-30 16:18:51 -04:00
|
|
|
var solution = recognizer.recognize('/test')[0];
|
|
|
|
expect(getComponentType(solution)).toEqual(handler['component']);
|
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', () => {
|
|
|
|
recognizer.addConfig('/', handler);
|
|
|
|
var solution = recognizer.recognize('/')[0];
|
2015-06-30 16:18:51 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(handler['component']);
|
2015-05-05 18:19:06 -04:00
|
|
|
});
|
|
|
|
|
2015-05-15 05:05:57 -04:00
|
|
|
|
|
|
|
it('should recognize a dynamic segment', () => {
|
2015-04-17 12:59:56 -04:00
|
|
|
recognizer.addConfig('/user/:name', handler);
|
2015-05-15 05:05:57 -04:00
|
|
|
var solution = recognizer.recognize('/user/brian')[0];
|
2015-06-30 16:18:51 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(handler['component']);
|
|
|
|
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', () => {
|
|
|
|
recognizer.addConfig('/first/*rest', handler);
|
|
|
|
var solution = recognizer.recognize('/first/second/third')[0];
|
2015-06-30 16:18:51 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(handler['component']);
|
|
|
|
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', () => {
|
|
|
|
recognizer.addConfig('/hello', handler);
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(() => recognizer.addConfig('/hello', handler2))
|
|
|
|
.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', () => {
|
|
|
|
recognizer.addConfig('/hello/:person/how/:doyoudou', handler);
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(() => recognizer.addConfig('/hello/:friend/how/:areyou', handler2))
|
|
|
|
.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-04-17 12:59:56 -04:00
|
|
|
recognizer.addRedirect('/a', '/b');
|
|
|
|
recognizer.addConfig('/b', handler);
|
|
|
|
var solutions = recognizer.recognize('/a');
|
|
|
|
expect(solutions.length).toBe(1);
|
2015-05-15 05:05:57 -04:00
|
|
|
|
|
|
|
var solution = solutions[0];
|
2015-06-30 16:18:51 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(handler['component']);
|
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', () => {
|
|
|
|
recognizer.addRedirect('/', '/foo');
|
|
|
|
recognizer.addConfig('/bar', handler);
|
|
|
|
var solutions = recognizer.recognize('/bar');
|
|
|
|
expect(solutions.length).toBe(1);
|
|
|
|
|
|
|
|
var solution = solutions[0];
|
2015-06-30 16:18:51 -04:00
|
|
|
expect(getComponentType(solution)).toEqual(handler['component']);
|
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',
|
|
|
|
() => {
|
|
|
|
recognizer.addRedirect('/', '/matias');
|
|
|
|
recognizer.addConfig('/matias', handler);
|
2015-06-15 03:45:02 -04:00
|
|
|
|
2015-06-17 14:57:38 -04:00
|
|
|
recognizer.addConfig('/fatias', handler);
|
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-04-17 12:59:56 -04:00
|
|
|
recognizer.addConfig('/app/user/:name', handler, '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-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-06-30 16:18:51 -04:00
|
|
|
recognizer.addConfig('app/user/:name', handler, 'user');
|
|
|
|
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-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 {}
|