UNPKG

6.7 kBJavaScriptView Raw
1import { __awaiter } from 'tslib';
2import { coerceBooleanProperty } from '@angular/cdk/coercion';
3import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
4
5class _MatCheckboxHarnessBase extends ComponentHarness {
6 /** Whether the checkbox is checked. */
7 isChecked() {
8 return __awaiter(this, void 0, void 0, function* () {
9 const checked = (yield this._input()).getProperty('checked');
10 return coerceBooleanProperty(yield checked);
11 });
12 }
13 /** Whether the checkbox is in an indeterminate state. */
14 isIndeterminate() {
15 return __awaiter(this, void 0, void 0, function* () {
16 const indeterminate = (yield this._input()).getProperty('indeterminate');
17 return coerceBooleanProperty(yield indeterminate);
18 });
19 }
20 /** Whether the checkbox is disabled. */
21 isDisabled() {
22 return __awaiter(this, void 0, void 0, function* () {
23 const disabled = (yield this._input()).getAttribute('disabled');
24 return coerceBooleanProperty(yield disabled);
25 });
26 }
27 /** Whether the checkbox is required. */
28 isRequired() {
29 return __awaiter(this, void 0, void 0, function* () {
30 const required = (yield this._input()).getProperty('required');
31 return coerceBooleanProperty(yield required);
32 });
33 }
34 /** Whether the checkbox is valid. */
35 isValid() {
36 return __awaiter(this, void 0, void 0, function* () {
37 const invalid = (yield this.host()).hasClass('ng-invalid');
38 return !(yield invalid);
39 });
40 }
41 /** Gets the checkbox's name. */
42 getName() {
43 return __awaiter(this, void 0, void 0, function* () {
44 return (yield this._input()).getAttribute('name');
45 });
46 }
47 /** Gets the checkbox's value. */
48 getValue() {
49 return __awaiter(this, void 0, void 0, function* () {
50 return (yield this._input()).getProperty('value');
51 });
52 }
53 /** Gets the checkbox's aria-label. */
54 getAriaLabel() {
55 return __awaiter(this, void 0, void 0, function* () {
56 return (yield this._input()).getAttribute('aria-label');
57 });
58 }
59 /** Gets the checkbox's aria-labelledby. */
60 getAriaLabelledby() {
61 return __awaiter(this, void 0, void 0, function* () {
62 return (yield this._input()).getAttribute('aria-labelledby');
63 });
64 }
65 /** Gets the checkbox's label text. */
66 getLabelText() {
67 return __awaiter(this, void 0, void 0, function* () {
68 return (yield this._label()).text();
69 });
70 }
71 /** Focuses the checkbox. */
72 focus() {
73 return __awaiter(this, void 0, void 0, function* () {
74 return (yield this._input()).focus();
75 });
76 }
77 /** Blurs the checkbox. */
78 blur() {
79 return __awaiter(this, void 0, void 0, function* () {
80 return (yield this._input()).blur();
81 });
82 }
83 /** Whether the checkbox is focused. */
84 isFocused() {
85 return __awaiter(this, void 0, void 0, function* () {
86 return (yield this._input()).isFocused();
87 });
88 }
89 /**
90 * Puts the checkbox in a checked state by toggling it if it is currently unchecked, or doing
91 * nothing if it is already checked.
92 *
93 * Note: This attempts to check the checkbox as a user would, by clicking it. Therefore if you
94 * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
95 * might not have the expected result.
96 */
97 check() {
98 return __awaiter(this, void 0, void 0, function* () {
99 if (!(yield this.isChecked())) {
100 yield this.toggle();
101 }
102 });
103 }
104 /**
105 * Puts the checkbox in an unchecked state by toggling it if it is currently checked, or doing
106 * nothing if it is already unchecked.
107 *
108 * Note: This attempts to uncheck the checkbox as a user would, by clicking it. Therefore if you
109 * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
110 * might not have the expected result.
111 */
112 uncheck() {
113 return __awaiter(this, void 0, void 0, function* () {
114 if (yield this.isChecked()) {
115 yield this.toggle();
116 }
117 });
118 }
119}
120/** Harness for interacting with a standard mat-checkbox in tests. */
121class MatCheckboxHarness extends _MatCheckboxHarnessBase {
122 constructor() {
123 super(...arguments);
124 this._input = this.locatorFor('input');
125 this._label = this.locatorFor('.mat-checkbox-label');
126 this._inputContainer = this.locatorFor('.mat-checkbox-inner-container');
127 }
128 /**
129 * Gets a `HarnessPredicate` that can be used to search for a `MatCheckboxHarness` that meets
130 * certain criteria.
131 * @param options Options for filtering which checkbox instances are considered a match.
132 * @return a `HarnessPredicate` configured with the given options.
133 */
134 static with(options = {}) {
135 return (new HarnessPredicate(MatCheckboxHarness, options)
136 .addOption('label', options.label, (harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
137 // We want to provide a filter option for "name" because the name of the checkbox is
138 // only set on the underlying input. This means that it's not possible for developers
139 // to retrieve the harness of a specific checkbox with name through a CSS selector.
140 .addOption('name', options.name, (harness, name) => __awaiter(this, void 0, void 0, function* () { return (yield harness.getName()) === name; }))
141 .addOption('checked', options.checked, (harness, checked) => __awaiter(this, void 0, void 0, function* () { return (yield harness.isChecked()) == checked; })));
142 }
143 toggle() {
144 return __awaiter(this, void 0, void 0, function* () {
145 return (yield this._inputContainer()).click();
146 });
147 }
148}
149/** The selector for the host element of a `MatCheckbox` instance. */
150MatCheckboxHarness.hostSelector = '.mat-checkbox';
151
152/**
153 * @license
154 * Copyright Google LLC All Rights Reserved.
155 *
156 * Use of this source code is governed by an MIT-style license that can be
157 * found in the LICENSE file at https://angular.io/license
158 */
159
160/**
161 * @license
162 * Copyright Google LLC All Rights Reserved.
163 *
164 * Use of this source code is governed by an MIT-style license that can be
165 * found in the LICENSE file at https://angular.io/license
166 */
167
168export { MatCheckboxHarness, _MatCheckboxHarnessBase };
169//# sourceMappingURL=testing.mjs.map