[6a3a178] | 1 | import { __awaiter } from 'tslib';
|
---|
| 2 | import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
---|
| 3 | import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @license
|
---|
| 7 | * Copyright Google LLC All Rights Reserved.
|
---|
| 8 | *
|
---|
| 9 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 10 | * found in the LICENSE file at https://angular.io/license
|
---|
| 11 | */
|
---|
| 12 | class _MatRadioGroupHarnessBase extends ComponentHarness {
|
---|
| 13 | /** Gets the name of the radio-group. */
|
---|
| 14 | getName() {
|
---|
| 15 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 16 | const hostName = yield this._getGroupNameFromHost();
|
---|
| 17 | // It's not possible to always determine the "name" of a radio-group by reading
|
---|
| 18 | // the attribute. This is because the radio-group does not set the "name" as an
|
---|
| 19 | // element attribute if the "name" value is set through a binding.
|
---|
| 20 | if (hostName !== null) {
|
---|
| 21 | return hostName;
|
---|
| 22 | }
|
---|
| 23 | // In case we couldn't determine the "name" of a radio-group by reading the
|
---|
| 24 | // "name" attribute, we try to determine the "name" of the group by going
|
---|
| 25 | // through all radio buttons.
|
---|
| 26 | const radioNames = yield this._getNamesFromRadioButtons();
|
---|
| 27 | if (!radioNames.length) {
|
---|
| 28 | return null;
|
---|
| 29 | }
|
---|
| 30 | if (!this._checkRadioNamesInGroupEqual(radioNames)) {
|
---|
| 31 | throw Error('Radio buttons in radio-group have mismatching names.');
|
---|
| 32 | }
|
---|
| 33 | return radioNames[0];
|
---|
| 34 | });
|
---|
| 35 | }
|
---|
| 36 | /** Gets the id of the radio-group. */
|
---|
| 37 | getId() {
|
---|
| 38 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 39 | return (yield this.host()).getProperty('id');
|
---|
| 40 | });
|
---|
| 41 | }
|
---|
| 42 | /** Gets the checked radio-button in a radio-group. */
|
---|
| 43 | getCheckedRadioButton() {
|
---|
| 44 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 45 | for (let radioButton of yield this.getRadioButtons()) {
|
---|
| 46 | if (yield radioButton.isChecked()) {
|
---|
| 47 | return radioButton;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | return null;
|
---|
| 51 | });
|
---|
| 52 | }
|
---|
| 53 | /** Gets the checked value of the radio-group. */
|
---|
| 54 | getCheckedValue() {
|
---|
| 55 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 56 | const checkedRadio = yield this.getCheckedRadioButton();
|
---|
| 57 | if (!checkedRadio) {
|
---|
| 58 | return null;
|
---|
| 59 | }
|
---|
| 60 | return checkedRadio.getValue();
|
---|
| 61 | });
|
---|
| 62 | }
|
---|
| 63 | /**
|
---|
| 64 | * Gets a list of radio buttons which are part of the radio-group.
|
---|
| 65 | * @param filter Optionally filters which radio buttons are included.
|
---|
| 66 | */
|
---|
| 67 | getRadioButtons(filter) {
|
---|
| 68 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 69 | return this.locatorForAll(this._buttonClass.with(filter))();
|
---|
| 70 | });
|
---|
| 71 | }
|
---|
| 72 | /**
|
---|
| 73 | * Checks a radio button in this group.
|
---|
| 74 | * @param filter An optional filter to apply to the child radio buttons. The first tab matching
|
---|
| 75 | * the filter will be selected.
|
---|
| 76 | */
|
---|
| 77 | checkRadioButton(filter) {
|
---|
| 78 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 79 | const radioButtons = yield this.getRadioButtons(filter);
|
---|
| 80 | if (!radioButtons.length) {
|
---|
| 81 | throw Error(`Could not find radio button matching ${JSON.stringify(filter)}`);
|
---|
| 82 | }
|
---|
| 83 | return radioButtons[0].check();
|
---|
| 84 | });
|
---|
| 85 | }
|
---|
| 86 | /** Gets the name attribute of the host element. */
|
---|
| 87 | _getGroupNameFromHost() {
|
---|
| 88 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 89 | return (yield this.host()).getAttribute('name');
|
---|
| 90 | });
|
---|
| 91 | }
|
---|
| 92 | /** Gets a list of the name attributes of all child radio buttons. */
|
---|
| 93 | _getNamesFromRadioButtons() {
|
---|
| 94 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 95 | const groupNames = [];
|
---|
| 96 | for (let radio of yield this.getRadioButtons()) {
|
---|
| 97 | const radioName = yield radio.getName();
|
---|
| 98 | if (radioName !== null) {
|
---|
| 99 | groupNames.push(radioName);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | return groupNames;
|
---|
| 103 | });
|
---|
| 104 | }
|
---|
| 105 | /** Checks if the specified radio names are all equal. */
|
---|
| 106 | _checkRadioNamesInGroupEqual(radioNames) {
|
---|
| 107 | let groupName = null;
|
---|
| 108 | for (let radioName of radioNames) {
|
---|
| 109 | if (groupName === null) {
|
---|
| 110 | groupName = radioName;
|
---|
| 111 | }
|
---|
| 112 | else if (groupName !== radioName) {
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | return true;
|
---|
| 117 | }
|
---|
| 118 | /**
|
---|
| 119 | * Checks if a radio-group harness has the given name. Throws if a radio-group with
|
---|
| 120 | * matching name could be found but has mismatching radio-button names.
|
---|
| 121 | */
|
---|
| 122 | static _checkRadioGroupName(harness, name) {
|
---|
| 123 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 124 | // Check if there is a radio-group which has the "name" attribute set
|
---|
| 125 | // to the expected group name. It's not possible to always determine
|
---|
| 126 | // the "name" of a radio-group by reading the attribute. This is because
|
---|
| 127 | // the radio-group does not set the "name" as an element attribute if the
|
---|
| 128 | // "name" value is set through a binding.
|
---|
| 129 | if ((yield harness._getGroupNameFromHost()) === name) {
|
---|
| 130 | return true;
|
---|
| 131 | }
|
---|
| 132 | // Check if there is a group with radio-buttons that all have the same
|
---|
| 133 | // expected name. This implies that the group has the given name. It's
|
---|
| 134 | // not possible to always determine the name of a radio-group through
|
---|
| 135 | // the attribute because there is
|
---|
| 136 | const radioNames = yield harness._getNamesFromRadioButtons();
|
---|
| 137 | if (radioNames.indexOf(name) === -1) {
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 | if (!harness._checkRadioNamesInGroupEqual(radioNames)) {
|
---|
| 141 | throw Error(`The locator found a radio-group with name "${name}", but some ` +
|
---|
| 142 | `radio-button's within the group have mismatching names, which is invalid.`);
|
---|
| 143 | }
|
---|
| 144 | return true;
|
---|
| 145 | });
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | /** Harness for interacting with a standard mat-radio-group in tests. */
|
---|
| 149 | class MatRadioGroupHarness extends _MatRadioGroupHarnessBase {
|
---|
| 150 | constructor() {
|
---|
| 151 | super(...arguments);
|
---|
| 152 | this._buttonClass = MatRadioButtonHarness;
|
---|
| 153 | }
|
---|
| 154 | /**
|
---|
| 155 | * Gets a `HarnessPredicate` that can be used to search for a `MatRadioGroupHarness` that meets
|
---|
| 156 | * certain criteria.
|
---|
| 157 | * @param options Options for filtering which radio group instances are considered a match.
|
---|
| 158 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 159 | */
|
---|
| 160 | static with(options = {}) {
|
---|
| 161 | return new HarnessPredicate(MatRadioGroupHarness, options)
|
---|
| 162 | .addOption('name', options.name, this._checkRadioGroupName);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | /** The selector for the host element of a `MatRadioGroup` instance. */
|
---|
| 166 | MatRadioGroupHarness.hostSelector = '.mat-radio-group';
|
---|
| 167 | class _MatRadioButtonHarnessBase extends ComponentHarness {
|
---|
| 168 | constructor() {
|
---|
| 169 | super(...arguments);
|
---|
| 170 | this._input = this.locatorFor('input');
|
---|
| 171 | }
|
---|
| 172 | /** Whether the radio-button is checked. */
|
---|
| 173 | isChecked() {
|
---|
| 174 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 175 | const checked = (yield this._input()).getProperty('checked');
|
---|
| 176 | return coerceBooleanProperty(yield checked);
|
---|
| 177 | });
|
---|
| 178 | }
|
---|
| 179 | /** Whether the radio-button is disabled. */
|
---|
| 180 | isDisabled() {
|
---|
| 181 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 182 | const disabled = (yield this._input()).getAttribute('disabled');
|
---|
| 183 | return coerceBooleanProperty(yield disabled);
|
---|
| 184 | });
|
---|
| 185 | }
|
---|
| 186 | /** Whether the radio-button is required. */
|
---|
| 187 | isRequired() {
|
---|
| 188 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 189 | const required = (yield this._input()).getAttribute('required');
|
---|
| 190 | return coerceBooleanProperty(yield required);
|
---|
| 191 | });
|
---|
| 192 | }
|
---|
| 193 | /** Gets the radio-button's name. */
|
---|
| 194 | getName() {
|
---|
| 195 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 196 | return (yield this._input()).getAttribute('name');
|
---|
| 197 | });
|
---|
| 198 | }
|
---|
| 199 | /** Gets the radio-button's id. */
|
---|
| 200 | getId() {
|
---|
| 201 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 202 | return (yield this.host()).getProperty('id');
|
---|
| 203 | });
|
---|
| 204 | }
|
---|
| 205 | /**
|
---|
| 206 | * Gets the value of the radio-button. The radio-button value will be converted to a string.
|
---|
| 207 | *
|
---|
| 208 | * Note: This means that for radio-button's with an object as a value `[object Object]` is
|
---|
| 209 | * intentionally returned.
|
---|
| 210 | */
|
---|
| 211 | getValue() {
|
---|
| 212 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 213 | return (yield this._input()).getProperty('value');
|
---|
| 214 | });
|
---|
| 215 | }
|
---|
| 216 | /** Gets the radio-button's label text. */
|
---|
| 217 | getLabelText() {
|
---|
| 218 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 219 | return (yield this._textLabel()).text();
|
---|
| 220 | });
|
---|
| 221 | }
|
---|
| 222 | /** Focuses the radio-button. */
|
---|
| 223 | focus() {
|
---|
| 224 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 225 | return (yield this._input()).focus();
|
---|
| 226 | });
|
---|
| 227 | }
|
---|
| 228 | /** Blurs the radio-button. */
|
---|
| 229 | blur() {
|
---|
| 230 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 231 | return (yield this._input()).blur();
|
---|
| 232 | });
|
---|
| 233 | }
|
---|
| 234 | /** Whether the radio-button is focused. */
|
---|
| 235 | isFocused() {
|
---|
| 236 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 237 | return (yield this._input()).isFocused();
|
---|
| 238 | });
|
---|
| 239 | }
|
---|
| 240 | /**
|
---|
| 241 | * Puts the radio-button in a checked state by clicking it if it is currently unchecked,
|
---|
| 242 | * or doing nothing if it is already checked.
|
---|
| 243 | */
|
---|
| 244 | check() {
|
---|
| 245 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 246 | if (!(yield this.isChecked())) {
|
---|
| 247 | return (yield this._clickLabel()).click();
|
---|
| 248 | }
|
---|
| 249 | });
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | /** Harness for interacting with a standard mat-radio-button in tests. */
|
---|
| 253 | class MatRadioButtonHarness extends _MatRadioButtonHarnessBase {
|
---|
| 254 | constructor() {
|
---|
| 255 | super(...arguments);
|
---|
| 256 | this._textLabel = this.locatorFor('.mat-radio-label-content');
|
---|
| 257 | this._clickLabel = this.locatorFor('.mat-radio-label');
|
---|
| 258 | }
|
---|
| 259 | /**
|
---|
| 260 | * Gets a `HarnessPredicate` that can be used to search for a `MatRadioButtonHarness` that meets
|
---|
| 261 | * certain criteria.
|
---|
| 262 | * @param options Options for filtering which radio button instances are considered a match.
|
---|
| 263 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 264 | */
|
---|
| 265 | static with(options = {}) {
|
---|
| 266 | return new HarnessPredicate(MatRadioButtonHarness, options)
|
---|
| 267 | .addOption('label', options.label, (harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
|
---|
| 268 | .addOption('name', options.name, (harness, name) => __awaiter(this, void 0, void 0, function* () { return (yield harness.getName()) === name; }));
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | /** The selector for the host element of a `MatRadioButton` instance. */
|
---|
| 272 | MatRadioButtonHarness.hostSelector = '.mat-radio-button';
|
---|
| 273 |
|
---|
| 274 | /**
|
---|
| 275 | * @license
|
---|
| 276 | * Copyright Google LLC All Rights Reserved.
|
---|
| 277 | *
|
---|
| 278 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 279 | * found in the LICENSE file at https://angular.io/license
|
---|
| 280 | */
|
---|
| 281 |
|
---|
| 282 | /**
|
---|
| 283 | * @license
|
---|
| 284 | * Copyright Google LLC All Rights Reserved.
|
---|
| 285 | *
|
---|
| 286 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 287 | * found in the LICENSE file at https://angular.io/license
|
---|
| 288 | */
|
---|
| 289 |
|
---|
| 290 | /**
|
---|
| 291 | * @license
|
---|
| 292 | * Copyright Google LLC All Rights Reserved.
|
---|
| 293 | *
|
---|
| 294 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 295 | * found in the LICENSE file at https://angular.io/license
|
---|
| 296 | */
|
---|
| 297 |
|
---|
| 298 | export { MatRadioButtonHarness, MatRadioGroupHarness, _MatRadioButtonHarnessBase, _MatRadioGroupHarnessBase };
|
---|
| 299 | //# sourceMappingURL=testing.js.map
|
---|