| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Location} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {SpyLocation} from '@angular/common/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {provide} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | import {RouterOutlet} from '../src/directives/router_outlet'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {ObservableWrapper, PromiseWrapper} from '../src/facade/async'; | 
					
						
							|  |  |  | import {ListWrapper} from '../src/facade/collection'; | 
					
						
							|  |  |  | import {Type} from '../src/facade/lang'; | 
					
						
							|  |  |  | import {AsyncRoute, Redirect, Route, RouteConfig} from '../src/route_config/route_config_decorator'; | 
					
						
							|  |  |  | import {ROUTER_PRIMARY_COMPONENT, RouteRegistry} from '../src/route_registry'; | 
					
						
							|  |  |  | import {RootRouter, Router} from '../src/router'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {SpyRouterOutlet} from './spies'; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('Router', () => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |     var router: Router; | 
					
						
							|  |  |  |     var location: Location; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     beforeEachProviders( | 
					
						
							|  |  |  |         () => | 
					
						
							|  |  |  |             [RouteRegistry, {provide: Location, useClass: SpyLocation}, | 
					
						
							|  |  |  |              {provide: ROUTER_PRIMARY_COMPONENT, useValue: AppCmp}, | 
					
						
							|  |  |  |              {provide: Router, useClass: RootRouter}]); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |     beforeEach(inject([Router, Location], (rtr: Router, loc: Location) => { | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |       router = rtr; | 
					
						
							|  |  |  |       location = loc; | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should navigate based on the initial URL state', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  |          router.config([new Route({path: '/', component: DummyComponent})]) | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |              .then((_) => router.registerPrimaryOutlet(outlet)) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                expect((<any>outlet).spy('activate')).toHaveBeenCalled(); | 
					
						
							|  |  |  |                expect((<SpyLocation>location).urlChanges).toEqual([]); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should activate viewports and update URL on navigate', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  |              .then((_) => router.config([new Route({path: '/a', component: DummyComponent})])) | 
					
						
							| 
									
										
										
										
											2015-09-08 21:41:56 -07:00
										 |  |  |              .then((_) => router.navigateByUrl('/a')) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                expect((<any>outlet).spy('activate')).toHaveBeenCalled(); | 
					
						
							|  |  |  |                expect((<SpyLocation>location).urlChanges).toEqual(['/a']); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 21:42:23 -07:00
										 |  |  |     it('should activate viewports and update URL when navigating via DSL', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-09-08 21:42:23 -07:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              .then( | 
					
						
							|  |  |  |                  (_) => | 
					
						
							|  |  |  |                      router.config([new Route({path: '/a', component: DummyComponent, name: 'A'})])) | 
					
						
							| 
									
										
										
										
											2015-09-08 21:42:23 -07:00
										 |  |  |              .then((_) => router.navigate(['/A'])) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                expect((<any>outlet).spy('activate')).toHaveBeenCalled(); | 
					
						
							|  |  |  |                expect((<SpyLocation>location).urlChanges).toEqual(['/a']); | 
					
						
							| 
									
										
										
										
											2015-09-08 21:42:23 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |     it('should not push a history change on when navigate is called with skipUrlChange', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |              .then((_) => router.config([new Route({path: '/b', component: DummyComponent})])) | 
					
						
							| 
									
										
										
										
											2015-09-08 21:41:56 -07:00
										 |  |  |              .then((_) => router.navigateByUrl('/b', true)) | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                expect((<any>outlet).spy('activate')).toHaveBeenCalled(); | 
					
						
							|  |  |  |                expect((<SpyLocation>location).urlChanges).toEqual([]); | 
					
						
							| 
									
										
										
										
											2015-07-28 01:46:09 -04:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 16:05:57 -08:00
										 |  |  |     // See https://github.com/angular/angular/issues/5590
 | 
					
						
							| 
									
										
										
										
											2016-02-25 10:21:53 -08:00
										 |  |  |     // This test is disabled because it is flaky.
 | 
					
						
							|  |  |  |     // TODO: bford. make this test not flaky and reenable it.
 | 
					
						
							|  |  |  |     xit('should replace history when triggered by a hashchange with a redirect', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |         inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-02-25 10:42:43 -08:00
										 |  |  |           var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           router.registerPrimaryOutlet(outlet) | 
					
						
							|  |  |  |               .then((_) => router.config([ | 
					
						
							|  |  |  |                 new Redirect({path: '/a', redirectTo: ['B']}), | 
					
						
							|  |  |  |                 new Route({path: '/b', component: DummyComponent, name: 'B'}) | 
					
						
							|  |  |  |               ])) | 
					
						
							|  |  |  |               .then((_) => { | 
					
						
							|  |  |  |                 router.subscribe((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                   expect((<SpyLocation>location).urlChanges).toEqual(['hash: a', 'replace: /b']); | 
					
						
							| 
									
										
										
										
											2016-02-25 10:42:43 -08:00
										 |  |  |                   async.done(); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                 (<SpyLocation>location).simulateHashChange('a'); | 
					
						
							| 
									
										
										
										
											2016-02-25 10:42:43 -08:00
										 |  |  |               }); | 
					
						
							|  |  |  |         })); | 
					
						
							| 
									
										
										
										
											2015-12-07 16:05:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should push history when triggered by a hashchange without a redirect', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-12-07 16:05:57 -08:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							|  |  |  |              .then((_) => router.config([new Route({path: '/a', component: DummyComponent})])) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							|  |  |  |                router.subscribe((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                  expect((<SpyLocation>location).urlChanges).toEqual(['hash: a']); | 
					
						
							| 
									
										
										
										
											2015-12-07 16:05:57 -08:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                (<SpyLocation>location).simulateHashChange('a'); | 
					
						
							| 
									
										
										
										
											2015-12-07 16:05:57 -08:00
										 |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-02 15:04:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 15:02:16 +00:00
										 |  |  |     it('should pass an object containing the component instruction to the router change subscription after a successful navigation', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-03-02 15:04:08 +00:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							|  |  |  |              .then((_) => router.config([new Route({path: '/a', component: DummyComponent})])) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-03-21 15:02:16 +00:00
										 |  |  |                router.subscribe(({status, instruction}) => { | 
					
						
							|  |  |  |                  expect(status).toEqual('success'); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                  expect(instruction) | 
					
						
							|  |  |  |                      .toEqual(jasmine.objectContaining({urlPath: 'a', urlParams: []})); | 
					
						
							| 
									
										
										
										
											2016-03-21 15:02:16 +00:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |                (<SpyLocation>location).simulateHashChange('a'); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should pass an object containing the bad url to the router change subscription after a failed navigation', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-03-21 15:02:16 +00:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							|  |  |  |              .then((_) => router.config([new Route({path: '/a', component: DummyComponent})])) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							|  |  |  |                router.subscribe(({status, url}) => { | 
					
						
							|  |  |  |                  expect(status).toEqual('fail'); | 
					
						
							| 
									
										
										
										
											2016-03-02 15:04:08 +00:00
										 |  |  |                  expect(url).toEqual('b'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |                (<SpyLocation>location).simulateHashChange('b'); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should navigate after being configured', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							| 
									
										
										
										
											2015-09-08 21:41:56 -07:00
										 |  |  |              .then((_) => router.navigateByUrl('/a')) | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                expect((<any>outlet).spy('activate')).not.toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  |                return router.config([new Route({path: '/a', component: DummyComponent})]); | 
					
						
							| 
									
										
										
										
											2015-06-03 13:42:57 -07:00
										 |  |  |              }) | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |              .then((_) => { | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |                expect((<any>outlet).spy('activate')).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 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.`); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 11:30:32 -07:00
										 |  |  |     it('should, when subscribed to, return a disposable subscription', () => { | 
					
						
							|  |  |  |       expect(() => { | 
					
						
							|  |  |  |         var subscription = router.subscribe((_) => {}); | 
					
						
							|  |  |  |         ObservableWrapper.dispose(subscription); | 
					
						
							|  |  |  |       }).not.toThrow(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should generate URLs from the root component when the path starts with /', () => { | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  |       router.config( | 
					
						
							|  |  |  |           [new Route({path: '/first/...', component: DummyParentComp, name: 'FirstCmp'})]); | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 17:36:32 -07:00
										 |  |  |       var instruction = router.generate(['/FirstCmp', 'SecondCmp']); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |       expect(stringifyInstruction(instruction)).toEqual('first/second'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 17:36:32 -07:00
										 |  |  |       instruction = router.generate(['/FirstCmp/SecondCmp']); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |       expect(stringifyInstruction(instruction)).toEqual('first/second'); | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 12:31:56 -07:00
										 |  |  |     it('should generate an instruction with terminal async routes', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-08-14 12:31:56 -07:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |          router.registerPrimaryOutlet(outlet); | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  |          router.config([new AsyncRoute({path: '/first', loader: loader, name: 'FirstCmp'})]); | 
					
						
							| 
									
										
										
										
											2015-08-14 12:31:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |          var instruction = router.generate(['/FirstCmp']); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          router.navigateByInstruction(instruction).then((_) => { | 
					
						
							|  |  |  |            expect((<any>outlet).spy('activate')).toHaveBeenCalled(); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return whether a given instruction is active with isRouteActive', | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							|  |  |  |              .then((_) => router.config([ | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  |                new Route({path: '/a', component: DummyComponent, name: 'A'}), | 
					
						
							|  |  |  |                new Route({path: '/b', component: DummyComponent, name: 'B'}) | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |              ])) | 
					
						
							| 
									
										
										
										
											2015-09-08 21:41:56 -07:00
										 |  |  |              .then((_) => router.navigateByUrl('/a')) | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |              .then((_) => { | 
					
						
							|  |  |  |                var instruction = router.generate(['/A']); | 
					
						
							|  |  |  |                var otherInstruction = router.generate(['/B']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(router.isRouteActive(instruction)).toEqual(true); | 
					
						
							|  |  |  |                expect(router.isRouteActive(otherInstruction)).toEqual(false); | 
					
						
							| 
									
										
										
										
											2015-08-14 12:31:56 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should provide the current instruction', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-02-18 09:25:03 -06:00
										 |  |  |          var outlet = makeDummyOutlet(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          router.registerPrimaryOutlet(outlet) | 
					
						
							|  |  |  |              .then((_) => router.config([ | 
					
						
							|  |  |  |                new Route({path: '/a', component: DummyComponent, name: 'A'}), | 
					
						
							|  |  |  |                new Route({path: '/b', component: DummyComponent, name: 'B'}) | 
					
						
							|  |  |  |              ])) | 
					
						
							|  |  |  |              .then((_) => router.navigateByUrl('/a')) | 
					
						
							|  |  |  |              .then((_) => { | 
					
						
							|  |  |  |                var instruction = router.generate(['/A']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                expect(router.currentInstruction).toEqual(instruction); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should provide the root level router from child routers', () => { | 
					
						
							|  |  |  |       let childRouter = router.childRouter(DummyComponent); | 
					
						
							|  |  |  |       expect(childRouter.root).toBe(router); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |     describe('query string params', () => { | 
					
						
							|  |  |  |       it('should use query string params for the root route', () => { | 
					
						
							|  |  |  |         router.config( | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  |             [new Route({path: '/hi/how/are/you', component: DummyComponent, name: 'GreetingUrl'})]); | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 17:36:32 -07:00
										 |  |  |         var instruction = router.generate(['/GreetingUrl', {'name': 'brad'}]); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |         var path = stringifyInstruction(instruction); | 
					
						
							|  |  |  |         expect(path).toEqual('hi/how/are/you?name=brad'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-05 22:34:10 -07:00
										 |  |  |       it('should preserve the number 1 as a query string value', () => { | 
					
						
							|  |  |  |         router.config( | 
					
						
							|  |  |  |             [new Route({path: '/hi/how/are/you', component: DummyComponent, name: 'GreetingUrl'})]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var instruction = router.generate(['/GreetingUrl', {'name': 1}]); | 
					
						
							|  |  |  |         var path = stringifyInstruction(instruction); | 
					
						
							|  |  |  |         expect(path).toEqual('hi/how/are/you?name=1'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |       it('should serialize parameters that are not part of the route definition as query string params', | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            router.config([new Route( | 
					
						
							|  |  |  |                {path: '/one/two/:three', component: DummyComponent, name: 'NumberUrl'})]); | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-14 17:36:32 -07:00
										 |  |  |            var instruction = router.generate(['/NumberUrl', {'three': 'three', 'four': 'four'}]); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |            var path = stringifyInstruction(instruction); | 
					
						
							|  |  |  |            expect(path).toEqual('one/two/three?four=four'); | 
					
						
							| 
									
										
										
										
											2015-07-21 01:26:43 -07:00
										 |  |  |          }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  |     describe('matrix params', () => { | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |       it('should generate matrix params for each non-root component', () => { | 
					
						
							| 
									
										
										
										
											2015-07-13 16:12:48 -07:00
										 |  |  |         router.config( | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  |             [new Route({path: '/first/...', component: DummyParentComp, name: 'FirstCmp'})]); | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |         var instruction = | 
					
						
							| 
									
										
										
										
											2015-09-14 17:36:32 -07:00
										 |  |  |             router.generate(['/FirstCmp', {'key': 'value'}, 'SecondCmp', {'project': 'angular'}]); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |         var path = stringifyInstruction(instruction); | 
					
						
							|  |  |  |         expect(path).toEqual('first/second;project=angular?key=value'); | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |       it('should work with named params', () => { | 
					
						
							|  |  |  |         router.config( | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  |             [new Route({path: '/first/:token/...', component: DummyParentComp, name: 'FirstCmp'})]); | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |         var instruction = | 
					
						
							| 
									
										
										
										
											2015-09-14 17:36:32 -07:00
										 |  |  |             router.generate(['/FirstCmp', {'token': 'min'}, 'SecondCmp', {'author': 'max'}]); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |         var path = stringifyInstruction(instruction); | 
					
						
							|  |  |  |         expect(path).toEqual('first/min/second;author=max'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-07-08 10:57:38 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
	{ path: '/foo', redirectTo: '/bar' },
	{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
	{ path: '/foo', redirectTo: ['Bar'] },
	{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
	{ path: '/tab', redirectTo: '/tab/users' }
	{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
	{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
	{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
	{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
											
										 
											2015-11-23 18:07:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  | function stringifyInstruction(instruction: any /** TODO #9100 */): string { | 
					
						
							| 
									
										
											  
											
												refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
	{ path: '/foo', redirectTo: '/bar' },
	{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
	{ path: '/foo', redirectTo: ['Bar'] },
	{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
	{ path: '/tab', redirectTo: '/tab/users' }
	{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
	{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
	{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
	{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
											
										 
											2015-11-23 18:07:37 -08:00
										 |  |  |   return instruction.toRootUrl(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-14 12:31:56 -07:00
										 |  |  | function loader(): Promise<Type> { | 
					
						
							|  |  |  |   return PromiseWrapper.resolve(DummyComponent); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 13:59:14 -07:00
										 |  |  | class DummyComponent {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-25 15:00:27 +05:30
										 |  |  | @RouteConfig([new Route({path: '/second', component: DummyComponent, name: 'SecondCmp'})]) | 
					
						
							| 
									
										
										
										
											2015-07-06 17:41:15 -07:00
										 |  |  | class DummyParentComp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  | function makeDummyOutlet(): RouterOutlet { | 
					
						
							| 
									
										
										
										
											2015-08-26 11:41:41 -07:00
										 |  |  |   var ref = new SpyRouterOutlet(); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   ref.spy('canActivate').andCallFake((_: any /** TODO #9100 */) => PromiseWrapper.resolve(true)); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   ref.spy('routerCanReuse') | 
					
						
							|  |  |  |       .andCallFake((_: any /** TODO #9100 */) => PromiseWrapper.resolve(false)); | 
					
						
							|  |  |  |   ref.spy('routerCanDeactivate') | 
					
						
							|  |  |  |       .andCallFake((_: any /** TODO #9100 */) => PromiseWrapper.resolve(true)); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   ref.spy('activate').andCallFake((_: any /** TODO #9100 */) => PromiseWrapper.resolve(true)); | 
					
						
							| 
									
										
										
										
											2016-02-19 11:49:31 -08:00
										 |  |  |   return <any>ref; | 
					
						
							| 
									
										
										
										
											2015-05-29 14:58:41 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AppCmp {} |