Skip to content

Commit 170299b

Browse files
mmalerbajelbourn
authored andcommitted
feat(menu): move test harness out of experimental (#17113)
1 parent 35a484b commit 170299b

16 files changed

+234
-137
lines changed

src/material-experimental/mdc-menu/BUILD.bazel

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package(default_visibility = ["//visibility:public"])
22

33
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary", "sass_library")
44
load("//src/e2e-app:test_suite.bzl", "e2e_test_suite")
5-
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_module", "ng_test_library", "ng_web_test_suite", "ts_library")
5+
load("//tools:defaults.bzl", "ng_e2e_test_library", "ng_module", "ng_test_library", "ng_web_test_suite")
66

77
ng_module(
88
name = "mdc-menu",
99
srcs = glob(
1010
["**/*.ts"],
1111
exclude = [
1212
"**/*.spec.ts",
13-
"harness/**",
1413
],
1514
),
1615
assets = [":menu_scss"] + glob(["**/*.html"]),
@@ -25,18 +24,6 @@ ng_module(
2524
],
2625
)
2726

28-
ts_library(
29-
name = "harness",
30-
srcs = glob(
31-
["harness/**/*.ts"],
32-
exclude = ["**/*.spec.ts"],
33-
),
34-
deps = [
35-
"//src/cdk/coercion",
36-
"//src/cdk/testing",
37-
],
38-
)
39-
4027
sass_library(
4128
name = "mdc_menu_scss_lib",
4229
srcs = glob(["**/_*.scss"]),
@@ -65,7 +52,6 @@ ng_test_library(
6552
exclude = ["**/*.e2e.spec.ts"],
6653
),
6754
deps = [
68-
":harness",
6955
":mdc-menu",
7056
"//src/cdk/a11y",
7157
"//src/cdk/bidi",
@@ -74,7 +60,6 @@ ng_test_library(
7460
"//src/cdk/private/testing",
7561
"//src/cdk/scrolling",
7662
"//src/cdk/testing",
77-
"//src/cdk/testing/testbed",
7863
"//src/material/core",
7964
"//src/material/menu",
8065
"@npm//@angular/platform-browser",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")
4+
5+
ng_module(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material-experimental/mdc-menu/testing",
12+
deps = [
13+
"//src/cdk/coercion",
14+
"//src/cdk/testing",
15+
"//src/material/menu/testing",
16+
],
17+
)
18+
19+
ng_test_library(
20+
name = "unit_tests_lib",
21+
srcs = glob(["**/*.spec.ts"]),
22+
deps = [
23+
":testing",
24+
"//src/material-experimental/mdc-menu",
25+
"//src/material/menu/testing:harness_tests_lib",
26+
],
27+
)
28+
29+
ng_web_test_suite(
30+
name = "unit_tests",
31+
deps = [
32+
":unit_tests_lib",
33+
"//src/material-experimental:mdc_require_config.js",
34+
],
35+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './public-api';
Lines changed: 4 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,7 @@
1-
import {HarnessLoader} from '@angular/cdk/testing';
2-
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3-
import {Component, Type} from '@angular/core';
4-
import {ComponentFixture, TestBed, inject} from '@angular/core/testing';
5-
import {MatMenuModule} from '@angular/material/menu';
6-
import {OverlayContainer} from '@angular/cdk/overlay';
7-
import {MatMenuModule as MatMdcMenuModule} from '../index';
8-
import {MatMenuHarness as MatMdcMenuHarness} from './mdc-menu-harness';
1+
import {runHarnessTests} from '@angular/material/menu/testing/shared.spec';
2+
import {MatMenuModule} from '../index';
93
import {MatMenuHarness} from './menu-harness';
104

11-
let fixture: ComponentFixture<MenuHarnessTest>;
12-
let loader: HarnessLoader;
13-
let menuHarness: typeof MatMenuHarness;
14-
let overlayContainer: OverlayContainer;
15-
16-
describe('MatMenuHarness', () => {
17-
describe('non-MDC-based', () => {
18-
beforeEach(async () => {
19-
await prepareTests(MatMenuModule, MenuHarnessTest);
20-
menuHarness = MatMenuHarness;
21-
});
22-
23-
runTests();
24-
});
25-
26-
describe('MDC-based', () => {
27-
beforeEach(async () => {
28-
await prepareTests(MatMdcMenuModule, MenuHarnessTest);
29-
// Public APIs are the same as MatMenuHarness, but cast is necessary because of different
30-
// private fields.
31-
menuHarness = MatMdcMenuHarness as any;
32-
});
33-
34-
runTests();
35-
});
5+
describe('MDC-based MatMenuHarness', () => {
6+
runHarnessTests(MatMenuModule, MatMenuHarness);
367
});
37-
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-
53-
/** Shared tests to run on both the original and MDC-based menues. */
54-
function runTests() {
55-
afterEach(() => {
56-
// Angular won't call this for us so we need to do it ourselves to avoid leaks.
57-
overlayContainer.ngOnDestroy();
58-
overlayContainer = null!;
59-
});
60-
61-
it('should load all menu harnesses', async () => {
62-
const menues = await loader.getAllHarnesses(menuHarness);
63-
expect(menues.length).toBe(2);
64-
});
65-
66-
it('should load menu with exact text', async () => {
67-
const menus = await loader.getAllHarnesses(menuHarness.with({triggerText: 'Settings'}));
68-
expect(menus.length).toBe(1);
69-
expect(await menus[0].getTriggerText()).toBe('Settings');
70-
});
71-
72-
it('should load menu with regex label match', async () => {
73-
const menus = await loader.getAllHarnesses(menuHarness.with({triggerText: /settings/i}));
74-
expect(menus.length).toBe(1);
75-
expect(await menus[0].getTriggerText()).toBe('Settings');
76-
});
77-
78-
it('should get disabled state', async () => {
79-
const [enabledMenu, disabledMenu] = await loader.getAllHarnesses(menuHarness);
80-
expect(await enabledMenu.isDisabled()).toBe(false);
81-
expect(await disabledMenu.isDisabled()).toBe(true);
82-
});
83-
84-
it('should get menu text', async () => {
85-
const [firstMenu, secondMenu] = await loader.getAllHarnesses(menuHarness);
86-
expect(await firstMenu.getTriggerText()).toBe('Settings');
87-
expect(await secondMenu.getTriggerText()).toBe('Disabled menu');
88-
});
89-
90-
it('should focus and blur a menu', async () => {
91-
const menu = await loader.getHarness(menuHarness.with({triggerText: 'Settings'}));
92-
expect(getActiveElementId()).not.toBe('settings');
93-
await menu.focus();
94-
expect(getActiveElementId()).toBe('settings');
95-
await menu.blur();
96-
expect(getActiveElementId()).not.toBe('settings');
97-
});
98-
}
99-
100-
function getActiveElementId() {
101-
return document.activeElement ? document.activeElement.id : '';
102-
}
103-
104-
@Component({
105-
template: `
106-
<button type="button" id="settings" [matMenuTriggerFor]="settingsMenu">Settings</button>
107-
<button type="button" disabled [matMenuTriggerFor]="settingsMenu">Disabled menu</button>
108-
109-
<mat-menu #settingsMenu>
110-
<menu mat-menu-item>Profile</menu>
111-
<menu mat-menu-item>Account</menu>
112-
</mat-menu>
113-
`
114-
})
115-
class MenuHarnessTest { }
116-

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11-
import {MenuHarnessFilters} from './menu-harness-filters';
1211
import {MatMenuItemHarness} from './menu-item-harness';
12+
import {MenuHarnessFilters} from '@angular/material/menu/testing/menu-harness-filters';
1313

1414
/**
15-
* Harness for interacting with a standard mat-menu in tests.
15+
* Harness for interacting with a MDC-based mat-menu in tests.
1616
* @dynamic
1717
*/
1818
export class MatMenuHarness extends ComponentHarness {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11-
import {MenuItemHarnessFilters} from './menu-harness-filters';
11+
import {MenuItemHarnessFilters} from '@angular/material/menu/testing/menu-harness-filters';
1212

1313

1414
/**
@@ -26,7 +26,7 @@ export class MatMenuItemHarness extends ComponentHarness {
2626
* @return a `HarnessPredicate` configured with the given options.
2727
*/
2828
static with(options: MenuItemHarnessFilters = {}): HarnessPredicate<MatMenuItemHarness> {
29-
return new HarnessPredicate(MatMenuItemHarness, options); // TODO: add options here
29+
return new HarnessPredicate(MatMenuItemHarness, options);
3030
}
3131

3232
/** Gets a boolean promise indicating if the menu is disabled. */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './menu-harness';
10+
export * from './menu-item-harness';

src/material/menu/testing/BUILD.bazel

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")
4+
5+
ng_module(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material/menu/testing",
12+
deps = [
13+
"//src/cdk/coercion",
14+
"//src/cdk/testing",
15+
],
16+
)
17+
18+
ng_test_library(
19+
name = "harness_tests_lib",
20+
srcs = ["shared.spec.ts"],
21+
deps = [
22+
":testing",
23+
"//src/cdk/overlay",
24+
"//src/cdk/testing",
25+
"//src/cdk/testing/testbed",
26+
"//src/material/menu",
27+
"@npm//@angular/platform-browser",
28+
],
29+
)
30+
31+
ng_test_library(
32+
name = "unit_tests_lib",
33+
srcs = glob(
34+
["**/*.spec.ts"],
35+
exclude = ["shared.spec.ts"],
36+
),
37+
deps = [
38+
":harness_tests_lib",
39+
":testing",
40+
"//src/material/menu",
41+
],
42+
)
43+
44+
ng_web_test_suite(
45+
name = "unit_tests",
46+
deps = [":unit_tests_lib"],
47+
)

src/material/menu/testing/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './public-api';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {MatMenuModule} from '@angular/material/menu';
2+
import {runHarnessTests} from '@angular/material/menu/testing/shared.spec';
3+
import {MatMenuHarness} from './menu-harness';
4+
5+
describe('Non-MDC-based MatMenuHarness', () => {
6+
runHarnessTests(MatMenuModule, MatMenuHarness);
7+
});

src/material-experimental/mdc-menu/harness/mdc-menu-harness.ts renamed to src/material/menu/testing/menu-harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11-
import {MatMenuItemHarness} from './mdc-menu-item-harness';
1211
import {MenuHarnessFilters} from './menu-harness-filters';
12+
import {MatMenuItemHarness} from './menu-item-harness';
1313

1414
/**
15-
* Harness for interacting with a MDC-based mat-menu in tests.
15+
* Harness for interacting with a standard mat-menu in tests.
1616
* @dynamic
1717
*/
1818
export class MatMenuHarness extends ComponentHarness {

src/material-experimental/mdc-menu/harness/mdc-menu-item-harness.ts renamed to src/material/menu/testing/menu-item-harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11-
import {MenuItemHarnessFilters} from './menu-harness-filters';
11+
import {MenuItemHarnessFilters} from '@angular/material/menu/testing/menu-harness-filters';
1212

1313

1414
/**
@@ -26,7 +26,7 @@ export class MatMenuItemHarness extends ComponentHarness {
2626
* @return a `HarnessPredicate` configured with the given options.
2727
*/
2828
static with(options: MenuItemHarnessFilters = {}): HarnessPredicate<MatMenuItemHarness> {
29-
return new HarnessPredicate(MatMenuItemHarness, options);
29+
return new HarnessPredicate(MatMenuItemHarness, options); // TODO: add options here
3030
}
3131

3232
/** Gets a boolean promise indicating if the menu is disabled. */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './menu-harness';
10+
export * from './menu-harness-filters';
11+
export * from './menu-item-harness';

0 commit comments

Comments
 (0)