| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | import { Component, DebugElement }   from '@angular/core'; | 
					
						
							|  |  |  | import { ComponentFixture, TestBed } from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | import { By } from '@angular/platform-browser'; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | import { HighlightDirective } from './highlight.directive'; | 
					
						
							|  |  |  | import { newEvent }           from '../../testing'; | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | // #docregion test-component
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | @Component({ | 
					
						
							|  |  |  |   template: `
 | 
					
						
							|  |  |  |   <h2 highlight="yellow">Something Yellow</h2> | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   <h2 highlight>The Default (Gray)</h2> | 
					
						
							|  |  |  |   <h2>No Highlight</h2> | 
					
						
							|  |  |  |   <input #box [highlight]="box.value" value="cyan"/>`
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | }) | 
					
						
							|  |  |  | class TestComponent { } | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  | // #enddocregion test-component
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('HighlightDirective', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let fixture: ComponentFixture<TestComponent>; | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   let des: DebugElement[];  // the three elements w/ the directive
 | 
					
						
							|  |  |  |   let bareH2: DebugElement; // the <h2> w/o the directive
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   // #docregion selected-tests
 | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     fixture = TestBed.configureTestingModule({ | 
					
						
							|  |  |  |       declarations: [ HighlightDirective, TestComponent ] | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .createComponent(TestComponent); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |     fixture.detectChanges(); // initial binding
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // all elements with an attached HighlightDirective
 | 
					
						
							|  |  |  |     des = fixture.debugElement.queryAll(By.directive(HighlightDirective)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // the h2 without the HighlightDirective
 | 
					
						
							|  |  |  |     bareH2 = fixture.debugElement.query(By.css('h2:not([highlight])')); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   // color tests
 | 
					
						
							|  |  |  |   it('should have three highlighted elements', () => { | 
					
						
							|  |  |  |     expect(des.length).toBe(3); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   it('should color 1st <h2> background "yellow"', () => { | 
					
						
							|  |  |  |     expect(des[0].styles['backgroundColor']).toBe('yellow'); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   it('should color 2nd <h2> background w/ default color', () => { | 
					
						
							|  |  |  |     const dir = des[1].injector.get(HighlightDirective) as HighlightDirective; | 
					
						
							|  |  |  |     expect(des[1].styles['backgroundColor']).toBe(dir.defaultColor); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |   it('should bind <input> background to value color', () => { | 
					
						
							|  |  |  |     // easier to work with nativeElement
 | 
					
						
							|  |  |  |     const input = des[2].nativeElement as HTMLInputElement; | 
					
						
							|  |  |  |     expect(input.style.backgroundColor).toBe('cyan', 'initial backgroundColor'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // dispatch a DOM event so that Angular responds to the input value change.
 | 
					
						
							|  |  |  |     input.value = 'green'; | 
					
						
							|  |  |  |     input.dispatchEvent(newEvent('input')); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |     fixture.detectChanges(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 19:57:59 -07:00
										 |  |  |     expect(input.style.backgroundColor).toBe('green', 'changed backgroundColor'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // customProperty tests
 | 
					
						
							|  |  |  |   it('all highlighted elements should have a true customProperty', () => { | 
					
						
							|  |  |  |     const allTrue = des.map(de => !!de.properties['customProperty']).every(v => v === true); | 
					
						
							|  |  |  |     expect(allTrue).toBe(true); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('bare <h2> should not have a customProperty', () => { | 
					
						
							|  |  |  |     expect(bareH2.properties['customProperty']).toBeUndefined(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // #enddocregion selected-tests
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // injected directive
 | 
					
						
							|  |  |  |   // attached HighlightDirective can be injected
 | 
					
						
							|  |  |  |   it('can inject `HighlightDirective` in 1st <h2>', () => { | 
					
						
							|  |  |  |     const dir = des[0].injector.get(HighlightDirective); | 
					
						
							|  |  |  |     expect(dir).toBeTruthy(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('cannot inject `HighlightDirective` in 3rd <h2>', () => { | 
					
						
							|  |  |  |     const dir = bareH2.injector.get(HighlightDirective, null); | 
					
						
							|  |  |  |     expect(dir).toBe(null); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // DebugElement.providerTokens
 | 
					
						
							|  |  |  |   // attached HighlightDirective should be listed in the providerTokens
 | 
					
						
							|  |  |  |   it('should have `HighlightDirective` in 1st <h2> providerTokens', () => { | 
					
						
							|  |  |  |     expect(des[0].providerTokens).toContain(HighlightDirective); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should not have `HighlightDirective` in 3rd <h2> providerTokens', () => { | 
					
						
							|  |  |  |     expect(bareH2.providerTokens).not.toContain(HighlightDirective); | 
					
						
							| 
									
										
										
										
											2016-09-13 14:39:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |