|
| 1 | +import {TestBed, async} from '@angular/core/testing'; |
| 2 | +import { |
| 3 | + NgModule, |
| 4 | + Component, |
| 5 | + ViewChild, |
| 6 | + ElementRef, |
| 7 | +} from '@angular/core'; |
| 8 | +import {ProjectionModule, MdProjectionService, MdProjectionHostDirective} from './projection'; |
| 9 | + |
| 10 | + |
| 11 | +describe('Projection', () => { |
| 12 | + beforeEach(async(() => { |
| 13 | + TestBed.configureTestingModule({ |
| 14 | + imports: [ProjectionModule.forRoot(), ProjectionTestModule], |
| 15 | + }); |
| 16 | + |
| 17 | + TestBed.compileComponents(); |
| 18 | + })); |
| 19 | + |
| 20 | + it('should project properly', async(() => { |
| 21 | + const fixture = TestBed.createComponent(ProjectionTestApp); |
| 22 | + const appEl: HTMLDivElement = fixture.nativeElement; |
| 23 | + const outerDivEl = appEl.querySelector('.outer'); |
| 24 | + const innerDivEl = appEl.querySelector('.inner'); |
| 25 | + |
| 26 | + // Expect the reverse of the tests down there. |
| 27 | + expect(appEl.querySelector('md-host')).not.toBeNull(); |
| 28 | + expect(outerDivEl.querySelector('.inner')).not.toBe(innerDivEl); |
| 29 | + |
| 30 | + const innerHtml = appEl.innerHTML; |
| 31 | + |
| 32 | + // Trigger OnInit (and thus the projection). |
| 33 | + fixture.detectChanges(); |
| 34 | + |
| 35 | + expect(appEl.innerHTML).not.toEqual(innerHtml); |
| 36 | + |
| 37 | + // Assert `<md-host>` is not in the DOM anymore. |
| 38 | + expect(appEl.querySelector('md-host')).toBeNull(); |
| 39 | + |
| 40 | + // Assert the outerDiv contains the innerDiv. |
| 41 | + expect(outerDivEl.querySelector('.inner')).toBe(innerDivEl); |
| 42 | + |
| 43 | + // Assert the innerDiv contains the content. |
| 44 | + expect(innerDivEl.querySelector('.content')).not.toBeNull(); |
| 45 | + })); |
| 46 | +}); |
| 47 | + |
| 48 | + |
| 49 | +/** Test-bed component that contains a projection. */ |
| 50 | +@Component({ |
| 51 | + selector: '[projection-test]', |
| 52 | + template: ` |
| 53 | + <div class="outer"> |
| 54 | + <md-host><ng-content></ng-content></md-host> |
| 55 | + </div> |
| 56 | + `, |
| 57 | +}) |
| 58 | +class ProjectionTestComponent { |
| 59 | + @ViewChild(MdProjectionHostDirective) _host: MdProjectionHostDirective; |
| 60 | + |
| 61 | + constructor(private _projection: MdProjectionService, private _ref: ElementRef) {} |
| 62 | + ngOnInit() { this._projection.project(this._ref, this._host); } |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +/** Test-bed component that contains a portal host and a couple of template portals. */ |
| 67 | +@Component({ |
| 68 | + selector: 'projection-app', |
| 69 | + template: ` |
| 70 | + <div projection-test class="inner"> |
| 71 | + <div class="content"></div> |
| 72 | + </div> |
| 73 | + `, |
| 74 | +}) |
| 75 | +class ProjectionTestApp { |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +const TEST_COMPONENTS = [ProjectionTestApp, ProjectionTestComponent]; |
| 81 | +@NgModule({ |
| 82 | + imports: [ProjectionModule], |
| 83 | + exports: TEST_COMPONENTS, |
| 84 | + declarations: TEST_COMPONENTS, |
| 85 | + entryComponents: TEST_COMPONENTS, |
| 86 | +}) |
| 87 | +class ProjectionTestModule { } |
| 88 | + |
0 commit comments