Skip to content

Commit 983363c

Browse files
crisbetojosephperrott
authored andcommitted
chore: fix tests leaking overlay containers (angular#9076)
1 parent c075fc2 commit 983363c

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/cdk/overlay/overlay-container.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('OverlayContainer', () => {
3737
overlayRef.attach(fixture.componentInstance.templatePortal);
3838
fixture.detectChanges();
3939

40-
expect(document.querySelectorAll('.cdk-overlay-container'))
40+
expect(document.querySelector('.cdk-overlay-container'))
4141
.not.toBeNull('Expected the overlay container to be in the DOM after opening an overlay');
4242

4343
// Manually call `ngOnDestroy` because there is no way to force Angular to destroy an

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ describe('MatAutocomplete', () => {
8989
return TestBed.createComponent(component);
9090
}
9191

92-
afterEach(() => {
93-
if (overlayContainer) {
94-
overlayContainer.ngOnDestroy();
95-
}
96-
});
92+
afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
93+
// Since we're resetting the testing module in some of the tests,
94+
// we can potentially have multiple overlay containers.
95+
currentOverlayContainer.ngOnDestroy();
96+
overlayContainer.ngOnDestroy();
97+
}));
9798

9899
describe('panel toggling', () => {
99100
let fixture: ComponentFixture<SimpleAutocomplete>;
@@ -598,6 +599,7 @@ describe('MatAutocomplete', () => {
598599
});
599600

600601
it('should disable the input when used with a value accessor and without `matInput`', () => {
602+
overlayContainer.ngOnDestroy();
601603
fixture.destroy();
602604
TestBed.resetTestingModule();
603605

src/lib/menu/menu.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ describe('MatMenu', () => {
7272
})();
7373
}));
7474

75-
afterEach(() => {
75+
afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
76+
// Since we're resetting the testing module in some of the tests,
77+
// we can potentially have multiple overlay containers.
78+
currentOverlayContainer.ngOnDestroy();
7679
overlayContainer.ngOnDestroy();
77-
});
80+
}));
7881

7982
it('should open the menu as an idempotent operation', () => {
8083
const fixture = TestBed.createComponent(SimpleMenu);

src/lib/tooltip/tooltip.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ describe('MatTooltip', () => {
6969
})();
7070
}));
7171

72-
afterEach(() => {
72+
afterEach(inject([OverlayContainer], (currentOverlayContainer: OverlayContainer) => {
73+
// Since we're resetting the testing module in some of the tests,
74+
// we can potentially have multiple overlay containers.
75+
currentOverlayContainer.ngOnDestroy();
7376
overlayContainer.ngOnDestroy();
74-
});
77+
}));
7578

7679
describe('basic usage', () => {
7780
let fixture: ComponentFixture<BasicTooltipDemo>;

0 commit comments

Comments
 (0)