|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ContentContainerComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; |
| 10 | +import {TreeItemHarnessFilters} from './tree-harness-filters'; |
| 11 | + |
| 12 | +/** Harness for interacting with an Aria tree item. */ |
| 13 | +export class TreeItemHarness extends ContentContainerComponentHarness<string> { |
| 14 | + static hostSelector = '[ngTreeItem]'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Gets a `HarnessPredicate` that can be used to search for a tree item with specific attributes. |
| 18 | + * @param options Options for narrowing the search |
| 19 | + * @return a `HarnessPredicate` configured with the given options. |
| 20 | + */ |
| 21 | + static with(options: TreeItemHarnessFilters = {}): HarnessPredicate<TreeItemHarness> { |
| 22 | + return new HarnessPredicate(TreeItemHarness, options) |
| 23 | + .addOption('text', options.text, (harness, text) => |
| 24 | + HarnessPredicate.stringMatches(harness.getText(), text), |
| 25 | + ) |
| 26 | + .addOption( |
| 27 | + 'disabled', |
| 28 | + options.disabled, |
| 29 | + async (harness, disabled) => (await harness.isDisabled()) === disabled, |
| 30 | + ) |
| 31 | + .addOption( |
| 32 | + 'expanded', |
| 33 | + options.expanded, |
| 34 | + async (harness, expanded) => (await harness.isExpanded()) === expanded, |
| 35 | + ) |
| 36 | + .addOption( |
| 37 | + 'selected', |
| 38 | + options.selected, |
| 39 | + async (harness, selected) => (await harness.isSelected()) === selected, |
| 40 | + ) |
| 41 | + .addOption( |
| 42 | + 'level', |
| 43 | + options.level, |
| 44 | + async (harness, level) => (await harness.getLevel()) === level, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** Whether the tree item is expanded. */ |
| 49 | + async isExpanded(): Promise<boolean> { |
| 50 | + return (await this._getHostAttribute('aria-expanded')) === 'true'; |
| 51 | + } |
| 52 | + |
| 53 | + /** Whether the tree item is disabled. */ |
| 54 | + async isDisabled(): Promise<boolean> { |
| 55 | + return (await this._getHostAttribute('aria-disabled')) === 'true'; |
| 56 | + } |
| 57 | + |
| 58 | + /** Whether the tree item is selected. */ |
| 59 | + async isSelected(): Promise<boolean> { |
| 60 | + return (await this._getHostAttribute('aria-selected')) === 'true'; |
| 61 | + } |
| 62 | + |
| 63 | + /** Gets the level of the tree item. Note that this gets the aria-level and is 1 indexed. */ |
| 64 | + async getLevel(): Promise<number> { |
| 65 | + const level = (await this._getHostAttribute('aria-level')) ?? '1'; |
| 66 | + return parseInt(level); |
| 67 | + } |
| 68 | + |
| 69 | + /** Gets the tree item's text. */ |
| 70 | + async getText(): Promise<string> { |
| 71 | + return (await this.host()).text({exclude: '[ngTreeItem], [ngTreeItemGroup]'}); |
| 72 | + } |
| 73 | + |
| 74 | + /** Clicks the tree item. */ |
| 75 | + async click(): Promise<void> { |
| 76 | + return (await this.host()).click(); |
| 77 | + } |
| 78 | + |
| 79 | + private async _getHostAttribute(attributeName: string): Promise<string | null> { |
| 80 | + return (await this.host()).getAttribute(attributeName); |
| 81 | + } |
| 82 | +} |
0 commit comments