|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {act, waitFor, within} from '@testing-library/react'; |
| 14 | +import {BaseTesterOpts, UserOpts} from './user'; |
| 15 | + |
| 16 | +export interface ComboBoxOptions extends UserOpts, BaseTesterOpts { |
| 17 | + user: any, |
| 18 | + trigger?: HTMLElement |
| 19 | +} |
| 20 | + |
| 21 | +export class ComboBoxTester { |
| 22 | + private user; |
| 23 | + private _interactionType: UserOpts['interactionType']; |
| 24 | + private _combobox: HTMLElement; |
| 25 | + private _trigger: HTMLElement | undefined; |
| 26 | + |
| 27 | + constructor(opts: ComboBoxOptions) { |
| 28 | + let {root, trigger, user, interactionType} = opts; |
| 29 | + this.user = user; |
| 30 | + this._interactionType = interactionType || 'mouse'; |
| 31 | + |
| 32 | + // Handle case where element provided is a wrapper around the combobox. The expectation is that the user at least uses a ref/data attribute to |
| 33 | + // query their combobox/combobox wrapper (in the case of RSP) which they then pass to thhis |
| 34 | + this._combobox = root; |
| 35 | + let combobox = within(root).queryByRole('combobox'); |
| 36 | + if (combobox) { |
| 37 | + this._combobox = combobox; |
| 38 | + } |
| 39 | + |
| 40 | + // This is for if user need to directly set the trigger button element (aka the element provided in setElement was the combobox input or the trigger is somewhere unexpected) |
| 41 | + if (trigger) { |
| 42 | + this._trigger = trigger; |
| 43 | + } else { |
| 44 | + let trigger = within(root).queryByRole('button', {hidden: true}); |
| 45 | + if (trigger) { |
| 46 | + this._trigger = trigger; |
| 47 | + } else { |
| 48 | + // For cases like https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/ where the combobox |
| 49 | + // is also the trigger button |
| 50 | + this._trigger = this._combobox; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + setInteractionType = (type: UserOpts['interactionType']) => { |
| 56 | + this._interactionType = type; |
| 57 | + }; |
| 58 | + |
| 59 | + open = async (opts: {triggerBehavior?: 'focus' | 'manual', interactionType?: UserOpts['interactionType']} = {}) => { |
| 60 | + let {triggerBehavior = 'manual', interactionType = this._interactionType} = opts; |
| 61 | + let trigger = this.trigger; |
| 62 | + let combobox = this.combobox; |
| 63 | + let isDisabled = trigger!.hasAttribute('disabled'); |
| 64 | + |
| 65 | + if (interactionType === 'mouse') { |
| 66 | + if (triggerBehavior === 'focus') { |
| 67 | + await this.user.click(combobox); |
| 68 | + } else { |
| 69 | + await this.user.click(trigger); |
| 70 | + } |
| 71 | + } else if (interactionType === 'keyboard' && this._trigger != null) { |
| 72 | + act(() => this._trigger!.focus()); |
| 73 | + if (triggerBehavior !== 'focus') { |
| 74 | + await this.user.keyboard('{ArrowDown}'); |
| 75 | + } |
| 76 | + } else if (interactionType === 'touch') { |
| 77 | + if (triggerBehavior === 'focus') { |
| 78 | + await this.user.pointer({target: combobox, keys: '[TouchA]'}); |
| 79 | + } else { |
| 80 | + await this.user.pointer({target: trigger, keys: '[TouchA]'}); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + await waitFor(() => { |
| 85 | + if (!isDisabled && combobox.getAttribute('aria-controls') == null) { |
| 86 | + throw new Error('No aria-controls found on combobox trigger element.'); |
| 87 | + } else { |
| 88 | + return true; |
| 89 | + } |
| 90 | + }); |
| 91 | + let listBoxId = combobox.getAttribute('aria-controls'); |
| 92 | + await waitFor(() => { |
| 93 | + if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) { |
| 94 | + throw new Error(`Listbox with id of ${listBoxId} not found in document.`); |
| 95 | + } else { |
| 96 | + return true; |
| 97 | + } |
| 98 | + }); |
| 99 | + }; |
| 100 | + |
| 101 | + selectOption = async (opts: {option?: HTMLElement, optionText?: string, triggerBehavior?: 'focus' | 'manual', interactionType?: UserOpts['interactionType']} = {}) => { |
| 102 | + let {optionText, option, triggerBehavior, interactionType = this._interactionType} = opts; |
| 103 | + if (!this.combobox.getAttribute('aria-controls')) { |
| 104 | + await this.open({triggerBehavior}); |
| 105 | + } |
| 106 | + |
| 107 | + let listbox = this.listbox; |
| 108 | + if (listbox) { |
| 109 | + if (!option && optionText) { |
| 110 | + option = within(listbox).getByText(optionText); |
| 111 | + } |
| 112 | + |
| 113 | + // TODO: keyboard method of selecting the the option is a bit tricky unless I simply simulate the user pressing the down arrow |
| 114 | + // the required amount of times to reach the option. For now just click the option even in keyboard mode |
| 115 | + if (interactionType === 'mouse' || interactionType === 'keyboard') { |
| 116 | + await this.user.click(option); |
| 117 | + } else { |
| 118 | + await this.user.pointer({target: option, keys: '[TouchA]'}); |
| 119 | + } |
| 120 | + |
| 121 | + if (option && option.getAttribute('href') == null) { |
| 122 | + await waitFor(() => { |
| 123 | + if (document.contains(listbox)) { |
| 124 | + throw new Error('Expected listbox element to not be in the document after selecting an option'); |
| 125 | + } else { |
| 126 | + return true; |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + } else { |
| 131 | + throw new Error("Attempted to select a option in the combobox, but the listbox wasn't found."); |
| 132 | + } |
| 133 | + }; |
| 134 | + |
| 135 | + close = async () => { |
| 136 | + let listbox = this.listbox; |
| 137 | + if (listbox) { |
| 138 | + act(() => this.combobox.focus()); |
| 139 | + await this.user.keyboard('[Escape]'); |
| 140 | + |
| 141 | + await waitFor(() => { |
| 142 | + if (document.contains(listbox)) { |
| 143 | + throw new Error('Expected listbox element to not be in the document after selecting an option'); |
| 144 | + } else { |
| 145 | + return true; |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + get combobox() { |
| 152 | + return this._combobox; |
| 153 | + } |
| 154 | + |
| 155 | + get trigger() { |
| 156 | + return this._trigger; |
| 157 | + } |
| 158 | + |
| 159 | + get listbox() { |
| 160 | + let listBoxId = this.combobox.getAttribute('aria-controls'); |
| 161 | + return listBoxId ? document.getElementById(listBoxId) || undefined : undefined; |
| 162 | + } |
| 163 | + |
| 164 | + options = (opts: {element?: HTMLElement} = {}): HTMLElement[] | never[] => { |
| 165 | + let {element} = opts; |
| 166 | + element = element || this.listbox; |
| 167 | + let options = []; |
| 168 | + if (element) { |
| 169 | + options = within(element).queryAllByRole('option'); |
| 170 | + } |
| 171 | + |
| 172 | + return options; |
| 173 | + }; |
| 174 | + |
| 175 | + get sections() { |
| 176 | + let listbox = this.listbox; |
| 177 | + if (listbox) { |
| 178 | + return within(listbox).queryAllByRole('group'); |
| 179 | + } else { |
| 180 | + return []; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + get focusedOption() { |
| 185 | + let focusedOptionId = this.combobox.getAttribute('aria-activedescendant'); |
| 186 | + return focusedOptionId ? document.getElementById(focusedOptionId) : undefined; |
| 187 | + } |
| 188 | +} |
0 commit comments