| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {beforeEach, ddescribe, xdescribe, describe, expect, iit, inject, beforeEachProviders, it, xit,} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | 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-06-08 16:38:52 -07:00
										 |  |  | import {Router, RouteRegistry, RouterLink, RouterOutlet, Route, RouteParams, ComponentInstruction} 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-06-08 16:38:52 -07:00
										 |  |  |     beforeEachProviders(() => [{provide: Location, useValue: makeDummyLocation()}, { | 
					
						
							|  |  |  |                           provide: Router, | 
					
						
							|  |  |  |                           useValue: makeDummyRouter() | 
					
						
							|  |  |  |                         }]); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     beforeEach( | 
					
						
							|  |  |  |         inject([TestComponentBuilder], (tcBuilder: any /** TODO #9100 */) => { tcb = tcBuilder; })); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should update a[href] attribute', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          tcb.createAsync(TestComponent).then((testComponent) => { | 
					
						
							|  |  |  |            testComponent.detectChanges(); | 
					
						
							|  |  |  |            let anchorElement = | 
					
						
							|  |  |  |                testComponent.debugElement.query(By.css('a.detail-view')).nativeElement; | 
					
						
							|  |  |  |            expect(getDOM().getAttribute(anchorElement, 'href')).toEqual('detail'); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should call router.navigate when a link is clicked', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [AsyncTestCompleter, Router], | 
					
						
							|  |  |  |            (async: AsyncTestCompleter, router: any /** TODO #9100 */) => { | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              tcb.createAsync(TestComponent).then((testComponent) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                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(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should call router.navigate when a link is clicked if target is _self', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [AsyncTestCompleter, Router], | 
					
						
							|  |  |  |            (async: AsyncTestCompleter, router: any /** TODO #9100 */) => { | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              tcb.createAsync(TestComponent).then((testComponent) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                testComponent.detectChanges(); | 
					
						
							|  |  |  |                testComponent.debugElement.query(By.css('a.detail-view-self')) | 
					
						
							|  |  |  |                    .triggerEventHandler('click', null); | 
					
						
							|  |  |  |                expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        inject( | 
					
						
							|  |  |  |            [AsyncTestCompleter, Router], | 
					
						
							|  |  |  |            (async: AsyncTestCompleter, router: any /** TODO #9100 */) => { | 
					
						
							| 
									
										
										
										
											2015-11-02 15:11:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |              tcb.createAsync(TestComponent).then((testComponent) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                testComponent.detectChanges(); | 
					
						
							|  |  |  |                testComponent.debugElement.query(By.css('a.detail-view-blank')) | 
					
						
							|  |  |  |                    .triggerEventHandler('click', null); | 
					
						
							|  |  |  |                expect(router.spy('navigateByInstruction')).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            })); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -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(); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   dl.spy('prepareExternalUrl').andCallFake((url: any /** TODO #9100 */) => 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(); | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |   dr.spy('generate').andCallFake((routeParams: any /** TODO #9100 */) => dummyInstruction); | 
					
						
							|  |  |  |   dr.spy('isRouteActive').andCallFake((_: any /** TODO #9100 */) => false); | 
					
						
							| 
									
										
										
										
											2015-07-17 13:36:53 -07:00
										 |  |  |   dr.spy('navigateInstruction'); | 
					
						
							| 
									
										
										
										
											2015-07-02 17:38:19 -07:00
										 |  |  |   return dr; | 
					
						
							|  |  |  | } |