Skip to content

Commit 4a61a64

Browse files
committed
feat(material-experimental/checkbox): Add test harnesses for both
version of mat-checkbox
1 parent 6a7fc81 commit 4a61a64

File tree

6 files changed

+198
-1
lines changed

6 files changed

+198
-1
lines changed

src/cdk-experimental/testing/protractor/protractor-element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ export class ProtractorElement implements TestElement {
5050
async getAttribute(name: string): Promise<string|null> {
5151
return this.element.getAttribute(name);
5252
}
53+
54+
async hasClass(name: string): Promise<boolean> {
55+
const classes = (await this.getAttribute('class')) || '';
56+
return new Set(classes.split(/\s+/).filter(c => c)).has(name);
57+
}
5358
}

src/cdk-experimental/testing/test-element.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ export interface TestElement {
4343
* falls back to reading the property.
4444
*/
4545
getAttribute(name: string): Promise<string | null>;
46+
47+
/** Checks whether the element has the given class. */
48+
hasClass(name: string): Promise<boolean>;
4649
}

src/cdk-experimental/testing/testbed/unit-test-element.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ export class UnitTestElement implements TestElement {
100100
}
101101
return value;
102102
}
103+
104+
async hasClass(name: string): Promise<boolean> {
105+
await this._stabilize();
106+
return this.element.classList.contains(name);
107+
}
103108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ng_module(
88
name = "mdc-checkbox",
99
srcs = glob(
1010
["**/*.ts"],
11-
exclude = ["**/*.spec.ts"],
11+
exclude = ["**/*.spec.ts", "harnesses/**"],
1212
),
1313
assets = [":checkbox_scss"] + glob(["**/*.html"]),
1414
module_name = "@angular/material-experimental/mdc-checkbox",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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-experimental/testing';
10+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11+
12+
export class MatCheckboxHarness extends ComponentHarness {
13+
static hostSelector = 'mat-checkbox';
14+
15+
static with(options: {label: string | RegExp}): HarnessPredicate<MatCheckboxHarness> {
16+
return new HarnessPredicate(MatCheckboxHarness)
17+
.addOption('label', options.label,
18+
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label));
19+
}
20+
21+
private _label = this.locatorFor('.mat-checkbox-label');
22+
private _input = this.locatorFor('input');
23+
24+
async isChecked(): Promise<boolean> {
25+
const checked = (await this._input()).getAttribute('checked');
26+
return coerceBooleanProperty(await checked);
27+
}
28+
29+
async isIndeterminate(): Promise<boolean> {
30+
const indeterminate = (await this._input()).getAttribute('indeterminate');
31+
return coerceBooleanProperty(await indeterminate);
32+
}
33+
34+
async isDisabled(): Promise<boolean> {
35+
const disabled = (await this._input()).getAttribute('disabled');
36+
return coerceBooleanProperty(await disabled);
37+
}
38+
39+
async isRequired(): Promise<boolean> {
40+
const required = (await this._input()).getAttribute('required');
41+
return coerceBooleanProperty(await required);
42+
}
43+
44+
async isValid(): Promise<boolean> {
45+
const invalid = (await this.host()).hasClass('ng-invalid');
46+
return !(await invalid);
47+
}
48+
49+
async getName(): Promise<string | null> {
50+
return (await this._input()).getAttribute('name');
51+
}
52+
53+
async getValue(): Promise<string | null> {
54+
return (await this._input()).getAttribute('value');
55+
}
56+
57+
async getAriaLabel(): Promise<string | null> {
58+
return (await this._input()).getAttribute('aria-label');
59+
}
60+
61+
async getAriaLabelledby(): Promise<string | null> {
62+
return (await this._input()).getAttribute('aria-labelledby');
63+
}
64+
65+
async getLabelText(): Promise<string> {
66+
return (await this._label()).text();
67+
}
68+
69+
async foucs(): Promise<void> {
70+
return (await this._input()).focus();
71+
}
72+
73+
async blur(): Promise<void> {
74+
return (await this._input()).blur();
75+
}
76+
77+
async toggle(): Promise<void> {
78+
return (await this._input()).click();
79+
}
80+
81+
async check(): Promise<void> {
82+
if (!(await this.isChecked())) {
83+
await this.toggle();
84+
}
85+
}
86+
87+
async uncheck(): Promise<void> {
88+
if (await this.isChecked()) {
89+
await this.toggle();
90+
}
91+
}
92+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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-experimental/testing';
10+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
11+
12+
export class MatCheckboxHarness extends ComponentHarness {
13+
static hostSelector = 'mat-checkbox';
14+
15+
static with(options: {label: string | RegExp}): HarnessPredicate<MatCheckboxHarness> {
16+
return new HarnessPredicate(MatCheckboxHarness)
17+
.addOption('label', options.label,
18+
(harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label));
19+
}
20+
21+
private _label = this.locatorFor('label');
22+
private _input = this.locatorFor('input');
23+
24+
async isChecked(): Promise<boolean> {
25+
const checked = (await this._input()).getAttribute('checked');
26+
return coerceBooleanProperty(await checked);
27+
}
28+
29+
async isIndeterminate(): Promise<boolean> {
30+
const indeterminate = (await this._input()).getAttribute('indeterminate');
31+
return coerceBooleanProperty(await indeterminate);
32+
}
33+
34+
async isDisabled(): Promise<boolean> {
35+
const disabled = (await this._input()).getAttribute('disabled');
36+
return coerceBooleanProperty(await disabled);
37+
}
38+
39+
async isRequired(): Promise<boolean> {
40+
const required = (await this._input()).getAttribute('required');
41+
return coerceBooleanProperty(await required);
42+
}
43+
44+
async isValid(): Promise<boolean> {
45+
const invalid = (await this.host()).hasClass('ng-invalid');
46+
return !(await invalid);
47+
}
48+
49+
async getName(): Promise<string | null> {
50+
return (await this._input()).getAttribute('name');
51+
}
52+
53+
async getValue(): Promise<string | null> {
54+
return (await this._input()).getAttribute('value');
55+
}
56+
57+
async getAriaLabel(): Promise<string | null> {
58+
return (await this._input()).getAttribute('aria-label');
59+
}
60+
61+
async getAriaLabelledby(): Promise<string | null> {
62+
return (await this._input()).getAttribute('aria-labelledby');
63+
}
64+
65+
async getLabelText(): Promise<string> {
66+
return (await this._label()).text();
67+
}
68+
69+
async foucs(): Promise<void> {
70+
return (await this._input()).focus();
71+
}
72+
73+
async blur(): Promise<void> {
74+
return (await this._input()).blur();
75+
}
76+
77+
async toggle(): Promise<void> {
78+
return (await this._input()).click();
79+
}
80+
81+
async check(): Promise<void> {
82+
if (!(await this.isChecked())) {
83+
await this.toggle();
84+
}
85+
}
86+
87+
async uncheck(): Promise<void> {
88+
if (await this.isChecked()) {
89+
await this.toggle();
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)