Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(overlay): remove overlay container on destroy (#5378)
- Loading branch information
Showing
2 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {async, inject, TestBed} from '@angular/core/testing'; | ||
import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core'; | ||
import {PortalModule, TemplatePortalDirective} from '../portal/portal-directives'; | ||
import {Overlay, OverlayContainer, OverlayModule} from './index'; | ||
|
||
describe('OverlayContainer', () => { | ||
let overlay: Overlay; | ||
let overlayContainer: OverlayContainer; | ||
|
||
beforeAll(() => { | ||
// Remove any stale overlay containers from previous tests that didn't clean up correctly. | ||
const staleContainers = document.querySelectorAll('.cdk-overlay-container'); | ||
for (let i = staleContainers.length - 1; i >= 0; i--) { | ||
staleContainers[i].parentNode!.removeChild(staleContainers[i]); | ||
} | ||
}); | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [OverlayTestModule] | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(inject([Overlay, OverlayContainer], (o: Overlay, oc: OverlayContainer) => { | ||
overlay = o; | ||
overlayContainer = oc; | ||
})); | ||
|
||
it('should remove the overlay container element from the DOM on destruction', () => { | ||
const fixture = TestBed.createComponent(TestComponentWithTemplatePortals); | ||
|
||
const overlayRef = overlay.create(); | ||
overlayRef.attach(fixture.componentInstance.templatePortal); | ||
fixture.detectChanges(); | ||
|
||
expect(document.querySelectorAll('.cdk-overlay-container')) | ||
.not.toBeNull('Expected the overlay container to be in the DOM after opening an overlay'); | ||
|
||
// Manually call `ngOnDestroy` because there is no way to force Angular to destroy an | ||
// injectable in a unit test. | ||
overlayContainer.ngOnDestroy(); | ||
|
||
expect(document.querySelector('.cdk-overlay-container')) | ||
.toBeNull('Expected the overlay container *not* to be in the DOM after destruction'); | ||
}); | ||
}); | ||
|
||
/** Test-bed component that contains a TempatePortal and an ElementRef. */ | ||
@Component({ | ||
template: `<ng-template cdk-portal>Cake</ng-template>`, | ||
providers: [Overlay], | ||
}) | ||
class TestComponentWithTemplatePortals { | ||
@ViewChild(TemplatePortalDirective) templatePortal: TemplatePortalDirective; | ||
|
||
constructor(public viewContainerRef: ViewContainerRef) { } | ||
} | ||
|
||
@NgModule({ | ||
imports: [OverlayModule, PortalModule], | ||
declarations: [TestComponentWithTemplatePortals] | ||
}) | ||
class OverlayTestModule { } |