| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   proxy, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   beforeEachBindings, | 
					
						
							|  |  |  |   SpyObject | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							|  |  |  | import {IMPLEMENTS} from 'angular2/src/facade/lang'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | import {ListWrapper} from 'angular2/src/facade/collection'; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | import {Router, RootRouter} from 'angular2/src/router/router'; | 
					
						
							|  |  |  | import {Pipeline} from 'angular2/src/router/pipeline'; | 
					
						
							|  |  |  | import {RouterOutlet} from 'angular2/src/router/router_outlet'; | 
					
						
							|  |  |  | import {SpyLocation} from 'angular2/src/mock/location_mock'; | 
					
						
							|  |  |  | import {Location} from 'angular2/src/router/location'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {RouteRegistry} from 'angular2/src/router/route_registry'; | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | import {RouteConfig} from 'angular2/src/router/route_config_decorator'; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {bind} from 'angular2/di'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('Router', () => { | 
					
						
							|  |  |  |     var router, location; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEachBindings(() => [ | 
					
						
							|  |  |  |       Pipeline, | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  |       RouteRegistry, | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |       DirectiveResolver, | 
					
						
							|  |  |  |       bind(Location).toClass(SpyLocation), | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |       bind(Router) | 
					
						
							|  |  |  |           .toFactory((registry, pipeline, | 
					
						
							|  |  |  |                       location) => { return new RootRouter(registry, pipeline, location, AppCmp); }, | 
					
						
							|  |  |  |                      [RouteRegistry, Pipeline, Location]) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |     ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(inject([Router, Location], (rtr, loc) => { | 
					
						
							|  |  |  |       router = rtr; | 
					
						
							|  |  |  |       location = loc; | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should navigate based on the initial URL state', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 13:59:14 -07:00
										 |  |  |          router.config({'path': '/', 'component': DummyComponent}) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |              .then((_) => router.registerOutlet(outlet)) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2015-07-07 15:44:29 -07:00
										 |  |  |                expect(outlet.spy('commit')).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |                expect(location.urlChanges).toEqual([]); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should activate viewports and update URL on navigate', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerOutlet(outlet) | 
					
						
							| 
									
										
										
										
											2015-05-21 13:59:14 -07:00
										 |  |  |              .then((_) => router.config({'path': '/a', 'component': DummyComponent})) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |              .then((_) => router.navigate('/a')) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2015-07-07 15:44:29 -07:00
										 |  |  |                expect(outlet.spy('commit')).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |                expect(location.urlChanges).toEqual(['/a']); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |     it('should navigate after being configured', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerOutlet(outlet) | 
					
						
							|  |  |  |              .then((_) => router.navigate('/a')) | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2015-07-07 15:44:29 -07:00
										 |  |  |                expect(outlet.spy('commit')).not.toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-05-21 13:59:14 -07:00
										 |  |  |                return router.config({'path': '/a', 'component': DummyComponent}); | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |              }) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2015-07-07 15:44:29 -07:00
										 |  |  |                expect(outlet.spy('commit')).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when linkParams does not start with a "/" or "./"', () => { | 
					
						
							|  |  |  |       expect(() => router.generate(['firstCmp', 'secondCmp'])) | 
					
						
							|  |  |  |           .toThrowError( | 
					
						
							|  |  |  |               `Link "${ListWrapper.toJSON(['firstCmp', 'secondCmp'])}" must start with "/", "./", or "../"`); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when linkParams does not include a route name', () => { | 
					
						
							|  |  |  |       expect(() => router.generate(['./'])) | 
					
						
							|  |  |  |           .toThrowError(`Link "${ListWrapper.toJSON(['./'])}" must include a route name.`); | 
					
						
							|  |  |  |       expect(() => router.generate(['/'])) | 
					
						
							|  |  |  |           .toThrowError(`Link "${ListWrapper.toJSON(['/'])}" must include a route name.`); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should generate URLs from the root component when the path starts with /', () => { | 
					
						
							|  |  |  |       router.config({'path': '/first/...', 'component': DummyParentComp, 'as': 'firstCmp'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(router.generate(['/firstCmp', 'secondCmp'])).toEqual('/first/second'); | 
					
						
							|  |  |  |       expect(router.generate(['/firstCmp', 'secondCmp'])).toEqual('/first/second'); | 
					
						
							|  |  |  |       expect(router.generate(['/firstCmp/secondCmp'])).toEqual('/first/second'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('matrix params', () => { | 
					
						
							|  |  |  |       it('should apply inline matrix params for each router path within the generated URL', () => { | 
					
						
							|  |  |  |         router.config({'path': '/first/...', 'component': DummyParentComp, 'as': 'firstCmp'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var path = | 
					
						
							|  |  |  |             router.generate(['/firstCmp', {'key': 'value'}, 'secondCmp', {'project': 'angular'}]); | 
					
						
							|  |  |  |         expect(path).toEqual('/first;key=value/second;project=angular'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should apply inline matrix params for each router path within the generated URL and also include named params', | 
					
						
							|  |  |  |          () => { | 
					
						
							|  |  |  |            router.config( | 
					
						
							|  |  |  |                {'path': '/first/:token/...', 'component': DummyParentComp, 'as': 'firstCmp'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var path = | 
					
						
							|  |  |  |                router.generate(['/firstCmp', {'token': 'min'}, 'secondCmp', {'author': 'max'}]); | 
					
						
							|  |  |  |            expect(path).toEqual('/first/min/second;author=max'); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @proxy | 
					
						
							|  |  |  | @IMPLEMENTS(RouterOutlet) | 
					
						
							|  |  |  | class DummyOutlet extends SpyObject { | 
					
						
							|  |  |  |   noSuchMethod(m) { return super.noSuchMethod(m) } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 13:59:14 -07:00
										 |  |  | class DummyComponent {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | @RouteConfig([{'path': '/second', 'component': DummyComponent, 'as': 'secondCmp'}]) | 
					
						
							|  |  |  | class DummyParentComp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | function makeDummyOutlet() { | 
					
						
							|  |  |  |   var ref = new DummyOutlet(); | 
					
						
							|  |  |  |   ref.spy('canActivate').andCallFake((_) => PromiseWrapper.resolve(true)); | 
					
						
							| 
									
										
										
										
											2015-07-07 15:44:29 -07:00
										 |  |  |   ref.spy('canReuse').andCallFake((_) => PromiseWrapper.resolve(false)); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   ref.spy('canDeactivate').andCallFake((_) => PromiseWrapper.resolve(true)); | 
					
						
							| 
									
										
										
										
											2015-07-07 15:44:29 -07:00
										 |  |  |   ref.spy('commit').andCallFake((_) => PromiseWrapper.resolve(true)); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   return ref; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppCmp {} |