@angular/material
Version:
Angular Material
124 lines (121 loc) • 5.27 kB
JavaScript
import { HarnessPredicate, parallel } from '@angular/cdk/testing';
import { M as MatFormFieldControlHarness } from './form-field-control-harness-adcc773d.mjs';
import { M as MatOptionHarness } from './option-harness-4cd4ee4e.mjs';
import { M as MatOptgroupHarness } from './optgroup-harness-d961ca3f.mjs';
/** Harness for interacting with a mat-select in tests. */
class MatSelectHarness extends MatFormFieldControlHarness {
static hostSelector = '.mat-mdc-select';
_prefix = 'mat-mdc';
_optionClass = MatOptionHarness;
_optionGroupClass = MatOptgroupHarness;
_documentRootLocator = this.documentRootLocatorFactory();
_backdrop = this._documentRootLocator.locatorFor('.cdk-overlay-backdrop');
/**
* Gets a `HarnessPredicate` that can be used to search for a select with specific attributes.
* @param options Options for filtering which select instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(this, options).addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}
/** Gets a boolean promise indicating if the select is disabled. */
async isDisabled() {
return (await this.host()).hasClass(`${this._prefix}-select-disabled`);
}
/** Gets a boolean promise indicating if the select is valid. */
async isValid() {
return !(await (await this.host()).hasClass('ng-invalid'));
}
/** Gets a boolean promise indicating if the select is required. */
async isRequired() {
return (await this.host()).hasClass(`${this._prefix}-select-required`);
}
/** Gets a boolean promise indicating if the select is empty (no value is selected). */
async isEmpty() {
return (await this.host()).hasClass(`${this._prefix}-select-empty`);
}
/** Gets a boolean promise indicating if the select is in multi-selection mode. */
async isMultiple() {
return (await this.host()).hasClass(`${this._prefix}-select-multiple`);
}
/** Gets a promise for the select's value text. */
async getValueText() {
const value = await this.locatorFor(`.${this._prefix}-select-value`)();
return value.text();
}
/** Focuses the select and returns a void promise that indicates when the action is complete. */
async focus() {
return (await this.host()).focus();
}
/** Blurs the select and returns a void promise that indicates when the action is complete. */
async blur() {
return (await this.host()).blur();
}
/** Whether the select is focused. */
async isFocused() {
return (await this.host()).isFocused();
}
/** Gets the options inside the select panel. */
async getOptions(filter) {
return this._documentRootLocator.locatorForAll(this._optionClass.with({
...(filter || {}),
ancestor: await this._getPanelSelector(),
}))();
}
/** Gets the groups of options inside the panel. */
async getOptionGroups(filter) {
return this._documentRootLocator.locatorForAll(this._optionGroupClass.with({
...(filter || {}),
ancestor: await this._getPanelSelector(),
}))();
}
/** Gets whether the select is open. */
async isOpen() {
return !!(await this._documentRootLocator.locatorForOptional(await this._getPanelSelector())());
}
/** Opens the select's panel. */
async open() {
if (!(await this.isOpen())) {
const trigger = await this.locatorFor(`.${this._prefix}-select-trigger`)();
return trigger.click();
}
}
/**
* Clicks the options that match the passed-in filter. If the select is in multi-selection
* mode all options will be clicked, otherwise the harness will pick the first matching option.
*/
async clickOptions(filter) {
await this.open();
const [isMultiple, options] = await parallel(() => [
this.isMultiple(),
this.getOptions(filter),
]);
if (options.length === 0) {
throw Error('Select does not have options matching the specified filter');
}
if (isMultiple) {
await parallel(() => options.map(option => option.click()));
}
else {
await options[0].click();
}
}
/** Closes the select's panel. */
async close() {
if (await this.isOpen()) {
// This is the most consistent way that works both in both single and multi-select modes,
// but it assumes that only one overlay is open at a time. We should be able to make it
// a bit more precise after #16645 where we can dispatch an ESCAPE press to the host instead.
return (await this._backdrop()).click();
}
}
/** Gets the selector that should be used to find this select's panel. */
async _getPanelSelector() {
const id = await (await this.host()).getAttribute('id');
return `#${id}-panel`;
}
}
export { MatSelectHarness as M };
//# sourceMappingURL=select-harness-9656d727.mjs.map