Skip to content

Commit 2d4cab2

Browse files
crisbetowagnermaciel
authored andcommitted
feat(material-experimenta/mdc-core): add test harnesses for MDC option and option group
Adds test harnesses for the MDC-based `MatOption` and `MatOptgroup`.
1 parent d7754e7 commit 2d4cab2

File tree

12 files changed

+230
-19
lines changed

12 files changed

+230
-19
lines changed

src/material-experimental/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ entryPoints = [
1010
"mdc-chips",
1111
"mdc-chips/testing",
1212
"mdc-core",
13+
"mdc-core/testing",
1314
"mdc-dialog",
1415
"mdc-dialog/testing",
1516
"mdc-form-field",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_library(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material-experimental/mdc-core/testing",
12+
deps = [
13+
"//src/cdk/testing",
14+
],
15+
)
16+
17+
filegroup(
18+
name = "source-files",
19+
srcs = glob(["**/*.ts"]),
20+
)
21+
22+
ng_test_library(
23+
name = "unit_tests_lib",
24+
srcs = glob(["**/*.spec.ts"]),
25+
deps = [
26+
":testing",
27+
"//src/material-experimental/mdc-core",
28+
"//src/material/core/testing:harness_tests_lib",
29+
],
30+
)
31+
32+
ng_web_test_suite(
33+
name = "unit_tests",
34+
deps = [":unit_tests_lib"],
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
import {BaseHarnessFilters} from '@angular/cdk/testing';
10+
11+
export interface OptgroupHarnessFilters extends BaseHarnessFilters {
12+
labelText?: string | RegExp;
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {MatOptionModule} from '@angular/material-experimental/mdc-core';
2+
import {runHarnessTests} from '@angular/material/core/testing/optgroup-shared.spec';
3+
import {MatOptgroupHarness} from './optgroup-harness';
4+
5+
describe('MDC-based MatOptgroupHarness', () => {
6+
runHarnessTests(MatOptionModule, MatOptgroupHarness as any);
7+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
10+
import {OptgroupHarnessFilters} from './optgroup-harness-filters';
11+
import {MatOptionHarness} from './option-harness';
12+
import {OptionHarnessFilters} from './option-harness-filters';
13+
14+
/** Harness for interacting with an MDC-based `mat-optgroup` in tests. */
15+
export class MatOptgroupHarness extends ComponentHarness {
16+
/** Selector used to locate option group instances. */
17+
static hostSelector = '.mat-mdc-optgroup';
18+
private _label = this.locatorFor('.mat-mdc-optgroup-label');
19+
20+
/**
21+
* Gets a `HarnessPredicate` that can be used to search for a `MatOptgroupHarness` that meets
22+
* certain criteria.
23+
* @param options Options for filtering which option instances are considered a match.
24+
* @return a `HarnessPredicate` configured with the given options.
25+
*/
26+
static with(options: OptgroupHarnessFilters = {}) {
27+
return new HarnessPredicate(MatOptgroupHarness, options)
28+
.addOption('labelText', options.labelText,
29+
async (harness, title) =>
30+
HarnessPredicate.stringMatches(await harness.getLabelText(), title));
31+
}
32+
33+
/** Gets the option group's label text. */
34+
async getLabelText(): Promise<string> {
35+
return (await this._label()).text();
36+
}
37+
38+
/** Gets whether the option group is disabled. */
39+
async isDisabled(): Promise<boolean> {
40+
return (await (await this.host()).getAttribute('aria-disabled')) === 'true';
41+
}
42+
43+
/**
44+
* Gets the options that are inside the group.
45+
* @param filter Optionally filters which options are included.
46+
*/
47+
async getOptions(filter: OptionHarnessFilters = {}): Promise<MatOptionHarness[]> {
48+
return this.locatorForAll(MatOptionHarness.with(filter))();
49+
}
50+
}
51+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
import {BaseHarnessFilters} from '@angular/cdk/testing';
10+
11+
export interface OptionHarnessFilters extends BaseHarnessFilters {
12+
text?: string | RegExp;
13+
isSelected?: boolean;
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {MatOptionModule, MatOption} from '@angular/material-experimental/mdc-core';
2+
import {runHarnessTests} from '@angular/material/core/testing/option-shared.spec';
3+
import {MatOptionHarness} from './option-harness';
4+
5+
describe('MDC-based MatOptionHarness', () => {
6+
runHarnessTests(MatOptionModule, MatOptionHarness as any, MatOption);
7+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
10+
import {OptionHarnessFilters} from './option-harness-filters';
11+
12+
/** Harness for interacting with an MDC-based `mat-option` in tests. */
13+
export class MatOptionHarness extends ComponentHarness {
14+
/** Selector used to locate option instances. */
15+
static hostSelector = '.mat-mdc-option';
16+
17+
/**
18+
* Gets a `HarnessPredicate` that can be used to search for a `MatOptionsHarness` that meets
19+
* certain criteria.
20+
* @param options Options for filtering which option instances are considered a match.
21+
* @return a `HarnessPredicate` configured with the given options.
22+
*/
23+
static with(options: OptionHarnessFilters = {}) {
24+
return new HarnessPredicate(MatOptionHarness, options)
25+
.addOption('text', options.text,
26+
async (harness, title) =>
27+
HarnessPredicate.stringMatches(await harness.getText(), title))
28+
.addOption('isSelected', options.isSelected,
29+
async (harness, isSelected) => await harness.isSelected() === isSelected);
30+
31+
}
32+
33+
/** Clicks the option. */
34+
async click(): Promise<void> {
35+
return (await this.host()).click();
36+
}
37+
38+
/** Gets the option's label text. */
39+
async getText(): Promise<string> {
40+
return (await this.host()).text();
41+
}
42+
43+
/** Gets whether the option is disabled. */
44+
async isDisabled(): Promise<boolean> {
45+
return (await this.host()).hasClass('mdc-list-item--disabled');
46+
}
47+
48+
/** Gets whether the option is selected. */
49+
async isSelected(): Promise<boolean> {
50+
return (await this.host()).hasClass('mdc-list-item--selected');
51+
}
52+
53+
/** Gets whether the option is active. */
54+
async isActive(): Promise<boolean> {
55+
return (await this.host()).hasClass('mat-mdc-option-active');
56+
}
57+
58+
/** Gets whether the option is in multiple selection mode. */
59+
async isMultiple(): Promise<boolean> {
60+
return (await this.host()).hasClass('mat-mdc-option-multiple');
61+
}
62+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 './option-harness';
10+
export * from './option-harness-filters';
11+
export * from './optgroup-harness';
12+
export * from './optgroup-harness-filters';
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {MatOptionModule} from '@angular/material/core';
1+
import {MatOptionModule, MatOption} from '@angular/material/core';
22
import {runHarnessTests} from './option-shared.spec';
33
import {MatOptionHarness} from './option-harness';
44

55
describe('Non-MDC-based MatOptionHarness', () => {
6-
runHarnessTests(MatOptionModule, MatOptionHarness);
6+
runHarnessTests(MatOptionModule, MatOptionHarness, MatOption);
77
});

src/material/core/testing/option-shared.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {MatOptionHarness} from './option-harness';
1212

1313
/** Shared tests to run on both the original and MDC-based options. */
1414
export function runHarnessTests(
15-
optionModule: typeof MatOptionModule, optionHarness: typeof MatOptionHarness) {
15+
optionModule: typeof MatOptionModule,
16+
optionHarness: typeof MatOptionHarness,
17+
optionComponent: typeof MatOption) {
1618
let fixture: ComponentFixture<OptionHarnessTest>;
1719
let loader: HarnessLoader;
1820

@@ -102,21 +104,19 @@ export function runHarnessTests(
102104

103105
expect(await option.isMultiple()).toBe(true);
104106
});
105-
}
106-
107107

108-
@Component({
109-
providers: [{
110-
provide: MAT_OPTION_PARENT_COMPONENT,
111-
useExisting: OptionHarnessTest
112-
}],
113-
template: `
114-
<mat-option>Plain option</mat-option>
115-
<mat-option disabled>Disabled option</mat-option>
116-
`
117-
})
118-
class OptionHarnessTest implements MatOptionParentComponent {
119-
@ViewChildren(MatOption) options: QueryList<MatOption>;
120-
multiple = false;
108+
@Component({
109+
providers: [{
110+
provide: MAT_OPTION_PARENT_COMPONENT,
111+
useExisting: OptionHarnessTest
112+
}],
113+
template: `
114+
<mat-option>Plain option</mat-option>
115+
<mat-option disabled>Disabled option</mat-option>
116+
`
117+
})
118+
class OptionHarnessTest implements MatOptionParentComponent {
119+
@ViewChildren(optionComponent) options: QueryList<{setActiveStyles(): void}>;
120+
multiple = false;
121+
}
121122
}
122-

0 commit comments

Comments
 (0)