|
| 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 {ComponentHarness, HarnessPredicate, BaseHarnessFilters} from '@angular/cdk/testing'; |
| 10 | + |
| 11 | +/** Selectors for the listbox sections. */ |
| 12 | +export enum ListboxSection { |
| 13 | + OPTION = '[ngOption]', |
| 14 | +} |
| 15 | + |
| 16 | +/** Filters for locating a `ListboxOptionHarness`. */ |
| 17 | +export interface ListboxOptionHarnessFilters extends BaseHarnessFilters { |
| 18 | + /** Only find instances whose text matches the given value. */ |
| 19 | + text?: string | RegExp; |
| 20 | + /** Only find instances whose selected state matches the given value. */ |
| 21 | + selected?: boolean; |
| 22 | + /** Only find instances whose disabled state matches the given value. */ |
| 23 | + disabled?: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +export class ListboxOptionHarness extends ComponentHarness { |
| 27 | + static hostSelector = '[ngOption]'; |
| 28 | + |
| 29 | + static with(options: ListboxOptionHarnessFilters = {}): HarnessPredicate<ListboxOptionHarness> { |
| 30 | + return new HarnessPredicate(ListboxOptionHarness, options) |
| 31 | + .addOption('text', options.text, (harness, text) => |
| 32 | + HarnessPredicate.stringMatches(harness.getText(), text), |
| 33 | + ) |
| 34 | + .addOption( |
| 35 | + 'selected', |
| 36 | + options.selected, |
| 37 | + async (harness, selected) => (await harness.isSelected()) === selected, |
| 38 | + ) |
| 39 | + .addOption( |
| 40 | + 'disabled', |
| 41 | + options.disabled, |
| 42 | + async (harness, disabled) => (await harness.isDisabled()) === disabled, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + async isSelected(): Promise<boolean> { |
| 47 | + const host = await this.host(); |
| 48 | + return (await host.getAttribute('aria-selected')) === 'true'; |
| 49 | + } |
| 50 | + |
| 51 | + async isDisabled(): Promise<boolean> { |
| 52 | + const host = await this.host(); |
| 53 | + return ( |
| 54 | + (await host.getAttribute('aria-disabled')) === 'true' || |
| 55 | + (await host.getProperty('disabled')) === true |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + async getText(): Promise<string> { |
| 60 | + const host = await this.host(); |
| 61 | + return host.text(); |
| 62 | + } |
| 63 | + |
| 64 | + async click(): Promise<void> { |
| 65 | + const host = await this.host(); |
| 66 | + return host.click(); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export interface ListboxHarnessFilters extends BaseHarnessFilters { |
| 71 | + disabled?: boolean; |
| 72 | +} |
| 73 | + |
| 74 | +export class ListboxHarness extends ComponentHarness { |
| 75 | + static hostSelector = '[ngListbox]'; |
| 76 | + |
| 77 | + static with(options: ListboxHarnessFilters = {}): HarnessPredicate<ListboxHarness> { |
| 78 | + return new HarnessPredicate(ListboxHarness, options).addOption( |
| 79 | + 'disabled', |
| 80 | + options.disabled, |
| 81 | + async (harness, disabled) => (await harness.isDisabled()) === disabled, |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + async getOrientation(): Promise<'vertical' | 'horizontal'> { |
| 86 | + const host = await this.host(); |
| 87 | + const orientation = await host.getAttribute('aria-orientation'); |
| 88 | + return orientation === 'horizontal' ? 'horizontal' : 'vertical'; |
| 89 | + } |
| 90 | + |
| 91 | + async isMulti(): Promise<boolean> { |
| 92 | + const host = await this.host(); |
| 93 | + return (await host.getAttribute('aria-multiselectable')) === 'true'; |
| 94 | + } |
| 95 | + |
| 96 | + async isDisabled(): Promise<boolean> { |
| 97 | + const host = await this.host(); |
| 98 | + return (await host.getAttribute('aria-disabled')) === 'true'; |
| 99 | + } |
| 100 | + |
| 101 | + async getOptions(filters: ListboxOptionHarnessFilters = {}): Promise<ListboxOptionHarness[]> { |
| 102 | + return this.locatorForAll(ListboxOptionHarness.with(filters))(); |
| 103 | + } |
| 104 | + |
| 105 | + async focus(): Promise<void> { |
| 106 | + await (await this.host()).focus(); |
| 107 | + } |
| 108 | + |
| 109 | + /** Blurs the listbox container. */ |
| 110 | + async blur(): Promise<void> { |
| 111 | + await (await this.host()).blur(); |
| 112 | + } |
| 113 | +} |
0 commit comments