| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | import {Component} from '@angular/core'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   beforeEachProviders, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   fakeAsync, | 
					
						
							|  |  |  |   tick | 
					
						
							|  |  |  | } from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing'; | 
					
						
							|  |  |  | import { ComponentResolver } from '@angular/core'; | 
					
						
							|  |  |  | import { SpyLocation } from '@angular/common/testing'; | 
					
						
							| 
									
										
										
										
											2016-05-24 14:47:26 -07:00
										 |  |  | import { UrlSerializer, DefaultUrlSerializer, RouterOutletMap, Router, ActivatedRoute, ROUTER_DIRECTIVES } from '../src/index'; | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | import { Observable } from 'rxjs/Observable'; | 
					
						
							|  |  |  | import 'rxjs/add/operator/map'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 13:55:17 -07:00
										 |  |  | describe("Integration", () => { | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |   beforeEachProviders(() => [ | 
					
						
							|  |  |  |     RouterOutletMap, | 
					
						
							|  |  |  |     {provide: UrlSerializer, useClass: DefaultUrlSerializer}, | 
					
						
							|  |  |  |     {provide: Location, useClass: SpyLocation}, | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       provide: Router, | 
					
						
							|  |  |  |       useFactory: (resolver, urlSerializer, outletMap, location) => | 
					
						
							| 
									
										
										
										
											2016-05-31 08:48:53 -07:00
										 |  |  |         new Router(RootCmp, resolver, urlSerializer, outletMap, location), | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |       deps: [ComponentResolver, UrlSerializer, RouterOutletMap, Location] | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |     }, | 
					
						
							|  |  |  |     {provide: ActivatedRoute, useFactory: (r) => r.routerState.root, deps: [Router]}, | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |   ]); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   it('should update location when navigating', | 
					
						
							|  |  |  |     fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => { | 
					
						
							|  |  |  |       router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |         { path: 'team/:id', component: TeamCmp } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |       ]); | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |       const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |       advance(fixture); | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/team/22'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  |       expect(location.path()).toEqual('/team/22'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/team/33'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(location.path()).toEqual('/team/33'); | 
					
						
							|  |  |  |     }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   xit('should navigate back and forward', | 
					
						
							|  |  |  |     fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => { | 
					
						
							|  |  |  |       router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |         { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |           { path: 'simple', component: SimpleCmp }, | 
					
						
							|  |  |  |           { path: 'user/:name', component: UserCmp } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |         ] } | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/team/33/simple'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  |       expect(location.path()).toEqual('/team/33/simple'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/team/22/user/victor'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       location.back(); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  |       expect(location.path()).toEqual('/team/33/simple'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       location.forward(); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  |       expect(location.path()).toEqual('/team/22/user/victor'); | 
					
						
							|  |  |  |     }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should navigate when locations changes', | 
					
						
							|  |  |  |     fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => { | 
					
						
							|  |  |  |       router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |         { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |           { path: 'user/:name', component: UserCmp } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |         ] } | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/team/22/user/victor'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       location.simulateHashChange("/team/22/user/fedor"); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(fixture.debugElement.nativeElement).toHaveText('team 22 { user fedor, right:  }'); | 
					
						
							|  |  |  |     }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should support secondary routes', | 
					
						
							|  |  |  |     fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |       router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |         { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |           { path: 'user/:name', component: UserCmp }, | 
					
						
							|  |  |  |           { path: 'simple', component: SimpleCmp, outlet: 'right' } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |         ] } | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |       router.navigateByUrl('/team/22/user/victor(right:simple)'); | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(fixture.debugElement.nativeElement) | 
					
						
							|  |  |  |         .toHaveText('team 22 { user victor, right: simple }'); | 
					
						
							|  |  |  |     }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should deactivate outlets', | 
					
						
							|  |  |  |     fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |       router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |         { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |           { path: 'user/:name', component: UserCmp }, | 
					
						
							|  |  |  |           { path: 'simple', component: SimpleCmp, outlet: 'right' } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |         ] } | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |       router.navigateByUrl('/team/22/user/victor(right:simple)'); | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/team/22/user/victor'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(fixture.debugElement.nativeElement).toHaveText('team 22 { user victor, right:  }'); | 
					
						
							|  |  |  |     }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should deactivate nested outlets', | 
					
						
							|  |  |  |     fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |       router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |         { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |           { path: 'user/:name', component: UserCmp }, | 
					
						
							|  |  |  |           { path: 'simple', component: SimpleCmp, outlet: 'right' } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |         ] } | 
					
						
							|  |  |  |       ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |       router.navigateByUrl('/team/22/user/victor(right:simple)'); | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       router.navigateByUrl('/'); | 
					
						
							|  |  |  |       advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(fixture.debugElement.nativeElement).toHaveText(''); | 
					
						
							|  |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe("router links", () => { | 
					
						
							|  |  |  |     it("should support string router links", | 
					
						
							|  |  |  |       fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |         router.resetConfig([ | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  |           { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |             { path: 'link', component: StringLinkCmp }, | 
					
						
							|  |  |  |             { path: 'simple', component: SimpleCmp } | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         router.navigateByUrl('/team/22/link'); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement).toHaveText('team 22 { link, right:  }'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const native = fixture.debugElement.nativeElement.querySelector("a"); | 
					
						
							|  |  |  |         expect(native.getAttribute("href")).toEqual("/team/33/simple"); | 
					
						
							|  |  |  |         native.click(); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement).toHaveText('team 33 { simple, right:  }'); | 
					
						
							|  |  |  |       }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should support absolute router links", | 
					
						
							|  |  |  |       fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |         router.resetConfig([ | 
					
						
							|  |  |  |           { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |             { path: 'link', component: AbsoluteLinkCmp }, | 
					
						
							|  |  |  |             { path: 'simple', component: SimpleCmp } | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  |           ] } | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         router.navigateByUrl('/team/22/link'); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement).toHaveText('team 22 { link, right:  }'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const native = fixture.debugElement.nativeElement.querySelector("a"); | 
					
						
							|  |  |  |         expect(native.getAttribute("href")).toEqual("/team/33/simple"); | 
					
						
							|  |  |  |         native.click(); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement).toHaveText('team 33 { simple, right:  }'); | 
					
						
							|  |  |  |       }))); | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it("should support relative router links", | 
					
						
							|  |  |  |       fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |         router.resetConfig([ | 
					
						
							|  |  |  |           { path: 'team/:id', component: TeamCmp, children: [ | 
					
						
							|  |  |  |             { path: 'link', component: RelativeLinkCmp }, | 
					
						
							|  |  |  |             { path: 'simple', component: SimpleCmp } | 
					
						
							|  |  |  |           ] } | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const fixture = tcb.createFakeAsync(RootCmp); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         router.navigateByUrl('/team/22/link'); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement) | 
					
						
							|  |  |  |           .toHaveText('team 22 { link, right:  }'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const native = fixture.debugElement.nativeElement.querySelector("a"); | 
					
						
							|  |  |  |         expect(native.getAttribute("href")).toEqual("/team/22/simple"); | 
					
						
							|  |  |  |         native.click(); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement) | 
					
						
							|  |  |  |           .toHaveText('team 22 { simple, right:  }'); | 
					
						
							|  |  |  |       }))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("should support top-level link", | 
					
						
							|  |  |  |       fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | 
					
						
							|  |  |  |         let fixture = tcb.createFakeAsync(AbsoluteLinkCmp); | 
					
						
							|  |  |  |         advance(fixture); | 
					
						
							|  |  |  |         expect(fixture.debugElement.nativeElement).toHaveText('link'); | 
					
						
							|  |  |  |       }))); | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-05-20 13:22:57 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'link-cmp', | 
					
						
							|  |  |  |   template: `<a routerLink="/team/33/simple">link</a>`, | 
					
						
							|  |  |  |   directives: ROUTER_DIRECTIVES | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class StringLinkCmp {} | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 16:51:56 -07:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'link-cmp', | 
					
						
							|  |  |  |   template: `<a [routerLink]="['/team/33/simple']">link</a>`, | 
					
						
							|  |  |  |   directives: ROUTER_DIRECTIVES | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class AbsoluteLinkCmp {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'link-cmp', | 
					
						
							|  |  |  |   template: `<a [routerLink]="['../simple']">link</a>`, | 
					
						
							|  |  |  |   directives: ROUTER_DIRECTIVES | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class RelativeLinkCmp {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'simple-cmp', | 
					
						
							|  |  |  |   template: `simple`, | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  |   directives: ROUTER_DIRECTIVES | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  | class SimpleCmp { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'team-cmp', | 
					
						
							|  |  |  |   template: `team {{id | async}} { <router-outlet></router-outlet>, right: <router-outlet name="right"></router-outlet> }`, | 
					
						
							| 
									
										
										
										
											2016-05-24 14:33:34 -07:00
										 |  |  |   directives: ROUTER_DIRECTIVES | 
					
						
							| 
									
										
										
										
											2016-05-24 13:23:27 -07:00
										 |  |  | }) | 
					
						
							|  |  |  | class TeamCmp { | 
					
						
							|  |  |  |   id: Observable<string>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(route: ActivatedRoute) { | 
					
						
							|  |  |  |     this.id = route.params.map(p => p['id']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'user-cmp', | 
					
						
							|  |  |  |   template: `user {{name | async}}`, | 
					
						
							|  |  |  |   directives: [ROUTER_DIRECTIVES] | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class UserCmp { | 
					
						
							|  |  |  |   name: Observable<string>; | 
					
						
							|  |  |  |   constructor(route: ActivatedRoute) { | 
					
						
							|  |  |  |     this.name = route.params.map(p => p['name']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'root-cmp', | 
					
						
							|  |  |  |   template: `<router-outlet></router-outlet>`, | 
					
						
							|  |  |  |   directives: [ROUTER_DIRECTIVES] | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class RootCmp {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function advance(fixture: ComponentFixture<any>): void { | 
					
						
							|  |  |  |   tick(); | 
					
						
							|  |  |  |   fixture.detectChanges(); | 
					
						
							|  |  |  | } |