Skip to content

Commit 20f6eb9

Browse files
crisbetojelbourn
authored andcommitted
chore: fix menu harness tests (#17018)
Fixes a failure that wasn't caught by the CI in #17007 for some reason. We were only injecting the overlay container for one kind of test harness.
1 parent 492fdcc commit 20f6eb9

File tree

3 files changed

+64
-70
lines changed

3 files changed

+64
-70
lines changed

src/material-experimental/mdc-autocomplete/harness/autocomplete-harness.spec.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {HarnessLoader} from '@angular/cdk-experimental/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3-
import {Component} from '@angular/core';
3+
import {Component, Type} from '@angular/core';
44
import {ComponentFixture, TestBed, inject} from '@angular/core/testing';
55
import {MatAutocompleteModule} from '@angular/material/autocomplete';
66
import {OverlayContainer} from '@angular/cdk/overlay';
@@ -16,33 +16,16 @@ let overlayContainer: OverlayContainer;
1616
describe('MatAutocompleteHarness', () => {
1717
describe('non-MDC-based', () => {
1818
beforeEach(async () => {
19-
await TestBed.configureTestingModule({
20-
imports: [MatAutocompleteModule],
21-
declarations: [AutocompleteHarnessTest],
22-
}).compileComponents();
23-
24-
fixture = TestBed.createComponent(AutocompleteHarnessTest);
25-
fixture.detectChanges();
26-
loader = TestbedHarnessEnvironment.loader(fixture);
19+
await prepareTests(MatAutocompleteModule, AutocompleteHarnessTest);
2720
harness = MatAutocompleteHarness;
28-
inject([OverlayContainer], (oc: OverlayContainer) => {
29-
overlayContainer = oc;
30-
})();
3121
});
3222

3323
runTests();
3424
});
3525

3626
describe('MDC-based', () => {
3727
beforeEach(async () => {
38-
await TestBed.configureTestingModule({
39-
imports: [MatMdcAutocompleteModule],
40-
declarations: [AutocompleteHarnessTest],
41-
}).compileComponents();
42-
43-
fixture = TestBed.createComponent(AutocompleteHarnessTest);
44-
fixture.detectChanges();
45-
loader = TestbedHarnessEnvironment.loader(fixture);
28+
await prepareTests(MatMdcAutocompleteModule, AutocompleteHarnessTest);
4629
// Public APIs are the same as MatAutocompleteHarness, but cast
4730
// is necessary because of different private fields.
4831
harness = MatMdcAutocompleteHarness as any;
@@ -53,11 +36,27 @@ describe('MatAutocompleteHarness', () => {
5336
});
5437
});
5538

39+
/** Shared test setup logic. */
40+
async function prepareTests(moduleType: Type<any>, fixtureType: Type<any>) {
41+
await TestBed.configureTestingModule({
42+
imports: [moduleType],
43+
declarations: [fixtureType],
44+
}).compileComponents();
45+
46+
fixture = TestBed.createComponent(fixtureType);
47+
fixture.detectChanges();
48+
loader = TestbedHarnessEnvironment.loader(fixture);
49+
inject([OverlayContainer], (oc: OverlayContainer) => {
50+
overlayContainer = oc;
51+
})();
52+
}
53+
5654
/** Shared tests to run on both the original and MDC-based autocomplete. */
5755
function runTests() {
5856
afterEach(() => {
5957
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
6058
overlayContainer.ngOnDestroy();
59+
overlayContainer = null!;
6160
});
6261

6362
it('should load all autocomplete harnesses', async () => {

src/material-experimental/mdc-menu/harness/menu-harness.spec.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {HarnessLoader} from '@angular/cdk-experimental/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3-
import {Component} from '@angular/core';
3+
import {Component, Type} from '@angular/core';
44
import {ComponentFixture, TestBed, inject} from '@angular/core/testing';
55
import {MatMenuModule} from '@angular/material/menu';
66
import {OverlayContainer} from '@angular/cdk/overlay';
@@ -16,33 +16,16 @@ let overlayContainer: OverlayContainer;
1616
describe('MatMenuHarness', () => {
1717
describe('non-MDC-based', () => {
1818
beforeEach(async () => {
19-
await TestBed.configureTestingModule({
20-
imports: [MatMenuModule],
21-
declarations: [MenuHarnessTest],
22-
}).compileComponents();
23-
24-
fixture = TestBed.createComponent(MenuHarnessTest);
25-
fixture.detectChanges();
26-
loader = TestbedHarnessEnvironment.loader(fixture);
19+
await prepareTests(MatMenuModule, MenuHarnessTest);
2720
menuHarness = MatMenuHarness;
28-
inject([OverlayContainer], (oc: OverlayContainer) => {
29-
overlayContainer = oc;
30-
})();
3121
});
3222

3323
runTests();
3424
});
3525

3626
describe('MDC-based', () => {
3727
beforeEach(async () => {
38-
await TestBed.configureTestingModule({
39-
imports: [MatMdcMenuModule],
40-
declarations: [MenuHarnessTest],
41-
}).compileComponents();
42-
43-
fixture = TestBed.createComponent(MenuHarnessTest);
44-
fixture.detectChanges();
45-
loader = TestbedHarnessEnvironment.loader(fixture);
28+
await prepareTests(MatMdcMenuModule, MenuHarnessTest);
4629
// Public APIs are the same as MatMenuHarness, but cast is necessary because of different
4730
// private fields.
4831
menuHarness = MatMdcMenuHarness as any;
@@ -52,11 +35,27 @@ describe('MatMenuHarness', () => {
5235
});
5336
});
5437

38+
/** Shared test setup logic. */
39+
async function prepareTests(moduleType: Type<any>, fixtureType: Type<any>) {
40+
await TestBed.configureTestingModule({
41+
imports: [moduleType],
42+
declarations: [fixtureType],
43+
}).compileComponents();
44+
45+
fixture = TestBed.createComponent(fixtureType);
46+
fixture.detectChanges();
47+
loader = TestbedHarnessEnvironment.loader(fixture);
48+
inject([OverlayContainer], (oc: OverlayContainer) => {
49+
overlayContainer = oc;
50+
})();
51+
}
52+
5553
/** Shared tests to run on both the original and MDC-based menues. */
5654
function runTests() {
5755
afterEach(() => {
5856
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
5957
overlayContainer.ngOnDestroy();
58+
overlayContainer = null!;
6059
});
6160

6261
it('should load all menu harnesses', async () => {

src/material-experimental/mdc-select/harness/select-harness.spec.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {HarnessLoader} from '@angular/cdk-experimental/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3-
import {Component} from '@angular/core';
3+
import {Component, Type} from '@angular/core';
44
import {ComponentFixture, TestBed, inject} from '@angular/core/testing';
55
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
66
import {ReactiveFormsModule, FormControl, Validators} from '@angular/forms';
@@ -19,43 +19,16 @@ let overlayContainer: OverlayContainer;
1919
describe('MatSelectHarness', () => {
2020
describe('non-MDC-based', () => {
2121
beforeEach(async () => {
22-
await TestBed.configureTestingModule({
23-
imports: [
24-
MatSelectModule,
25-
MatFormFieldModule,
26-
NoopAnimationsModule,
27-
ReactiveFormsModule,
28-
],
29-
declarations: [SelectHarnessTest],
30-
}).compileComponents();
31-
32-
fixture = TestBed.createComponent(SelectHarnessTest);
33-
fixture.detectChanges();
34-
loader = TestbedHarnessEnvironment.loader(fixture);
22+
await prepareTests(MatSelectModule, SelectHarnessTest);
3523
harness = MatSelectHarness;
36-
inject([OverlayContainer], (oc: OverlayContainer) => {
37-
overlayContainer = oc;
38-
})();
3924
});
4025

4126
runTests();
4227
});
4328

4429
describe('MDC-based', () => {
4530
beforeEach(async () => {
46-
await TestBed.configureTestingModule({
47-
imports: [
48-
MatMdcSelectModule,
49-
MatFormFieldModule,
50-
NoopAnimationsModule,
51-
ReactiveFormsModule,
52-
],
53-
declarations: [SelectHarnessTest],
54-
}).compileComponents();
55-
56-
fixture = TestBed.createComponent(SelectHarnessTest);
57-
fixture.detectChanges();
58-
loader = TestbedHarnessEnvironment.loader(fixture);
31+
await prepareTests(MatMdcSelectModule, SelectHarnessTest);
5932
// Public APIs are the same as MatSelectHarness, but cast
6033
// is necessary because of different private fields.
6134
harness = MatMdcSelectHarness as any;
@@ -66,11 +39,34 @@ describe('MatSelectHarness', () => {
6639
});
6740
});
6841

42+
/** Shared test setup logic. */
43+
async function prepareTests(moduleType: Type<any>, fixtureType: Type<any>) {
44+
await TestBed.configureTestingModule({
45+
imports: [
46+
moduleType,
47+
MatFormFieldModule,
48+
NoopAnimationsModule,
49+
ReactiveFormsModule,
50+
],
51+
declarations: [fixtureType],
52+
}).compileComponents();
53+
54+
fixture = TestBed.createComponent(fixtureType);
55+
fixture.detectChanges();
56+
loader = TestbedHarnessEnvironment.loader(fixture);
57+
inject([OverlayContainer], (oc: OverlayContainer) => {
58+
overlayContainer = oc;
59+
})();
60+
}
61+
62+
63+
6964
/** Shared tests to run on both the original and MDC-based select. */
7065
function runTests() {
7166
afterEach(() => {
7267
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
7368
overlayContainer.ngOnDestroy();
69+
overlayContainer = null!;
7470
});
7571

7672
it('should load all select harnesses', async () => {

0 commit comments

Comments
 (0)