* Opens the menu. Defaults to using the interaction type set on the menu tester.
(opts: MenuOpenOpts = {})
| 112 | * Opens the menu. Defaults to using the interaction type set on the menu tester. |
| 113 | */ |
| 114 | async open(opts: MenuOpenOpts = {}): Promise<void> { |
| 115 | let {needsLongPress, interactionType = this._interactionType, direction} = opts; |
| 116 | let trigger = this.getTrigger(); |
| 117 | let isDisabled = trigger.hasAttribute('disabled'); |
| 118 | if (interactionType === 'mouse' || interactionType === 'touch') { |
| 119 | if (needsLongPress) { |
| 120 | let pointerType = interactionType === 'mouse' ? 'mouse' : 'touch'; |
| 121 | await triggerLongPress({ |
| 122 | element: trigger, |
| 123 | advanceTimer: this._advanceTimer!, |
| 124 | pointerOpts: {pointerType} |
| 125 | }); |
| 126 | } else if (interactionType === 'mouse') { |
| 127 | await this.user.click(trigger); |
| 128 | } else { |
| 129 | await this.user.pointer({target: trigger, keys: '[TouchA]'}); |
| 130 | } |
| 131 | } else if (interactionType === 'keyboard' && !isDisabled) { |
| 132 | if (direction === 'up') { |
| 133 | act(() => trigger.focus()); |
| 134 | await this.user.keyboard('[ArrowUp]'); |
| 135 | } else if (direction === 'down') { |
| 136 | act(() => trigger.focus()); |
| 137 | await this.user.keyboard('[ArrowDown]'); |
| 138 | } else { |
| 139 | act(() => trigger.focus()); |
| 140 | await this.user.keyboard('[Enter]'); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | await waitFor(() => { |
| 145 | if (trigger.getAttribute('aria-controls') == null && !isDisabled) { |
| 146 | throw new Error('No aria-controls found on menu trigger element.'); |
| 147 | } else { |
| 148 | return true; |
| 149 | } |
| 150 | }); |
| 151 | if (!isDisabled) { |
| 152 | let menuId = trigger.getAttribute('aria-controls'); |
| 153 | await waitFor(() => { |
| 154 | if (!menuId || document.getElementById(menuId) == null) { |
| 155 | throw new Error(`Menu with id of ${menuId} not found in document.`); |
| 156 | } else { |
| 157 | return true; |
| 158 | } |
| 159 | }); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns a option matching the specified index or text content. |
no test coverage detected