| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | import { async, ComponentFixture, TestBed | 
					
						
							|  |  |  | } from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { By }           from '@angular/platform-browser'; | 
					
						
							|  |  |  | import { DebugElement } from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 20:01:44 -07:00
										 |  |  | import { addMatchers, click } from '../../testing'; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { Hero } from '../model/hero'; | 
					
						
							|  |  |  | import { DashboardHeroComponent } from './dashboard-hero.component'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | beforeEach( addMatchers ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('DashboardHeroComponent when tested directly', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let comp: DashboardHeroComponent; | 
					
						
							|  |  |  |   let expectedHero: Hero; | 
					
						
							|  |  |  |   let fixture: ComponentFixture<DashboardHeroComponent>; | 
					
						
							|  |  |  |   let heroEl: DebugElement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion setup, compile-components
 | 
					
						
							| 
									
										
										
										
											2016-10-04 07:02:07 -06:00
										 |  |  |   // async beforeEach
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   beforeEach( async(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ DashboardHeroComponent ], | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .compileComponents(); // compile template and css
 | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  |   // #enddocregion compile-components
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // synchronous beforeEach
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     fixture = TestBed.createComponent(DashboardHeroComponent); | 
					
						
							|  |  |  |     comp    = fixture.componentInstance; | 
					
						
							|  |  |  |     heroEl  = fixture.debugElement.query(By.css('.hero')); // find hero element
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // pretend that it was wired to something that supplied a hero
 | 
					
						
							|  |  |  |     expectedHero = new Hero(42, 'Test Name'); | 
					
						
							|  |  |  |     comp.hero = expectedHero; | 
					
						
							|  |  |  |     fixture.detectChanges(); // trigger initial data binding
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion name-test
 | 
					
						
							|  |  |  |   it('should display hero name', () => { | 
					
						
							|  |  |  |     const expectedPipedName = expectedHero.name.toUpperCase(); | 
					
						
							|  |  |  |     expect(heroEl.nativeElement.textContent).toContain(expectedPipedName); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion name-test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion click-test
 | 
					
						
							|  |  |  |   it('should raise selected event when clicked', () => { | 
					
						
							|  |  |  |     let selectedHero: Hero; | 
					
						
							|  |  |  |     comp.selected.subscribe((hero: Hero) => selectedHero = hero); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-21 20:01:44 -07:00
										 |  |  |   // #docregion trigger-event-handler
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     heroEl.triggerEventHandler('click', null); | 
					
						
							| 
									
										
										
										
											2016-09-21 20:01:44 -07:00
										 |  |  |   // #enddocregion trigger-event-handler
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     expect(selectedHero).toBe(expectedHero); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion click-test
 | 
					
						
							| 
									
										
										
										
											2016-09-21 20:01:44 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // #docregion click-test-2
 | 
					
						
							|  |  |  |   it('should raise selected event when clicked', () => { | 
					
						
							|  |  |  |     let selectedHero: Hero; | 
					
						
							|  |  |  |     comp.selected.subscribe((hero: Hero) => selectedHero = hero); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     click(heroEl);   // triggerEventHandler helper
 | 
					
						
							|  |  |  |     expect(selectedHero).toBe(expectedHero); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion click-test-2
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('DashboardHeroComponent when inside a test host', () => { | 
					
						
							|  |  |  |   let testHost: TestHostComponent; | 
					
						
							|  |  |  |   let fixture: ComponentFixture<TestHostComponent>; | 
					
						
							|  |  |  |   let heroEl: DebugElement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion test-host-setup
 | 
					
						
							|  |  |  |   beforeEach( async(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ DashboardHeroComponent, TestHostComponent ], // declare both
 | 
					
						
							|  |  |  |     }).compileComponents(); | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-10-04 14:59:10 +02:00
										 |  |  |     // create TestHostComponent instead of DashboardHeroComponent
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     fixture  = TestBed.createComponent(TestHostComponent); | 
					
						
							|  |  |  |     testHost = fixture.componentInstance; | 
					
						
							|  |  |  |     heroEl   = fixture.debugElement.query(By.css('.hero')); // find hero
 | 
					
						
							|  |  |  |     fixture.detectChanges(); // trigger initial data binding
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion test-host-setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion test-host-tests
 | 
					
						
							|  |  |  |   it('should display hero name', () => { | 
					
						
							|  |  |  |     const expectedPipedName = testHost.hero.name.toUpperCase(); | 
					
						
							|  |  |  |     expect(heroEl.nativeElement.textContent).toContain(expectedPipedName); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should raise selected event when clicked', () => { | 
					
						
							| 
									
										
										
										
											2016-09-21 20:01:44 -07:00
										 |  |  |     click(heroEl); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     // selected hero should be the same data bound hero
 | 
					
						
							|  |  |  |     expect(testHost.selectedHero).toBe(testHost.hero); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion test-host-tests
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////// Test Host Component //////
 | 
					
						
							|  |  |  | import { Component } from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion test-host
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   template: `
 | 
					
						
							| 
									
										
										
										
											2016-09-21 20:01:44 -07:00
										 |  |  |     <dashboard-hero  [hero]="hero"  (selected)="onSelected($event)"></dashboard-hero>` | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | }) | 
					
						
							|  |  |  | class TestHostComponent { | 
					
						
							|  |  |  |   hero = new Hero(42, 'Test Name'); | 
					
						
							|  |  |  |   selectedHero: Hero; | 
					
						
							|  |  |  |   onSelected(hero: Hero) { this.selectedHero = hero; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | // #enddocregion test-host
 |