Skip to content

Commit bf03d14

Browse files
crisbetojosephperrott
authored andcommitted
chore: fix tests leaking overlay containers (#9076)
1 parent a779017 commit bf03d14

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
@@ -29,7 +29,7 @@ describe('OverlayContainer', () => {
2929
overlayRef.attach(fixture.componentInstance.templatePortal);
3030
fixture.detectChanges();
3131

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

3535
// 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
@@ -70,9 +70,12 @@ describe('MatTooltip', () => {
7070
})();
7171
}));
7272

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

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

0 commit comments

Comments
 (0)