@angular/material
Version:
Angular Material
151 lines (145 loc) • 5.66 kB
JavaScript
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
class _MatCheckboxHarnessBase extends ComponentHarness {
/** Whether the checkbox is checked. */
async isChecked() {
const checked = (await this._input()).getProperty('checked');
return coerceBooleanProperty(await checked);
}
/** Whether the checkbox is in an indeterminate state. */
async isIndeterminate() {
const indeterminate = (await this._input()).getProperty('indeterminate');
return coerceBooleanProperty(await indeterminate);
}
/** Whether the checkbox is disabled. */
async isDisabled() {
const disabled = (await this._input()).getAttribute('disabled');
return coerceBooleanProperty(await disabled);
}
/** Whether the checkbox is required. */
async isRequired() {
const required = (await this._input()).getProperty('required');
return coerceBooleanProperty(await required);
}
/** Whether the checkbox is valid. */
async isValid() {
const invalid = (await this.host()).hasClass('ng-invalid');
return !(await invalid);
}
/** Gets the checkbox's name. */
async getName() {
return (await this._input()).getAttribute('name');
}
/** Gets the checkbox's value. */
async getValue() {
return (await this._input()).getProperty('value');
}
/** Gets the checkbox's aria-label. */
async getAriaLabel() {
return (await this._input()).getAttribute('aria-label');
}
/** Gets the checkbox's aria-labelledby. */
async getAriaLabelledby() {
return (await this._input()).getAttribute('aria-labelledby');
}
/** Gets the checkbox's label text. */
async getLabelText() {
return (await this._label()).text();
}
/** Focuses the checkbox. */
async focus() {
return (await this._input()).focus();
}
/** Blurs the checkbox. */
async blur() {
return (await this._input()).blur();
}
/** Whether the checkbox is focused. */
async isFocused() {
return (await this._input()).isFocused();
}
/**
* Puts the checkbox in a checked state by toggling it if it is currently unchecked, or doing
* nothing if it is already checked.
*
* Note: This attempts to check the checkbox as a user would, by clicking it. Therefore if you
* are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
* might not have the expected result.
*/
async check() {
if (!(await this.isChecked())) {
await this.toggle();
}
}
/**
* Puts the checkbox in an unchecked state by toggling it if it is currently checked, or doing
* nothing if it is already unchecked.
*
* Note: This attempts to uncheck the checkbox as a user would, by clicking it. Therefore if you
* are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
* might not have the expected result.
*/
async uncheck() {
if (await this.isChecked()) {
await this.toggle();
}
}
}
/** Harness for interacting with a standard mat-checkbox in tests. */
class MatCheckboxHarness extends _MatCheckboxHarnessBase {
constructor() {
super(...arguments);
this._input = this.locatorFor('input');
this._label = this.locatorFor('.mat-checkbox-label');
this._inputContainer = this.locatorFor('.mat-checkbox-inner-container');
}
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatCheckboxHarness` that meets
* certain criteria.
* @param options Options for filtering which checkbox instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return (new HarnessPredicate(MatCheckboxHarness, options)
.addOption('label', options.label, (harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
// We want to provide a filter option for "name" because the name of the checkbox is
// only set on the underlying input. This means that it's not possible for developers
// to retrieve the harness of a specific checkbox with name through a CSS selector.
.addOption('name', options.name, async (harness, name) => (await harness.getName()) === name));
}
async toggle() {
return (await this._inputContainer()).click();
}
}
/** The selector for the host element of a `MatCheckbox` instance. */
MatCheckboxHarness.hostSelector = '.mat-checkbox';
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export { MatCheckboxHarness, _MatCheckboxHarnessBase };
//# sourceMappingURL=testing.mjs.map