@angular/material
Version:
Angular Material
208 lines (202 loc) • 7.67 kB
JavaScript
import { ContentContainerComponentHarness, TestKey, HarnessPredicate } from '@angular/cdk/testing';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
/**
* @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 _MatMenuHarnessBase extends ContentContainerComponentHarness {
constructor() {
super(...arguments);
this._documentRootLocator = this.documentRootLocatorFactory();
}
// TODO: potentially extend MatLegacyButtonHarness
/** Whether the menu is disabled. */
async isDisabled() {
const disabled = (await this.host()).getAttribute('disabled');
return coerceBooleanProperty(await disabled);
}
/** Whether the menu is open. */
async isOpen() {
return !!(await this._getMenuPanel());
}
/** Gets the text of the menu's trigger element. */
async getTriggerText() {
return (await this.host()).text();
}
/** Focuses the menu. */
async focus() {
return (await this.host()).focus();
}
/** Blurs the menu. */
async blur() {
return (await this.host()).blur();
}
/** Whether the menu is focused. */
async isFocused() {
return (await this.host()).isFocused();
}
/** Opens the menu. */
async open() {
if (!(await this.isOpen())) {
return (await this.host()).click();
}
}
/** Closes the menu. */
async close() {
const panel = await this._getMenuPanel();
if (panel) {
return panel.sendKeys(TestKey.ESCAPE);
}
}
/**
* Gets a list of `MatMenuItemHarness` representing the items in the menu.
* @param filters Optionally filters which menu items are included.
*/
async getItems(filters) {
const panelId = await this._getPanelId();
if (panelId) {
return this._documentRootLocator.locatorForAll(this._itemClass.with({
...(filters || {}),
ancestor: `#${panelId}`,
}))();
}
return [];
}
/**
* Clicks an item in the menu, and optionally continues clicking items in subsequent sub-menus.
* @param itemFilter A filter used to represent which item in the menu should be clicked. The
* first matching menu item will be clicked.
* @param subItemFilters A list of filters representing the items to click in any subsequent
* sub-menus. The first item in the sub-menu matching the corresponding filter in
* `subItemFilters` will be clicked.
*/
async clickItem(itemFilter, ...subItemFilters) {
await this.open();
const items = await this.getItems(itemFilter);
if (!items.length) {
throw Error(`Could not find item matching ${JSON.stringify(itemFilter)}`);
}
if (!subItemFilters.length) {
return await items[0].click();
}
const menu = await items[0].getSubmenu();
if (!menu) {
throw Error(`Item matching ${JSON.stringify(itemFilter)} does not have a submenu`);
}
return menu.clickItem(...subItemFilters);
}
async getRootHarnessLoader() {
const panelId = await this._getPanelId();
return this.documentRootLocatorFactory().harnessLoaderFor(`#${panelId}`);
}
/** Gets the menu panel associated with this menu. */
async _getMenuPanel() {
const panelId = await this._getPanelId();
return panelId ? this._documentRootLocator.locatorForOptional(`#${panelId}`)() : null;
}
/** Gets the id of the menu panel associated with this menu. */
async _getPanelId() {
const panelId = await (await this.host()).getAttribute('aria-controls');
return panelId || null;
}
}
class _MatMenuItemHarnessBase extends ContentContainerComponentHarness {
/** Whether the menu is disabled. */
async isDisabled() {
const disabled = (await this.host()).getAttribute('disabled');
return coerceBooleanProperty(await disabled);
}
/** Gets the text of the menu item. */
async getText() {
return (await this.host()).text();
}
/** Focuses the menu item. */
async focus() {
return (await this.host()).focus();
}
/** Blurs the menu item. */
async blur() {
return (await this.host()).blur();
}
/** Whether the menu item is focused. */
async isFocused() {
return (await this.host()).isFocused();
}
/** Clicks the menu item. */
async click() {
return (await this.host()).click();
}
/** Whether this item has a submenu. */
async hasSubmenu() {
return (await this.host()).matchesSelector(this._menuClass.hostSelector);
}
/** Gets the submenu associated with this menu item, or null if none. */
async getSubmenu() {
if (await this.hasSubmenu()) {
return new this._menuClass(this.locatorFactory);
}
return null;
}
}
/** Harness for interacting with an MDC-based mat-menu in tests. */
class MatMenuHarness extends _MatMenuHarnessBase {
constructor() {
super(...arguments);
this._itemClass = MatMenuItemHarness;
}
/**
* Gets a `HarnessPredicate` that can be used to search for a menu with specific attributes.
* @param options Options for filtering which menu instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(this, options).addOption('triggerText', options.triggerText, (harness, text) => HarnessPredicate.stringMatches(harness.getTriggerText(), text));
}
}
/** The selector for the host element of a `MatMenu` instance. */
MatMenuHarness.hostSelector = '.mat-mdc-menu-trigger';
/** Harness for interacting with an MDC-based mat-menu-item in tests. */
class MatMenuItemHarness extends _MatMenuItemHarnessBase {
constructor() {
super(...arguments);
this._menuClass = MatMenuHarness;
}
/**
* Gets a `HarnessPredicate` that can be used to search for a menu item with specific attributes.
* @param options Options for filtering which menu item instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(this, options)
.addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
.addOption('hasSubmenu', options.hasSubmenu, async (harness, hasSubmenu) => (await harness.hasSubmenu()) === hasSubmenu);
}
}
/** The selector for the host element of a `MatMenuItem` instance. */
MatMenuItemHarness.hostSelector = '.mat-mdc-menu-item';
/**
* @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 { MatMenuHarness, MatMenuItemHarness, _MatMenuHarnessBase, _MatMenuItemHarnessBase };
//# sourceMappingURL=testing.mjs.map