|
| 1 | +import {async, inject, TestBed} from '@angular/core/testing'; |
| 2 | +import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core'; |
| 3 | +import {PortalModule, TemplatePortalDirective} from '../portal/portal-directives'; |
| 4 | +import {Overlay, OverlayContainer, OverlayModule} from './index'; |
| 5 | + |
| 6 | +describe('OverlayContainer', () => { |
| 7 | + let overlay: Overlay; |
| 8 | + let overlayContainer: OverlayContainer; |
| 9 | + |
| 10 | + beforeEach(async(() => { |
| 11 | + TestBed.configureTestingModule({ |
| 12 | + imports: [OverlayTestModule] |
| 13 | + }).compileComponents(); |
| 14 | + })); |
| 15 | + |
| 16 | + beforeEach(inject([Overlay, OverlayContainer], (o: Overlay, oc: OverlayContainer) => { |
| 17 | + overlay = o; |
| 18 | + overlayContainer = oc; |
| 19 | + })); |
| 20 | + |
| 21 | + it('should remove the overlay container element from the DOM on destruction', () => { |
| 22 | + const fixture = TestBed.createComponent(TestComponentWithTemplatePortals); |
| 23 | + |
| 24 | + const overlayRef = overlay.create(); |
| 25 | + overlayRef.attach(fixture.componentInstance.templatePortal); |
| 26 | + fixture.detectChanges(); |
| 27 | + |
| 28 | + expect(document.body.querySelector('.cdk-overlay-container')) |
| 29 | + .not.toBeNull('Expected the overlay container to be in the DOM after opening an overlay'); |
| 30 | + |
| 31 | + // Manually call `ngOnDestroy` because there is no way to force Angular to destroy an |
| 32 | + // injectable in a unit test. |
| 33 | + overlayContainer.ngOnDestroy(); |
| 34 | + |
| 35 | + expect(document.body.querySelector('.cdk-overlay-container')) |
| 36 | + .toBeNull('Expected the overlay container *not* to be in the DOM after destruction'); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +/** Test-bed component that contains a TempatePortal and an ElementRef. */ |
| 41 | +@Component({ |
| 42 | + template: `<ng-template cdk-portal>Cake</ng-template>`, |
| 43 | + providers: [Overlay], |
| 44 | +}) |
| 45 | +class TestComponentWithTemplatePortals { |
| 46 | + @ViewChild(TemplatePortalDirective) templatePortal: TemplatePortalDirective; |
| 47 | + |
| 48 | + constructor(public viewContainerRef: ViewContainerRef) { } |
| 49 | +} |
| 50 | + |
| 51 | +@NgModule({ |
| 52 | + imports: [OverlayModule, PortalModule], |
| 53 | + declarations: [TestComponentWithTemplatePortals] |
| 54 | +}) |
| 55 | +class OverlayTestModule { } |
0 commit comments