|
| 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 | +/** Filters for locating an `AccordionTriggerHarness`. */ |
| 12 | +export interface AccordionTriggerHarnessFilters extends BaseHarnessFilters { |
| 13 | + /** Only find instances whose text matches the given value. */ |
| 14 | + text?: string | RegExp; |
| 15 | + /** Only find instances whose expanded state matches the given value. */ |
| 16 | + expanded?: boolean; |
| 17 | + /** Only find instances whose disabled state matches the given value. */ |
| 18 | + disabled?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +/** Filters for locating an `AccordionPanelHarness`. */ |
| 22 | +export interface AccordionPanelHarnessFilters extends BaseHarnessFilters { |
| 23 | + /** Find the panel associated with the given trigger harness. */ |
| 24 | + trigger?: AccordionTriggerHarness; |
| 25 | +} |
| 26 | + |
| 27 | +/** Filters for locating an `AccordionGroupHarness`. */ |
| 28 | +export interface AccordionGroupHarnessFilters extends BaseHarnessFilters {} |
| 29 | + |
| 30 | +/** Harness for interacting with an `ngAccordionPanel` in tests. */ |
| 31 | +export class AccordionPanelHarness extends ComponentHarness { |
| 32 | + /** The selector for the host element of an `ngAccordionPanel` instance. */ |
| 33 | + static hostSelector = '[ngAccordionPanel]'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets a `HarnessPredicate` that can be used to search for a panel with specific attributes. |
| 37 | + * @param options Options for narrowing the search. |
| 38 | + * @return a `HarnessPredicate` configured with the given options. |
| 39 | + */ |
| 40 | + static with(options: AccordionPanelHarnessFilters = {}): HarnessPredicate<AccordionPanelHarness> { |
| 41 | + return new HarnessPredicate(AccordionPanelHarness, options).addOption( |
| 42 | + 'trigger', |
| 43 | + options.trigger, |
| 44 | + async (harness, trigger) => { |
| 45 | + const targetPanelId = await (await trigger.host()).getAttribute('aria-controls'); |
| 46 | + const panelId = await (await harness.host()).getAttribute('id'); |
| 47 | + return panelId === targetPanelId; |
| 48 | + }, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** Gets the text content of the accordion panel. */ |
| 53 | + async getText(): Promise<string> { |
| 54 | + return (await this.host()).text(); |
| 55 | + } |
| 56 | + |
| 57 | + /** Whether the accordion panel is expanded (visible and not inert). */ |
| 58 | + async isExpanded(): Promise<boolean> { |
| 59 | + return (await (await this.host()).getAttribute('inert')) === null; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** Harness for interacting with an `ngAccordionTrigger` in tests. */ |
| 64 | +export class AccordionTriggerHarness extends ComponentHarness { |
| 65 | + /** The selector for the host element of an `ngAccordionTrigger` instance. */ |
| 66 | + static hostSelector = '[ngAccordionTrigger]'; |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets a `HarnessPredicate` that can be used to search for a trigger with specific attributes. |
| 70 | + * @param options Options for narrowing the search. |
| 71 | + * @return a `HarnessPredicate` configured with the given options. |
| 72 | + */ |
| 73 | + static with( |
| 74 | + options: AccordionTriggerHarnessFilters = {}, |
| 75 | + ): HarnessPredicate<AccordionTriggerHarness> { |
| 76 | + return new HarnessPredicate(AccordionTriggerHarness, options) |
| 77 | + .addOption('text', options.text, (harness, text) => |
| 78 | + HarnessPredicate.stringMatches(harness.getText(), text), |
| 79 | + ) |
| 80 | + .addOption( |
| 81 | + 'expanded', |
| 82 | + options.expanded, |
| 83 | + async (harness, expanded) => (await harness.isExpanded()) === expanded, |
| 84 | + ) |
| 85 | + .addOption( |
| 86 | + 'disabled', |
| 87 | + options.disabled, |
| 88 | + async (harness, disabled) => (await harness.isDisabled()) === disabled, |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** Gets the text content of the accordion trigger. */ |
| 93 | + async getText(): Promise<string> { |
| 94 | + return (await this.host()).text(); |
| 95 | + } |
| 96 | + |
| 97 | + /** Clicks the accordion trigger. */ |
| 98 | + async click(): Promise<void> { |
| 99 | + return (await this.host()).click(); |
| 100 | + } |
| 101 | + |
| 102 | + /** Focuses the accordion trigger. */ |
| 103 | + async focus(): Promise<void> { |
| 104 | + return (await this.host()).focus(); |
| 105 | + } |
| 106 | + |
| 107 | + /** Blurs the accordion trigger. */ |
| 108 | + async blur(): Promise<void> { |
| 109 | + return (await this.host()).blur(); |
| 110 | + } |
| 111 | + |
| 112 | + /** Whether the accordion trigger is focused. */ |
| 113 | + async isFocused(): Promise<boolean> { |
| 114 | + return (await this.host()).isFocused(); |
| 115 | + } |
| 116 | + |
| 117 | + /** Whether the accordion panel associated with this trigger is expanded. */ |
| 118 | + async isExpanded(): Promise<boolean> { |
| 119 | + const ariaExpanded = await (await this.host()).getAttribute('aria-expanded'); |
| 120 | + return ariaExpanded === 'true'; |
| 121 | + } |
| 122 | + |
| 123 | + /** Whether the accordion trigger is disabled. */ |
| 124 | + async isDisabled(): Promise<boolean> { |
| 125 | + const ariaDisabled = await (await this.host()).getAttribute('aria-disabled'); |
| 126 | + return ariaDisabled === 'true'; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** Harness for interacting with an `ngAccordionGroup` in tests. */ |
| 131 | +export class AccordionGroupHarness extends ComponentHarness { |
| 132 | + /** The selector for the host element of an `ngAccordionGroup` instance. */ |
| 133 | + static hostSelector = '[ngAccordionGroup]'; |
| 134 | + |
| 135 | + /** |
| 136 | + * Gets a `HarnessPredicate` that can be used to search for an accordion group with specific attributes. |
| 137 | + * @param options Options for narrowing the search. |
| 138 | + * @return a `HarnessPredicate` configured with the given options. |
| 139 | + */ |
| 140 | + static with(options: AccordionGroupHarnessFilters = {}): HarnessPredicate<AccordionGroupHarness> { |
| 141 | + return new HarnessPredicate(AccordionGroupHarness, options); |
| 142 | + } |
| 143 | + |
| 144 | + /** Gets all accordion triggers within this group. */ |
| 145 | + async getTriggers( |
| 146 | + filters: AccordionTriggerHarnessFilters = {}, |
| 147 | + ): Promise<AccordionTriggerHarness[]> { |
| 148 | + return this.locatorForAll(AccordionTriggerHarness.with(filters))(); |
| 149 | + } |
| 150 | + |
| 151 | + /** Gets all accordion panels within this group. */ |
| 152 | + async getPanels(filters: AccordionPanelHarnessFilters = {}): Promise<AccordionPanelHarness[]> { |
| 153 | + return this.locatorForAll(AccordionPanelHarness.with(filters))(); |
| 154 | + } |
| 155 | +} |
0 commit comments