| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | // #docplaster
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | import { async, ComponentFixture, TestBed | 
					
						
							|  |  |  | } from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | import { DebugElement } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | import { By } from '@angular/platform-browser'; | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   // #docregion setup-schemas
 | 
					
						
							|  |  |  |   import { NO_ERRORS_SCHEMA }          from '@angular/core'; | 
					
						
							|  |  |  |   // #enddocregion setup-schemas
 | 
					
						
							|  |  |  |   // #docregion setup-stubs-w-imports
 | 
					
						
							|  |  |  |   import { Component }                 from '@angular/core'; | 
					
						
							|  |  |  |   // #docregion setup-schemas
 | 
					
						
							|  |  |  |   import { AppComponent }              from './app.component'; | 
					
						
							|  |  |  |   // #enddocregion setup-schemas
 | 
					
						
							|  |  |  |   import { BannerComponent }           from './banner.component'; | 
					
						
							|  |  |  |   import { RouterLinkStubDirective }   from '../testing'; | 
					
						
							|  |  |  |   // #docregion setup-schemas
 | 
					
						
							|  |  |  |   import { RouterOutletStubComponent } from '../testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #enddocregion setup-schemas
 | 
					
						
							|  |  |  |   @Component({selector: 'app-welcome', template: ''}) | 
					
						
							|  |  |  |   class WelcomeStubComponent {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #enddocregion setup-stubs-w-imports
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | let comp:    AppComponent; | 
					
						
							|  |  |  | let fixture: ComponentFixture<AppComponent>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('AppComponent & TestModule', () => { | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   // #docregion setup-stubs, setup-stubs-w-imports
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   beforeEach( async(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |         AppComponent, | 
					
						
							|  |  |  |         BannerComponent, WelcomeStubComponent, | 
					
						
							|  |  |  |         RouterLinkStubDirective, RouterOutletStubComponent | 
					
						
							|  |  |  |       ] | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .compileComponents() | 
					
						
							|  |  |  |     .then(() => { | 
					
						
							|  |  |  |       fixture = TestBed.createComponent(AppComponent); | 
					
						
							|  |  |  |       comp    = fixture.componentInstance; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   })); | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   // #enddocregion setup-stubs, setup-stubs-w-imports
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   tests(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | //////// Testing w/ NO_ERRORS_SCHEMA //////
 | 
					
						
							|  |  |  | describe('AppComponent & NO_ERRORS_SCHEMA', () => { | 
					
						
							|  |  |  |   // #docregion setup-schemas
 | 
					
						
							|  |  |  |   beforeEach( async(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ AppComponent, RouterLinkStubDirective ], | 
					
						
							|  |  |  |       schemas:      [ NO_ERRORS_SCHEMA ] | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |     .compileComponents() | 
					
						
							|  |  |  |     .then(() => { | 
					
						
							|  |  |  |       fixture = TestBed.createComponent(AppComponent); | 
					
						
							|  |  |  |       comp    = fixture.componentInstance; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  |   // #enddocregion setup-schemas
 | 
					
						
							|  |  |  |   tests(); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | //////// Testing w/ real root module //////
 | 
					
						
							|  |  |  | // Tricky because we are disabling the router and its configuration
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | // Better to use RouterTestingModule
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | import { AppModule }    from './app.module'; | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | import { AppRoutingModule } from './app-routing.module'; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('AppComponent & AppModule', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach( async(() => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |       imports: [ AppModule ] | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |     // Get rid of app's Router configuration otherwise many failures.
 | 
					
						
							|  |  |  |     // Doing so removes Router declarations; add the Router stubs
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     .overrideModule(AppModule, { | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |       remove: { | 
					
						
							|  |  |  |         imports: [ AppRoutingModule ] | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |       add: { | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |         declarations: [ RouterLinkStubDirective, RouterOutletStubComponent ] | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .compileComponents() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     .then(() => { | 
					
						
							|  |  |  |       fixture = TestBed.createComponent(AppComponent); | 
					
						
							|  |  |  |       comp    = fixture.componentInstance; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   tests(); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | function tests() { | 
					
						
							|  |  |  |   let links: RouterLinkStubDirective[]; | 
					
						
							|  |  |  |   let linkDes: DebugElement[]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion test-setup
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     // trigger initial data binding
 | 
					
						
							|  |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // find DebugElements with an attached RouterLinkStubDirective
 | 
					
						
							|  |  |  |     linkDes = fixture.debugElement | 
					
						
							|  |  |  |       .queryAll(By.directive(RouterLinkStubDirective)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // get the attached link directive instances using the DebugElement injectors
 | 
					
						
							|  |  |  |     links = linkDes | 
					
						
							|  |  |  |       .map(de => de.injector.get(RouterLinkStubDirective) as RouterLinkStubDirective); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion test-setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('can instantiate it', () => { | 
					
						
							|  |  |  |     expect(comp).not.toBeNull(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion tests
 | 
					
						
							|  |  |  |   it('can get RouterLinks from template', () => { | 
					
						
							|  |  |  |     expect(links.length).toBe(3, 'should have 3 links'); | 
					
						
							|  |  |  |     expect(links[0].linkParams).toBe('/dashboard', '1st link should go to Dashboard'); | 
					
						
							|  |  |  |     expect(links[1].linkParams).toBe('/heroes', '1st link should go to Heroes'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('can click Heroes link in template', () => { | 
					
						
							|  |  |  |     const heroesLinkDe = linkDes[1]; | 
					
						
							|  |  |  |     const heroesLink = links[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(heroesLink.navigatedTo).toBeNull('link should not have navigated yet'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     heroesLinkDe.triggerEventHandler('click', null); | 
					
						
							|  |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(heroesLink.navigatedTo).toBe('/heroes'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #docregion tests
 | 
					
						
							|  |  |  | } |