| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   xdescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   beforeEachProviders, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | } from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {TestComponentBuilder} from '@angular/compiler/testing'; | 
					
						
							|  |  |  | import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-09 11:12:41 -08:00
										 |  |  | import {SpyRouter, SpyLocation} from '../spies'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {provide, Component} from '@angular/core'; | 
					
						
							|  |  |  | import {Location} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   Router, | 
					
						
							|  |  |  |   RouteRegistry, | 
					
						
							|  |  |  |   RouterLink, | 
					
						
							|  |  |  |   RouterOutlet, | 
					
						
							|  |  |  |   Route, | 
					
						
							|  |  |  |   RouteParams, | 
					
						
							|  |  |  |   ComponentInstruction | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | } from '@angular/router-deprecated'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2016-05-02 10:36:58 -07:00
										 |  |  | import {ResolvedInstruction} from '@angular/router-deprecated/src/instruction'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {By} from '@angular/platform-browser/src/dom/debug/by'; | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-18 10:05:55 -08:00
										 |  |  | let dummyInstruction = new ResolvedInstruction( | 
					
						
							| 
									
										
										
										
											2016-03-30 11:26:31 -07:00
										 |  |  |     new ComponentInstruction('detail', [], null, null, true, '0', null, 'Detail'), null, {}); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |   describe('routerLink directive', function() { | 
					
						
							| 
									
										
										
										
											2015-08-24 11:24:53 -07:00
										 |  |  |     var tcb: TestComponentBuilder; | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |     beforeEachProviders(() => [ | 
					
						
							|  |  |  |       provide(Location, {useValue: makeDummyLocation()}), | 
					
						
							|  |  |  |       provide(Router, {useValue: makeDummyRouter()}) | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-24 11:24:53 -07:00
										 |  |  |     beforeEach(inject([TestComponentBuilder], (tcBuilder) => { tcb = tcBuilder; })); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-24 11:24:53 -07:00
										 |  |  |     it('should update a[href] attribute', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((testComponent) => { | 
					
						
							|  |  |  |                testComponent.detectChanges(); | 
					
						
							|  |  |  |                let anchorElement = | 
					
						
							|  |  |  |                    testComponent.debugElement.query(By.css('a.detail-view')).nativeElement; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |                expect(getDOM().getAttribute(anchorElement, 'href')).toEqual('detail'); | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should call router.navigate when a link is clicked', | 
					
						
							| 
									
										
										
										
											2015-08-24 11:24:53 -07:00
										 |  |  |        inject([AsyncTestCompleter, Router], (async, router) => { | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((testComponent) => { | 
					
						
							|  |  |  |                testComponent.detectChanges(); | 
					
						
							|  |  |  |                // TODO: shouldn't this be just 'click' rather than '^click'?
 | 
					
						
							|  |  |  |                testComponent.debugElement.query(By.css('a.detail-view')) | 
					
						
							|  |  |  |                    .triggerEventHandler('click', null); | 
					
						
							|  |  |  |                expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should call router.navigate when a link is clicked if target is _self', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, Router], (async, router) => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((testComponent) => { | 
					
						
							|  |  |  |                testComponent.detectChanges(); | 
					
						
							|  |  |  |                testComponent.debugElement.query(By.css('a.detail-view-self')) | 
					
						
							|  |  |  |                    .triggerEventHandler('click', null); | 
					
						
							|  |  |  |                expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should NOT call router.navigate when a link is clicked if target is set to other than _self', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter, Router], (async, router) => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |          tcb.createAsync(TestComponent) | 
					
						
							|  |  |  |              .then((testComponent) => { | 
					
						
							|  |  |  |                testComponent.detectChanges(); | 
					
						
							|  |  |  |                testComponent.debugElement.query(By.css('a.detail-view-blank')) | 
					
						
							|  |  |  |                    .triggerEventHandler('click', null); | 
					
						
							|  |  |  |                expect(router.spy('navigateByInstruction')).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | @Component({selector: 'user-cmp', template: "hello {{user}}"}) | 
					
						
							| 
									
										
										
										
											2015-08-24 11:24:53 -07:00
										 |  |  | class UserCmp { | 
					
						
							|  |  |  |   user: string; | 
					
						
							|  |  |  |   constructor(params: RouteParams) { this.user = params.get('name'); } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-08 13:36:48 -08:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'test-component', | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |   template: `
 | 
					
						
							|  |  |  |     <div> | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |       <a [routerLink]="['/Detail']" | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  |          class="detail-view"> | 
					
						
							|  |  |  |            detail view | 
					
						
							|  |  |  |       </a> | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |       <a [routerLink]="['/Detail']" | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  |          class="detail-view-self" | 
					
						
							|  |  |  |          target="_self"> | 
					
						
							|  |  |  |            detail view with _self target | 
					
						
							|  |  |  |       </a> | 
					
						
							| 
									
										
										
										
											2015-11-23 16:02:19 -08:00
										 |  |  |       <a [routerLink]="['/Detail']" | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  |          class="detail-view-blank" | 
					
						
							|  |  |  |          target="_blank"> | 
					
						
							|  |  |  |            detail view with _blank target | 
					
						
							|  |  |  |       </a> | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |     </div>`,
 | 
					
						
							|  |  |  |   directives: [RouterLink] | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class TestComponent { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function makeDummyLocation() { | 
					
						
							| 
									
										
										
										
											2015-08-26 11:41:41 -07:00
										 |  |  |   var dl = new SpyLocation(); | 
					
						
							| 
									
										
										
										
											2015-10-26 13:57:41 +00:00
										 |  |  |   dl.spy('prepareExternalUrl').andCallFake((url) => url); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |   return dl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function makeDummyRouter() { | 
					
						
							| 
									
										
										
										
											2015-08-26 11:41:41 -07:00
										 |  |  |   var dr = new SpyRouter(); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |   dr.spy('generate').andCallFake((routeParams) => dummyInstruction); | 
					
						
							| 
									
										
										
										
											2015-08-30 21:25:46 -07:00
										 |  |  |   dr.spy('isRouteActive').andCallFake((_) => false); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |   dr.spy('navigateInstruction'); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |   return dr; | 
					
						
							|  |  |  | } |