| 
									
										
										
										
											2016-07-21 17:12:00 -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-11-22 14:50:52 -08:00
										 |  |  | import {Location} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-12-01 15:46:22 -08:00
										 |  |  | import {TestBed, inject} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-09-21 11:37:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 14:33:18 -07:00
										 |  |  | import {ResolveData} from '../src/config'; | 
					
						
							| 
									
										
										
										
											2016-09-21 11:37:43 -07:00
										 |  |  | import {PreActivation, Router} from '../src/router'; | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  | import {RouterOutletMap} from '../src/router_outlet_map'; | 
					
						
							| 
									
										
										
										
											2016-10-25 14:33:18 -07:00
										 |  |  | import {ActivatedRouteSnapshot, RouterStateSnapshot, createEmptyStateSnapshot} from '../src/router_state'; | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  | import {DefaultUrlSerializer} from '../src/url_tree'; | 
					
						
							|  |  |  | import {TreeNode} from '../src/utils/tree'; | 
					
						
							| 
									
										
										
										
											2016-09-21 11:37:43 -07:00
										 |  |  | import {RouterTestingModule} from '../testing/router_testing_module'; | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('Router', () => { | 
					
						
							| 
									
										
										
										
											2016-09-21 11:37:43 -07:00
										 |  |  |   describe('resetRootComponentType', () => { | 
					
						
							|  |  |  |     class NewRootComponent {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not change root route when updating the root component', () => { | 
					
						
							|  |  |  |       const r: Router = TestBed.get(Router); | 
					
						
							|  |  |  |       const root = r.routerState.root; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       r.resetRootComponentType(NewRootComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(r.routerState.root).toBe(root); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 14:50:52 -08:00
										 |  |  |   describe('setUpLocationChangeListener', () => { | 
					
						
							|  |  |  |     beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-01 15:46:22 -08:00
										 |  |  |     it('should be indempotent', inject([Router, Location], (r: Router, location: Location) => { | 
					
						
							|  |  |  |          r.setUpLocationChangeListener(); | 
					
						
							|  |  |  |          const a = (<any>r).locationSubscription; | 
					
						
							|  |  |  |          r.setUpLocationChangeListener(); | 
					
						
							|  |  |  |          const b = (<any>r).locationSubscription; | 
					
						
							| 
									
										
										
										
											2016-11-22 14:50:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-01 15:46:22 -08:00
										 |  |  |          expect(a).toBe(b); | 
					
						
							| 
									
										
										
										
											2016-11-22 14:50:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-01 15:46:22 -08:00
										 |  |  |          r.dispose(); | 
					
						
							|  |  |  |          r.setUpLocationChangeListener(); | 
					
						
							|  |  |  |          const c = (<any>r).locationSubscription; | 
					
						
							| 
									
										
										
										
											2016-11-22 14:50:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-01 15:46:22 -08:00
										 |  |  |          expect(c).not.toBe(b); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-11-22 14:50:52 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  |   describe('PreActivation', () => { | 
					
						
							|  |  |  |     const serializer = new DefaultUrlSerializer(); | 
					
						
							|  |  |  |     const inj = {get: (token: any) => () => `${token}_value`}; | 
					
						
							|  |  |  |     let empty: RouterStateSnapshot; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { empty = createEmptyStateSnapshot(serializer.parse('/'), null); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should resolve data', () => { | 
					
						
							| 
									
										
										
										
											2016-10-25 14:33:18 -07:00
										 |  |  |       const r = {data: 'resolver'}; | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  |       const n = createActivatedRouteSnapshot('a', {resolve: r}); | 
					
						
							|  |  |  |       const s = new RouterStateSnapshot('url', new TreeNode(empty.root, [new TreeNode(n, [])])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       checkResolveData(s, empty, inj, () => { | 
					
						
							|  |  |  |         expect(s.root.firstChild.data).toEqual({data: 'resolver_value'}); | 
					
						
							| 
									
										
										
										
											2016-07-13 15:25:48 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-06-01 14:32:15 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-06-02 14:44:57 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 14:43:26 -07:00
										 |  |  |     it('should wait for the parent resolve to complete', () => { | 
					
						
							| 
									
										
										
										
											2016-10-25 14:33:18 -07:00
										 |  |  |       const parentResolve = {data: 'resolver'}; | 
					
						
							|  |  |  |       const childResolve = {}; | 
					
						
							| 
									
										
										
										
											2016-10-05 14:43:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 14:33:18 -07:00
										 |  |  |       const parent = createActivatedRouteSnapshot(null, {resolve: parentResolve}); | 
					
						
							| 
									
										
										
										
											2016-10-05 14:43:26 -07:00
										 |  |  |       const child = createActivatedRouteSnapshot('b', {resolve: childResolve}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const s = new RouterStateSnapshot( | 
					
						
							|  |  |  |           'url', new TreeNode(empty.root, [new TreeNode(parent, [new TreeNode(child, [])])])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const inj = {get: (token: any) => () => Promise.resolve(`${token}_value`)}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       checkResolveData(s, empty, inj, () => { | 
					
						
							|  |  |  |         expect(s.root.firstChild.firstChild.data).toEqual({data: 'resolver_value'}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  |     it('should copy over data when creating a snapshot', () => { | 
					
						
							| 
									
										
										
										
											2016-10-25 14:33:18 -07:00
										 |  |  |       const r1 = {data: 'resolver1'}; | 
					
						
							|  |  |  |       const r2 = {data: 'resolver2'}; | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const n1 = createActivatedRouteSnapshot('a', {resolve: r1}); | 
					
						
							|  |  |  |       const s1 = new RouterStateSnapshot('url', new TreeNode(empty.root, [new TreeNode(n1, [])])); | 
					
						
							|  |  |  |       checkResolveData(s1, empty, inj, () => {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const n21 = createActivatedRouteSnapshot('a', {resolve: r1}); | 
					
						
							|  |  |  |       const n22 = createActivatedRouteSnapshot('b', {resolve: r2}); | 
					
						
							|  |  |  |       const s2 = new RouterStateSnapshot( | 
					
						
							|  |  |  |           'url', new TreeNode(empty.root, [new TreeNode(n21, [new TreeNode(n22, [])])])); | 
					
						
							|  |  |  |       checkResolveData(s2, s1, inj, () => { | 
					
						
							|  |  |  |         expect(s2.root.firstChild.data).toEqual({data: 'resolver1_value'}); | 
					
						
							|  |  |  |         expect(s2.root.firstChild.firstChild.data).toEqual({data: 'resolver2_value'}); | 
					
						
							| 
									
										
										
										
											2016-07-26 14:39:02 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-06-06 10:55:12 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-06-01 14:32:15 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-05-20 13:22:57 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  | function checkResolveData( | 
					
						
							|  |  |  |     future: RouterStateSnapshot, curr: RouterStateSnapshot, injector: any, check: any): void { | 
					
						
							|  |  |  |   const p = new PreActivation(future, curr, injector); | 
					
						
							|  |  |  |   p.traverse(new RouterOutletMap()); | 
					
						
							|  |  |  |   p.resolveData().subscribe(check, (e) => { throw e; }); | 
					
						
							| 
									
										
										
										
											2016-06-21 11:49:42 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  | function createActivatedRouteSnapshot(cmp: string, extra: any = {}): ActivatedRouteSnapshot { | 
					
						
							|  |  |  |   return new ActivatedRouteSnapshot( | 
					
						
							| 
									
										
										
										
											2017-02-23 22:12:30 -08:00
										 |  |  |       <any>[], {}, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>{}, <any>null, -1, | 
					
						
							| 
									
										
										
										
											2016-08-10 15:53:57 -07:00
										 |  |  |       extra.resolve); | 
					
						
							|  |  |  | } |