UNPKG

@angular/material

Version:
1 lines 10.5 kB
{"version":3,"file":"select-testing.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/material/select/testing/select-harness.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {ComponentHarnessConstructor, HarnessPredicate, parallel} from '@angular/cdk/testing';\nimport {\n MatOptionHarness,\n MatOptgroupHarness,\n OptionHarnessFilters,\n OptgroupHarnessFilters,\n} from '@angular/material/core/testing';\nimport {MatFormFieldControlHarnessBase} from '@angular/material/form-field/testing/control';\nimport {SelectHarnessFilters} from './select-harness-filters';\n\n/** Harness for interacting with a mat-select in tests. */\nexport class MatSelectHarness extends MatFormFieldControlHarnessBase {\n static hostSelector = '.mat-mdc-select';\n private _prefix = 'mat-mdc';\n private _optionClass = MatOptionHarness;\n private _optionGroupClass = MatOptgroupHarness;\n private _documentRootLocator = this.documentRootLocatorFactory();\n private _backdrop = this._documentRootLocator.locatorFor('.cdk-overlay-backdrop');\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a select with specific attributes.\n * @param options Options for filtering which select instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatSelectHarness>(\n this: ComponentHarnessConstructor<T>,\n options: SelectHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options)\n .addOption('disabled', options.disabled, async (harness, disabled) => {\n return (await harness.isDisabled()) === disabled;\n })\n .addOption('label', options.label, (harness, label) => {\n return HarnessPredicate.stringMatches(harness.getLabel(), label);\n });\n }\n\n /** Gets a boolean promise indicating if the select is disabled. */\n async isDisabled(): Promise<boolean> {\n return (await this.host()).hasClass(`${this._prefix}-select-disabled`);\n }\n\n /** Gets a boolean promise indicating if the select is valid. */\n async isValid(): Promise<boolean> {\n return !(await (await this.host()).hasClass('ng-invalid'));\n }\n\n /** Gets a boolean promise indicating if the select is required. */\n async isRequired(): Promise<boolean> {\n return (await this.host()).hasClass(`${this._prefix}-select-required`);\n }\n\n /** Gets a boolean promise indicating if the select is empty (no value is selected). */\n async isEmpty(): Promise<boolean> {\n return (await this.host()).hasClass(`${this._prefix}-select-empty`);\n }\n\n /** Gets a boolean promise indicating if the select is in multi-selection mode. */\n async isMultiple(): Promise<boolean> {\n return (await this.host()).hasClass(`${this._prefix}-select-multiple`);\n }\n\n /** Gets a promise for the select's value text. */\n async getValueText(): Promise<string> {\n const value = await this.locatorFor(`.${this._prefix}-select-value`)();\n return value.text();\n }\n\n /** Focuses the select and returns a void promise that indicates when the action is complete. */\n async focus(): Promise<void> {\n return (await this.host()).focus();\n }\n\n /** Blurs the select and returns a void promise that indicates when the action is complete. */\n async blur(): Promise<void> {\n return (await this.host()).blur();\n }\n\n /** Whether the select is focused. */\n async isFocused(): Promise<boolean> {\n return (await this.host()).isFocused();\n }\n\n /** Gets the options inside the select panel. */\n async getOptions(filter?: Omit<OptionHarnessFilters, 'ancestor'>): Promise<MatOptionHarness[]> {\n return this._documentRootLocator.locatorForAll(\n this._optionClass.with({\n ...(filter || {}),\n ancestor: await this._getPanelSelector(),\n } as OptionHarnessFilters),\n )();\n }\n\n /** Gets the groups of options inside the panel. */\n async getOptionGroups(\n filter?: Omit<OptgroupHarnessFilters, 'ancestor'>,\n ): Promise<MatOptgroupHarness[]> {\n return this._documentRootLocator.locatorForAll(\n this._optionGroupClass.with({\n ...(filter || {}),\n ancestor: await this._getPanelSelector(),\n } as OptgroupHarnessFilters),\n )() as Promise<MatOptgroupHarness[]>;\n }\n\n /** Gets whether the select is open. */\n async isOpen(): Promise<boolean> {\n return !!(await this._documentRootLocator.locatorForOptional(await this._getPanelSelector())());\n }\n\n /** Opens the select's panel. */\n async open(): Promise<void> {\n if (!(await this.isOpen())) {\n const trigger = await this.locatorFor(`.${this._prefix}-select-trigger`)();\n return trigger.click();\n }\n }\n\n /**\n * Clicks the options that match the passed-in filter. If the select is in multi-selection\n * mode all options will be clicked, otherwise the harness will pick the first matching option.\n */\n async clickOptions(filter?: OptionHarnessFilters): Promise<void> {\n await this.open();\n\n const [isMultiple, options] = await parallel(() => [\n this.isMultiple(),\n this.getOptions(filter),\n ]);\n\n if (options.length === 0) {\n throw Error('Select does not have options matching the specified filter');\n }\n\n if (isMultiple) {\n await parallel(() => options.map(option => option.click()));\n } else {\n await options[0].click();\n }\n }\n\n /** Closes the select's panel. */\n async close(): Promise<void> {\n if (await this.isOpen()) {\n // This is the most consistent way that works both in both single and multi-select modes,\n // but it assumes that only one overlay is open at a time. We should be able to make it\n // a bit more precise after #16645 where we can dispatch an ESCAPE press to the host instead.\n return (await this._backdrop()).click();\n }\n }\n\n /** Gets the selector that should be used to find this select's panel. */\n private async _getPanelSelector(): Promise<string> {\n const id = await (await this.host()).getAttribute('id');\n return `#${id}-panel`;\n }\n}\n"],"names":["MatSelectHarness","MatFormFieldControlHarnessBase","hostSelector","_prefix","_optionClass","MatOptionHarness","_optionGroupClass","MatOptgroupHarness","_documentRootLocator","documentRootLocatorFactory","_backdrop","locatorFor","with","options","HarnessPredicate","addOption","disabled","harness","isDisabled","label","stringMatches","getLabel","host","hasClass","isValid","isRequired","isEmpty","isMultiple","getValueText","value","text","focus","blur","isFocused","getOptions","filter","locatorForAll","ancestor","_getPanelSelector","getOptionGroups","isOpen","locatorForOptional","open","trigger","click","clickOptions","parallel","length","Error","map","option","close","id","getAttribute"],"mappings":";;;;AAmBM,MAAOA,gBAAiB,SAAQC,8BAA8B,CAAA;EAClE,OAAOC,YAAY,GAAG,iBAAiB;AAC/BC,EAAAA,OAAO,GAAG,SAAS;AACnBC,EAAAA,YAAY,GAAGC,gBAAgB;AAC/BC,EAAAA,iBAAiB,GAAGC,kBAAkB;AACtCC,EAAAA,oBAAoB,GAAG,IAAI,CAACC,0BAA0B,EAAE;EACxDC,SAAS,GAAG,IAAI,CAACF,oBAAoB,CAACG,UAAU,CAAC,uBAAuB,CAAC;AAOjF,EAA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