| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | /* tslint:disable:no-unused-variable */ | 
					
						
							|  |  |  | import { AppComponent } from './app.component'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | import { By }                     from '@angular/platform-browser'; | 
					
						
							|  |  |  | import { DebugElement, provide  } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   beforeEach, beforeEachProviders, | 
					
						
							|  |  |  |   describe, ddescribe, xdescribe, | 
					
						
							|  |  |  |   expect, it, iit, xit, | 
					
						
							| 
									
										
										
										
											2016-05-20 01:09:45 -07:00
										 |  |  |   async, inject | 
					
						
							|  |  |  | } from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { Hero, HeroService, MockHeroService } from './mock-hero.service'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { Router,       MockRouter, | 
					
						
							|  |  |  |          RouterLink,   MockRouterLink, | 
					
						
							|  |  |  |          RouterOutlet, MockRouterOutlet} from './mock-router'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('AppComponent', () => { | 
					
						
							| 
									
										
										
										
											2016-05-20 01:09:45 -07:00
										 |  |  |   let fixture: ComponentFixture<AppComponent>; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |   let comp:    AppComponent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |   beforeEach(async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  |     tcb | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |       .overrideDirective(AppComponent, RouterLink,   MockRouterLink) | 
					
						
							|  |  |  |       .overrideDirective(AppComponent, RouterOutlet, MockRouterOutlet) | 
					
						
							|  |  |  |       .overrideProviders(AppComponent, [ | 
					
						
							|  |  |  |         provide(HeroService, {useClass: MockHeroService}), | 
					
						
							|  |  |  |         provide(Router,      {useClass: MockRouter}), | 
					
						
							|  |  |  |       ]) | 
					
						
							|  |  |  |       .createAsync(AppComponent) | 
					
						
							|  |  |  |       .then(fix => { | 
					
						
							|  |  |  |         fixture = fix; | 
					
						
							|  |  |  |         comp = fixture.debugElement.componentInstance; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |   }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('can instantiate it', () => { | 
					
						
							|  |  |  |     expect(comp).not.toBeNull(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('can get title from template', () => { | 
					
						
							|  |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  |     let titleEl = fixture.debugElement.query(By.css('h1')).nativeElement; | 
					
						
							|  |  |  |     expect(titleEl).toHaveText(comp.title); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('can get RouterLinks from template', () => { | 
					
						
							|  |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let links = fixture.debugElement | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       .queryAll(By.directive(MockRouterLink)) | 
					
						
							| 
									
										
										
										
											2016-04-26 22:03:07 -07:00
										 |  |  |       .map(de => <MockRouterLink> de.injector.get(MockRouterLink) ); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(links.length).toEqual(2, 'should have 2 links'); | 
					
						
							|  |  |  |     expect(links[0].routeParams[0]).toEqual('Dashboard', '1st link should go to Dashboard'); | 
					
						
							|  |  |  |     expect(links[1].routeParams[0]).toEqual('Heroes', '1st link should go to Heroes'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let result = links[1].onClick(); | 
					
						
							|  |  |  |     expect(result).toEqual(false, 'click should prevent default browser behavior'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('can click Heroes link in template', () => { | 
					
						
							|  |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Heroes RouterLink DebugElement
 | 
					
						
							|  |  |  |     let heroesDe = fixture.debugElement | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       .queryAll(By.directive(MockRouterLink))[1]; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     expect(heroesDe).toBeDefined('should have a 2nd RouterLink'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 22:03:07 -07:00
										 |  |  |     let link = <MockRouterLink> heroesDe.injector.get(MockRouterLink); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(link.navigatedTo).toBeNull('link should not have navigate yet'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     heroesDe.triggerEventHandler('click', null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  |     expect(link.navigatedTo[0]).toEqual('Heroes'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 |