| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							|  |  |  | // #docregion imports
 | 
					
						
							|  |  |  | import { ComponentFixture, TestBed } from '@angular/core/testing'; | 
					
						
							|  |  |  | import { By }              from '@angular/platform-browser'; | 
					
						
							|  |  |  | import { DebugElement }    from '@angular/core'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { BannerComponent } from './banner.component'; | 
					
						
							|  |  |  | // #enddocregion imports
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion setup
 | 
					
						
							|  |  |  | let comp:    BannerComponent; | 
					
						
							|  |  |  | let fixture: ComponentFixture<BannerComponent>; | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  | let de:      DebugElement; | 
					
						
							|  |  |  | let el:      HTMLElement; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('BannerComponent', () => { | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ BannerComponent ], // declare the test component
 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fixture = TestBed.createComponent(BannerComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     comp = fixture.componentInstance; // BannerComponent test instance
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     // query for the title <h1> by CSS element selector
 | 
					
						
							|  |  |  |     de = fixture.debugElement.query(By.css('h1')); | 
					
						
							|  |  |  |     el = de.nativeElement; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | // #enddocregion setup
 | 
					
						
							|  |  |  |   // #docregion tests
 | 
					
						
							|  |  |  |   it('should display original title', () => { | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  |     expect(el.textContent).toContain(comp.title); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should display a different test title', () => { | 
					
						
							|  |  |  |     comp.title = 'Test Title'; | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  |     expect(el.textContent).toContain('Test Title'); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion tests
 | 
					
						
							|  |  |  |   // #docregion test-w-o-detect-changes
 | 
					
						
							|  |  |  |   it('no title in the DOM until manually call `detectChanges`', () => { | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     expect(el.textContent).toEqual(''); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion test-w-o-detect-changes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion setup
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | // #enddocregion setup
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /////////  With AutoChangeDetect /////
 | 
					
						
							|  |  |  | import { ComponentFixtureAutoDetect } from '@angular/core/testing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('BannerComponent with AutoChangeDetect', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     // #docregion auto-detect
 | 
					
						
							|  |  |  |     fixture = TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ BannerComponent ], | 
					
						
							|  |  |  |       providers: [ | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |         { provide: ComponentFixtureAutoDetect, useValue: true } | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |       ] | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     // #enddocregion auto-detect
 | 
					
						
							|  |  |  |     .createComponent(BannerComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     comp = fixture.componentInstance; // BannerComponent test instance
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     // query for the title <h1> by CSS element selector
 | 
					
						
							|  |  |  |     de = fixture.debugElement.query(By.css('h1')); | 
					
						
							|  |  |  |     el = de.nativeElement; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion auto-detect-tests
 | 
					
						
							|  |  |  |   it('should display original title', () => { | 
					
						
							|  |  |  |     // Hooray! No `fixture.detectChanges()` needed
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     expect(el.textContent).toContain(comp.title); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should still see original title after comp.title change', () => { | 
					
						
							|  |  |  |     const oldTitle = comp.title; | 
					
						
							|  |  |  |     comp.title = 'Test Title'; | 
					
						
							|  |  |  |     // Displayed title is old because Angular didn't hear the change :(
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     expect(el.textContent).toContain(oldTitle); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should display updated title after detectChanges', () => { | 
					
						
							|  |  |  |     comp.title = 'Test Title'; | 
					
						
							|  |  |  |     fixture.detectChanges(); // detect changes explicitly
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     expect(el.textContent).toContain(comp.title); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion auto-detect-tests
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('BannerComponent (simpified)', () => { | 
					
						
							|  |  |  |   // #docregion simple-example-before-each
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // refine the test module by declaring the test component
 | 
					
						
							|  |  |  |     TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ BannerComponent ], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // create component and test fixture
 | 
					
						
							|  |  |  |     fixture = TestBed.createComponent(BannerComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // get test component from the fixture
 | 
					
						
							|  |  |  |     comp = fixture.componentInstance; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion simple-example-before-each
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion simple-example-it
 | 
					
						
							|  |  |  |   it('should display original title', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     // trigger change detection to update the view
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     // query for the title <h1> by CSS element selector
 | 
					
						
							|  |  |  |     de = fixture.debugElement.query(By.css('h1')); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // confirm the element's content
 | 
					
						
							| 
									
										
										
										
											2016-09-23 02:03:20 -07:00
										 |  |  |     expect(de.nativeElement.textContent).toContain(comp.title); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion simple-example-it
 | 
					
						
							|  |  |  | }); |