From 76372eee148272ec2037bf027c70ecd0e11e25f0 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Wed, 20 Dec 2017 22:15:48 +0100 Subject: [PATCH] chore: fix tests leaking overlay containers Fixes some overlay containers not being cleaned out between tests due to particular tests resetting the testing module. --- src/cdk/overlay/overlay-container.spec.ts | 2 +- src/lib/autocomplete/autocomplete.spec.ts | 12 +++++++----- src/lib/menu/menu.spec.ts | 7 +++++-- src/lib/tooltip/tooltip.spec.ts | 7 +++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/cdk/overlay/overlay-container.spec.ts b/src/cdk/overlay/overlay-container.spec.ts index ee635f22f033..312ddfb96a5b 100644 --- a/src/cdk/overlay/overlay-container.spec.ts +++ b/src/cdk/overlay/overlay-container.spec.ts @@ -29,7 +29,7 @@ describe('OverlayContainer', () => { overlayRef.attach(fixture.componentInstance.templatePortal); fixture.detectChanges(); - expect(document.querySelectorAll('.cdk-overlay-container')) + expect(document.querySelector('.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 diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index fbf79bda30eb..3425f263732a 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -89,11 +89,12 @@ describe('MatAutocomplete', () => { return TestBed.createComponent(component); } - afterEach(() => { - if (overlayContainer) { - overlayContainer.ngOnDestroy(); - } - }); + afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => { + // Since we're resetting the testing module in some of the tests, + // we can potentially have multiple overlay containers. + currentOverlayContainer.ngOnDestroy(); + overlayContainer.ngOnDestroy(); + })); describe('panel toggling', () => { let fixture: ComponentFixture; @@ -598,6 +599,7 @@ describe('MatAutocomplete', () => { }); it('should disable the input when used with a value accessor and without `matInput`', () => { + overlayContainer.ngOnDestroy(); fixture.destroy(); TestBed.resetTestingModule(); diff --git a/src/lib/menu/menu.spec.ts b/src/lib/menu/menu.spec.ts index 13443427e397..8fdaffa64048 100644 --- a/src/lib/menu/menu.spec.ts +++ b/src/lib/menu/menu.spec.ts @@ -72,9 +72,12 @@ describe('MatMenu', () => { })(); })); - afterEach(() => { + afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => { + // Since we're resetting the testing module in some of the tests, + // we can potentially have multiple overlay containers. + currentOverlayContainer.ngOnDestroy(); overlayContainer.ngOnDestroy(); - }); + })); it('should open the menu as an idempotent operation', () => { const fixture = TestBed.createComponent(SimpleMenu); diff --git a/src/lib/tooltip/tooltip.spec.ts b/src/lib/tooltip/tooltip.spec.ts index 5291b89b5948..f892f6e0f050 100644 --- a/src/lib/tooltip/tooltip.spec.ts +++ b/src/lib/tooltip/tooltip.spec.ts @@ -70,9 +70,12 @@ describe('MatTooltip', () => { })(); })); - afterEach(() => { + afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => { + // Since we're resetting the testing module in some of the tests, + // we can potentially have multiple overlay containers. + currentOverlayContainer.ngOnDestroy(); overlayContainer.ngOnDestroy(); - }); + })); describe('basic usage', () => { let fixture: ComponentFixture;