import { parallel, HarnessPredicate } from '@angular/cdk/testing'; import { MatOptionHarness, MatOptgroupHarness } from '@angular/material/core/testing'; import { MatFormFieldControlHarness } from '@angular/material/form-field/testing/control'; class _MatSelectHarnessBase extends MatFormFieldControlHarness { constructor() { super(...arguments); this._documentRootLocator = this.documentRootLocatorFactory(); this._backdrop = this._documentRootLocator.locatorFor('.cdk-overlay-backdrop'); } /** 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`; } } /** Harness for interacting with an MDC-based mat-select in tests. */ class MatSelectHarness extends _MatSelectHarnessBase { constructor() { super(...arguments); this._prefix = 'mat-mdc'; this._optionClass = MatOptionHarness; this._optionGroupClass = MatOptgroupHarness; } static { this.hostSelector = '.mat-mdc-select'; } /** * 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; }); } } export { MatSelectHarness, _MatSelectHarnessBase }; //# sourceMappingURL=testing.mjs.map