| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | /* tslint:disable:no-unused-variable */ | 
					
						
							|  |  |  | import { DashboardComponent } from './dashboard.component'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | import { By }       from '@angular/platform-browser'; | 
					
						
							|  |  |  | import { 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 } from './mock-router'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('DashboardComponent', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ////////  WITHOUT ANGULAR INVOLVED ///////
 | 
					
						
							|  |  |  |   describe('w/o Angular', () => { | 
					
						
							|  |  |  |     let comp: DashboardComponent; | 
					
						
							|  |  |  |     let mockHeroService: MockHeroService; | 
					
						
							|  |  |  |     let router: MockRouter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       router = new MockRouter(); | 
					
						
							|  |  |  |       mockHeroService = new MockHeroService(); | 
					
						
							|  |  |  |       comp = new DashboardComponent(router, mockHeroService); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should NOT have heroes before calling OnInit', () => { | 
					
						
							|  |  |  |       expect(comp.heroes.length).toEqual(0, | 
					
						
							|  |  |  |         'should not have heroes before OnInit'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should NOT have heroes immediately after OnInit', () => { | 
					
						
							|  |  |  |       comp.ngOnInit(); // ngOnInit -> getHeroes
 | 
					
						
							|  |  |  |       expect(comp.heroes.length).toEqual(0, | 
					
						
							|  |  |  |         'should not have heroes until service promise resolves'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     it('should HAVE heroes after HeroService gets them', (done: DoneFn) => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |       comp.ngOnInit(); // ngOnInit -> getHeroes
 | 
					
						
							|  |  |  |       mockHeroService.lastPromise // the one from getHeroes
 | 
					
						
							|  |  |  |         .then(() => { | 
					
						
							|  |  |  |           // throw new Error('deliberate error'); // see it fail gracefully
 | 
					
						
							|  |  |  |           expect(comp.heroes.length).toBeGreaterThan(0, | 
					
						
							|  |  |  |             'should have heroes after service promise resolves'); | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .then(done, done.fail); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should tell ROUTER to navigate by hero id', () => { | 
					
						
							|  |  |  |       let hero: Hero = {id: 42, name: 'Abbracadabra' }; | 
					
						
							|  |  |  |       let spy = spyOn(router, 'navigate').and.callThrough(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       comp.gotoDetail(hero); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let linkParams = spy.calls.mostRecent().args[0]; | 
					
						
							|  |  |  |       expect(linkParams[0]).toEqual('HeroDetail', 'should nav to "HeroDetail"'); | 
					
						
							|  |  |  |       expect(linkParams[1].id).toEqual(hero.id, 'should nav to fake hero\'s id'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //////  WITH ANGULAR TEST INFRASTRUCTURE ///////
 | 
					
						
							|  |  |  |   describe('using TCB', () => { | 
					
						
							|  |  |  |     let comp: DashboardComponent; | 
					
						
							|  |  |  |     let mockHeroService: MockHeroService; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEachProviders(() => { | 
					
						
							|  |  |  |       mockHeroService = new MockHeroService(); | 
					
						
							|  |  |  |       return [ | 
					
						
							|  |  |  |         provide(Router,      {useClass: MockRouter}), | 
					
						
							|  |  |  |         provide(MockRouter,  {useExisting: Router}), | 
					
						
							|  |  |  |         provide(HeroService, {useValue: mockHeroService}) | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('can instantiate it', | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  |       tcb.createAsync(DashboardComponent); | 
					
						
							|  |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should NOT have heroes before OnInit', | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  |       tcb.createAsync(DashboardComponent).then(fixture => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |         comp = fixture.debugElement.componentInstance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(comp.heroes.length).toEqual(0, | 
					
						
							|  |  |  |           'should not have heroes before OnInit'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should NOT have heroes immediately after OnInit', | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							|  |  |  |       tcb.createAsync(DashboardComponent).then(fixture => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |         comp = fixture.debugElement.componentInstance; | 
					
						
							|  |  |  |         fixture.detectChanges(); // runs initial lifecycle hooks
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(comp.heroes.length).toEqual(0, | 
					
						
							|  |  |  |           'should not have heroes until service promise resolves'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should HAVE heroes after HeroService gets them', | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       tcb.createAsync(DashboardComponent).then(fixture => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |         comp = fixture.debugElement.componentInstance; | 
					
						
							|  |  |  |         fixture.detectChanges();           // runs ngOnInit -> getHeroes
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |         mockHeroService.lastPromise // the one from getHeroes
 | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |           .then(() => { | 
					
						
							|  |  |  |             expect(comp.heroes.length).toBeGreaterThan(0, | 
					
						
							|  |  |  |               'should have heroes after service promise resolves'); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should DISPLAY heroes after HeroService gets them', | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       tcb.createAsync(DashboardComponent).then(fixture => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |         comp = fixture.debugElement.componentInstance; | 
					
						
							|  |  |  |         fixture.detectChanges();           // runs ngOnInit -> getHeroes
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |         mockHeroService.lastPromise // the one from getHeroes
 | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |           .then(() => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Find and examine the displayed heroes
 | 
					
						
							|  |  |  |             fixture.detectChanges();      // update bindings
 | 
					
						
							|  |  |  |             let heroNames = fixture.debugElement.queryAll(By.css('h4')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             expect(heroNames.length).toEqual(4, 'should display 4 heroes'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // the 4th displayed hero should be the 5th mock hero
 | 
					
						
							|  |  |  |             expect(heroNames[3].nativeElement) | 
					
						
							|  |  |  |               .toHaveText(mockHeroService.mockHeroes[4].name); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should tell ROUTER to navigate by hero id', | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       async(inject([TestComponentBuilder, Router], | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |       (tcb: TestComponentBuilder, router: MockRouter) => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let spy = spyOn(router, 'navigate').and.callThrough(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |       tcb.createAsync(DashboardComponent).then(fixture => { | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |         let hero: Hero = {id: 42, name: 'Abbracadabra' }; | 
					
						
							|  |  |  |         comp = fixture.debugElement.componentInstance; | 
					
						
							|  |  |  |         comp.gotoDetail(hero); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let linkParams = spy.calls.mostRecent().args[0]; | 
					
						
							|  |  |  |         expect(linkParams[0]).toEqual('HeroDetail', 'should nav to "HeroDetail"'); | 
					
						
							|  |  |  |         expect(linkParams[1].id).toEqual(hero.id, 'should nav to fake hero\'s id'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-04-26 16:15:40 -07:00
										 |  |  |     }))); | 
					
						
							| 
									
										
										
										
											2016-04-14 10:36:38 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |