UNPKG

@angular/material

Version:
1 lines 11.9 kB
{"version":3,"file":"testing.mjs","sources":["../../../../../../../src/material/menu/testing/menu-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.io/license\n */\n\nimport {\n ComponentHarnessConstructor,\n ContentContainerComponentHarness,\n HarnessLoader,\n HarnessPredicate,\n TestElement,\n TestKey,\n} from '@angular/cdk/testing';\nimport {coerceBooleanProperty} from '@angular/cdk/coercion';\nimport {MenuHarnessFilters, MenuItemHarnessFilters} from './menu-harness-filters';\n\n/** Harness for interacting with an MDC-based mat-menu in tests. */\nexport class MatMenuHarness extends ContentContainerComponentHarness<string> {\n private _documentRootLocator = this.documentRootLocatorFactory();\n\n /** The selector for the host element of a `MatMenu` instance. */\n static hostSelector = '.mat-mdc-menu-trigger';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a menu with specific attributes.\n * @param options Options for filtering which menu instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatMenuHarness>(\n this: ComponentHarnessConstructor<T>,\n options: MenuHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options).addOption(\n 'triggerText',\n options.triggerText,\n (harness, text) => HarnessPredicate.stringMatches(harness.getTriggerText(), text),\n );\n }\n\n /** Whether the menu is disabled. */\n async isDisabled(): Promise<boolean> {\n const disabled = (await this.host()).getAttribute('disabled');\n return coerceBooleanProperty(await disabled);\n }\n\n /** Whether the menu is open. */\n async isOpen(): Promise<boolean> {\n return !!(await this._getMenuPanel());\n }\n\n /** Gets the text of the menu's trigger element. */\n async getTriggerText(): Promise<string> {\n return (await this.host()).text();\n }\n\n /** Focuses the menu. */\n async focus(): Promise<void> {\n return (await this.host()).focus();\n }\n\n /** Blurs the menu. */\n async blur(): Promise<void> {\n return (await this.host()).blur();\n }\n\n /** Whether the menu is focused. */\n async isFocused(): Promise<boolean> {\n return (await this.host()).isFocused();\n }\n\n /** Opens the menu. */\n async open(): Promise<void> {\n if (!(await this.isOpen())) {\n return (await this.host()).click();\n }\n }\n\n /** Closes the menu. */\n async close(): Promise<void> {\n const panel = await this._getMenuPanel();\n if (panel) {\n return panel.sendKeys(TestKey.ESCAPE);\n }\n }\n\n /**\n * Gets a list of `MatMenuItemHarness` representing the items in the menu.\n * @param filters Optionally filters which menu items are included.\n */\n async getItems(\n filters?: Omit<MenuItemHarnessFilters, 'ancestor'>,\n ): Promise<MatMenuItemHarness[]> {\n const panelId = await this._getPanelId();\n if (panelId) {\n return this._documentRootLocator.locatorForAll(\n MatMenuItemHarness.with({\n ...(filters || {}),\n ancestor: `#${panelId}`,\n } as MenuItemHarnessFilters),\n )();\n }\n return [];\n }\n\n /**\n * Clicks an item in the menu, and optionally continues clicking items in subsequent sub-menus.\n * @param itemFilter A filter used to represent which item in the menu should be clicked. The\n * first matching menu item will be clicked.\n * @param subItemFilters A list of filters representing the items to click in any subsequent\n * sub-menus. The first item in the sub-menu matching the corresponding filter in\n * `subItemFilters` will be clicked.\n */\n async clickItem(\n itemFilter: Omit<MenuItemHarnessFilters, 'ancestor'>,\n ...subItemFilters: Omit<MenuItemHarnessFilters, 'ancestor'>[]\n ): Promise<void> {\n await this.open();\n const items = await this.getItems(itemFilter);\n if (!items.length) {\n throw Error(`Could not find item matching ${JSON.stringify(itemFilter)}`);\n }\n\n if (!subItemFilters.length) {\n return await items[0].click();\n }\n\n const menu = await items[0].getSubmenu();\n if (!menu) {\n throw Error(`Item matching ${JSON.stringify(itemFilter)} does not have a submenu`);\n }\n return menu.clickItem(...(subItemFilters as [Omit<MenuItemHarnessFilters, 'ancestor'>]));\n }\n\n protected override async getRootHarnessLoader(): Promise<HarnessLoader> {\n const panelId = await this._getPanelId();\n return this.documentRootLocatorFactory().harnessLoaderFor(`#${panelId}`);\n }\n\n /** Gets the menu panel associated with this menu. */\n private async _getMenuPanel(): Promise<TestElement | null> {\n const panelId = await this._getPanelId();\n return panelId ? this._documentRootLocator.locatorForOptional(`#${panelId}`)() : null;\n }\n\n /** Gets the id of the menu panel associated with this menu. */\n private async _getPanelId(): Promise<string | null> {\n const panelId = await (await this.host()).getAttribute('aria-controls');\n return panelId || null;\n }\n}\n\nexport class MatMenuItemHarness extends ContentContainerComponentHarness<string> {\n /** The selector for the host element of a `MatMenuItem` instance. */\n static hostSelector = '.mat-mdc-menu-item';\n\n /**\n * Gets a `HarnessPredicate` that can be used to search for a menu item with specific attributes.\n * @param options Options for filtering which menu item instances are considered a match.\n * @return a `HarnessPredicate` configured with the given options.\n */\n static with<T extends MatMenuItemHarness>(\n this: ComponentHarnessConstructor<T>,\n options: MenuItemHarnessFilters = {},\n ): HarnessPredicate<T> {\n return new HarnessPredicate(this, options)\n .addOption('text', options.text, (harness, text) =>\n HarnessPredicate.stringMatches(harness.getText(), text),\n )\n .addOption(\n 'hasSubmenu',\n options.hasSubmenu,\n async (harness, hasSubmenu) => (await harness.hasSubmenu()) === hasSubmenu,\n );\n }\n\n /** Whether the menu is disabled. */\n async isDisabled(): Promise<boolean> {\n const disabled = (await this.host()).getAttribute('disabled');\n return coerceBooleanProperty(await disabled);\n }\n\n /** Gets the text of the menu item. */\n async getText(): Promise<string> {\n return (await this.host()).text();\n }\n\n /** Focuses the menu item. */\n async focus(): Promise<void> {\n return (await this.host()).focus();\n }\n\n /** Blurs the menu item. */\n async blur(): Promise<void> {\n return (await this.host()).blur();\n }\n\n /** Whether the menu item is focused. */\n async isFocused(): Promise<boolean> {\n return (await this.host()).isFocused();\n }\n\n /** Clicks the menu item. */\n async click(): Promise<void> {\n return (await this.host()).click();\n }\n\n /** Whether this item has a submenu. */\n async hasSubmenu(): Promise<boolean> {\n return (await this.host()).matchesSelector(MatMenuHarness.hostSelector);\n }\n\n /** Gets the submenu associated with this menu item, or null if none. */\n async getSubmenu(): Promise<MatMenuHarness | null> {\n if (await this.hasSubmenu()) {\n return new MatMenuHarness(this.locatorFactory);\n }\n return null;\n }\n}\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