@react-aria/test-utils 1.0.0-nightly.5041 → 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -0
- package/dist/import.mjs +6 -4
- package/dist/main.js +15 -23
- package/dist/main.js.map +1 -1
- package/dist/module.js +6 -4
- package/dist/module.js.map +1 -1
- package/dist/private/act.cjs +33 -0
- package/dist/private/act.cjs.map +1 -0
- package/dist/private/act.js +28 -0
- package/dist/private/act.js.map +1 -0
- package/dist/private/checkboxgroup.cjs +107 -0
- package/dist/private/checkboxgroup.cjs.map +1 -0
- package/dist/private/checkboxgroup.js +102 -0
- package/dist/private/checkboxgroup.js.map +1 -0
- package/dist/private/combobox.cjs +199 -0
- package/dist/private/combobox.cjs.map +1 -0
- package/dist/private/combobox.js +194 -0
- package/dist/private/combobox.js.map +1 -0
- package/dist/private/dialog.cjs +110 -0
- package/dist/private/dialog.cjs.map +1 -0
- package/dist/private/dialog.js +105 -0
- package/dist/private/dialog.js.map +1 -0
- package/dist/private/gridlist.cjs +173 -0
- package/dist/private/gridlist.cjs.map +1 -0
- package/dist/private/gridlist.js +168 -0
- package/dist/private/gridlist.js.map +1 -0
- package/dist/private/listbox.cjs +163 -0
- package/dist/private/listbox.cjs.map +1 -0
- package/dist/private/listbox.js +158 -0
- package/dist/private/listbox.js.map +1 -0
- package/dist/private/menu.cjs +265 -0
- package/dist/private/menu.cjs.map +1 -0
- package/dist/private/menu.js +260 -0
- package/dist/private/menu.js.map +1 -0
- package/dist/private/radiogroup.cjs +122 -0
- package/dist/private/radiogroup.cjs.map +1 -0
- package/dist/private/radiogroup.js +117 -0
- package/dist/private/radiogroup.js.map +1 -0
- package/dist/private/select.cjs +169 -0
- package/dist/private/select.cjs.map +1 -0
- package/dist/private/select.js +164 -0
- package/dist/private/select.js.map +1 -0
- package/dist/private/table.cjs +346 -0
- package/dist/private/table.cjs.map +1 -0
- package/dist/private/table.js +341 -0
- package/dist/private/table.js.map +1 -0
- package/dist/private/tabs.cjs +131 -0
- package/dist/private/tabs.cjs.map +1 -0
- package/dist/private/tabs.js +126 -0
- package/dist/private/tabs.js.map +1 -0
- package/dist/private/testSetup.cjs +87 -0
- package/dist/private/testSetup.cjs.map +1 -0
- package/dist/private/testSetup.js +81 -0
- package/dist/private/testSetup.js.map +1 -0
- package/dist/private/tree.cjs +181 -0
- package/dist/private/tree.cjs.map +1 -0
- package/dist/private/tree.js +176 -0
- package/dist/private/tree.js.map +1 -0
- package/dist/private/user.cjs +85 -0
- package/dist/private/user.cjs.map +1 -0
- package/dist/private/user.js +76 -0
- package/dist/private/user.js.map +1 -0
- package/dist/{userEventMaps.main.js → private/userEventMaps.cjs} +3 -3
- package/dist/private/userEventMaps.cjs.map +1 -0
- package/dist/{userEventMaps.mjs → private/userEventMaps.js} +3 -3
- package/dist/private/userEventMaps.js.map +1 -0
- package/dist/private/utils.cjs +136 -0
- package/dist/private/utils.cjs.map +1 -0
- package/dist/private/utils.js +127 -0
- package/dist/private/utils.js.map +1 -0
- package/dist/types/src/act.d.ts +4 -0
- package/dist/types/src/checkboxgroup.d.ts +47 -0
- package/dist/types/src/combobox.d.ts +87 -0
- package/dist/types/src/dialog.d.ts +37 -0
- package/dist/types/src/events.d.ts +25 -0
- package/dist/types/src/gridlist.d.ts +56 -0
- package/dist/types/src/index.d.ts +16 -0
- package/dist/types/src/listbox.d.ts +91 -0
- package/dist/types/src/menu.d.ts +112 -0
- package/dist/types/src/radiogroup.d.ts +47 -0
- package/dist/types/src/select.d.ts +74 -0
- package/dist/types/src/table.d.ts +120 -0
- package/dist/types/src/tabs.d.ts +59 -0
- package/dist/types/src/testSetup.d.ts +6 -0
- package/dist/types/src/tree.d.ts +62 -0
- package/dist/types/src/types.d.ts +143 -0
- package/dist/types/src/user.d.ts +49 -0
- package/dist/types/src/userEventMaps.d.ts +2 -0
- package/dist/types/src/utils.d.ts +29 -0
- package/package.json +26 -18
- package/src/act.ts +35 -0
- package/src/checkboxgroup.ts +165 -0
- package/src/combobox.ts +307 -0
- package/src/dialog.ts +155 -0
- package/src/gridlist.ts +278 -0
- package/src/index.ts +17 -3
- package/src/listbox.ts +300 -0
- package/src/menu.ts +479 -0
- package/src/radiogroup.ts +179 -0
- package/src/select.ts +273 -0
- package/src/table.ts +589 -0
- package/src/tabs.ts +204 -0
- package/src/testSetup.ts +41 -36
- package/src/tree.ts +290 -0
- package/src/types.ts +171 -0
- package/src/user.ts +153 -0
- package/src/userEventMaps.ts +1 -1
- package/src/utils.ts +155 -0
- package/dist/events.main.js +0 -37
- package/dist/events.main.js.map +0 -1
- package/dist/events.mjs +0 -31
- package/dist/events.module.js +0 -31
- package/dist/events.module.js.map +0 -1
- package/dist/testSetup.main.js +0 -82
- package/dist/testSetup.main.js.map +0 -1
- package/dist/testSetup.mjs +0 -76
- package/dist/testSetup.module.js +0 -76
- package/dist/testSetup.module.js.map +0 -1
- package/dist/types.d.ts +0 -16
- package/dist/types.d.ts.map +0 -1
- package/dist/userEventMaps.main.js.map +0 -1
- package/dist/userEventMaps.module.js +0 -38
- package/dist/userEventMaps.module.js.map +0 -1
- package/src/events.ts +0 -28
package/src/menu.ts
ADDED
|
@@ -0,0 +1,479 @@
|
|
|
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} from './act';
|
|
14
|
+
import {formatTargetNode, triggerLongPress} from './utils';
|
|
15
|
+
import {MenuTesterOpts, UserOpts} from './types';
|
|
16
|
+
import {waitFor, within} from '@testing-library/dom';
|
|
17
|
+
|
|
18
|
+
interface MenuOpenOpts {
|
|
19
|
+
/**
|
|
20
|
+
* Whether the menu needs to be long pressed to open.
|
|
21
|
+
*/
|
|
22
|
+
needsLongPress?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* What interaction type to use when opening the menu. Defaults to the interaction type set on the
|
|
25
|
+
* tester.
|
|
26
|
+
*/
|
|
27
|
+
interactionType?: UserOpts['interactionType'];
|
|
28
|
+
/**
|
|
29
|
+
* Whether to open the menu via ArrowUp or ArrowDown if in keyboard modality.
|
|
30
|
+
*/
|
|
31
|
+
direction?: 'up' | 'down';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface MenuSelectOpts extends MenuOpenOpts {
|
|
35
|
+
/**
|
|
36
|
+
* The index, text, or node of the option to select. Option nodes can be sourced via
|
|
37
|
+
* `getOptions()`.
|
|
38
|
+
*/
|
|
39
|
+
option: number | string | HTMLElement;
|
|
40
|
+
/**
|
|
41
|
+
* The menu's selection mode. Will affect whether or not the menu is expected to be closed upon
|
|
42
|
+
* option selection.
|
|
43
|
+
*
|
|
44
|
+
* @default 'single'
|
|
45
|
+
*/
|
|
46
|
+
menuSelectionMode?: 'single' | 'multiple';
|
|
47
|
+
/**
|
|
48
|
+
* Whether or not the menu closes on select. Depends on menu implementation and configuration.
|
|
49
|
+
*
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
closesOnSelect?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Whether the option should be triggered by Space or Enter in keyboard modality.
|
|
55
|
+
*
|
|
56
|
+
* @default 'Enter'
|
|
57
|
+
*/
|
|
58
|
+
keyboardActivation?: 'Space' | 'Enter';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface MenuOpenSubmenuOpts extends MenuOpenOpts {
|
|
62
|
+
/**
|
|
63
|
+
* The text or node of the submenu trigger to open. Available submenu trigger nodes can be sourced
|
|
64
|
+
* via `getSubmenuTriggers()`.
|
|
65
|
+
*/
|
|
66
|
+
submenuTrigger: string | HTMLElement;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class MenuTester {
|
|
70
|
+
private user;
|
|
71
|
+
private _interactionType: UserOpts['interactionType'];
|
|
72
|
+
private _advanceTimer: UserOpts['advanceTimer'];
|
|
73
|
+
private _trigger: HTMLElement | undefined;
|
|
74
|
+
private _isSubmenu: boolean = false;
|
|
75
|
+
private _rootMenu: HTMLElement | undefined;
|
|
76
|
+
|
|
77
|
+
constructor(opts: MenuTesterOpts) {
|
|
78
|
+
let {root, user, interactionType, advanceTimer, isSubmenu, rootMenu} = opts;
|
|
79
|
+
this.user = user;
|
|
80
|
+
this._interactionType = interactionType || 'mouse';
|
|
81
|
+
this._advanceTimer = advanceTimer;
|
|
82
|
+
|
|
83
|
+
// Handle case where a submenu trigger is provided to the tester
|
|
84
|
+
if (root.getAttribute('role') === 'menuitem') {
|
|
85
|
+
this._trigger = root;
|
|
86
|
+
} else {
|
|
87
|
+
// Handle case where element provided is a wrapper of the trigger button.
|
|
88
|
+
let buttons = within(root).queryAllByRole('button');
|
|
89
|
+
let triggerButton: HTMLElement | undefined;
|
|
90
|
+
if (buttons.length === 0) {
|
|
91
|
+
triggerButton = root;
|
|
92
|
+
} else if (buttons.length === 1) {
|
|
93
|
+
triggerButton = buttons[0];
|
|
94
|
+
} else {
|
|
95
|
+
triggerButton = buttons.find(button => button.hasAttribute('aria-haspopup'));
|
|
96
|
+
}
|
|
97
|
+
this._trigger = triggerButton ?? root;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
this._isSubmenu = isSubmenu || false;
|
|
101
|
+
this._rootMenu = rootMenu;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Set the interaction type used by the menu tester.
|
|
106
|
+
*/
|
|
107
|
+
setInteractionType(type: UserOpts['interactionType']): void {
|
|
108
|
+
this._interactionType = type;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
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.
|
|
165
|
+
*/
|
|
166
|
+
findOption(opts: {indexOrText: number | string}): HTMLElement {
|
|
167
|
+
let {indexOrText} = opts;
|
|
168
|
+
|
|
169
|
+
let option;
|
|
170
|
+
let options = this.getOptions();
|
|
171
|
+
let menu = this.getMenu();
|
|
172
|
+
|
|
173
|
+
if (typeof indexOrText === 'number') {
|
|
174
|
+
option = options[indexOrText];
|
|
175
|
+
} else if (typeof indexOrText === 'string' && menu != null) {
|
|
176
|
+
option = within(menu!)
|
|
177
|
+
.getByText(indexOrText)
|
|
178
|
+
.closest('[role=menuitem], [role=menuitemradio], [role=menuitemcheckbox]')! as HTMLElement;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return option;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Toggles the selection of the desired menu option if possible. Defaults to using the interaction
|
|
186
|
+
* type set on the menu tester. If necessary, will open the menu dropdown beforehand. The desired
|
|
187
|
+
* option can be targeted via the option's node, the option's text, or the option's index.
|
|
188
|
+
*/
|
|
189
|
+
async toggleOptionSelection(opts: MenuSelectOpts): Promise<void> {
|
|
190
|
+
let {
|
|
191
|
+
menuSelectionMode = 'single',
|
|
192
|
+
needsLongPress,
|
|
193
|
+
closesOnSelect = true,
|
|
194
|
+
option,
|
|
195
|
+
interactionType = this._interactionType,
|
|
196
|
+
keyboardActivation = 'Enter'
|
|
197
|
+
} = opts;
|
|
198
|
+
let trigger = this.getTrigger();
|
|
199
|
+
|
|
200
|
+
if (!trigger.getAttribute('aria-controls') && !trigger.hasAttribute('aria-expanded')) {
|
|
201
|
+
await this.open({needsLongPress});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let menu = this.getMenu();
|
|
205
|
+
|
|
206
|
+
if (!menu) {
|
|
207
|
+
throw new Error('Menu not found.');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (menu) {
|
|
211
|
+
if (typeof option === 'string' || typeof option === 'number') {
|
|
212
|
+
option = this.findOption({indexOrText: option});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!option) {
|
|
216
|
+
throw new Error(`Target option "${formatTargetNode(opts.option)}" not found in the menu.`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (interactionType === 'keyboard') {
|
|
220
|
+
if (option?.getAttribute('aria-disabled') === 'true') {
|
|
221
|
+
throw new Error(`Cannot select disabled option "${formatTargetNode(opts.option)}".`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (document.activeElement !== menu && !menu.contains(document.activeElement)) {
|
|
225
|
+
act(() => menu.focus());
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
await this.keyboardNavigateToOption({option});
|
|
229
|
+
await this.user.keyboard(`[${keyboardActivation}]`);
|
|
230
|
+
} else {
|
|
231
|
+
if (interactionType === 'mouse') {
|
|
232
|
+
await this.user.click(option);
|
|
233
|
+
} else {
|
|
234
|
+
await this.user.pointer({target: option, keys: '[TouchA]'});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// This chain of waitFors is needed in place of running all timers since we don't know how long transitions may take, or what action
|
|
239
|
+
// the menu option select may trigger.
|
|
240
|
+
if (
|
|
241
|
+
!(menuSelectionMode === 'single' && !closesOnSelect) &&
|
|
242
|
+
!(
|
|
243
|
+
menuSelectionMode === 'multiple' &&
|
|
244
|
+
(keyboardActivation === 'Space' || interactionType === 'mouse')
|
|
245
|
+
)
|
|
246
|
+
) {
|
|
247
|
+
// For RSP, clicking on a submenu option seems to briefly lose focus to the body before moving to the clicked option in the test so we need to wait
|
|
248
|
+
// for focus to be coerced to somewhere else in place of running all timers.
|
|
249
|
+
if (this._isSubmenu) {
|
|
250
|
+
await waitFor(() => {
|
|
251
|
+
if (document.activeElement === document.body) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
'Expected focus to move to somewhere other than the body after selecting a submenu option.'
|
|
254
|
+
);
|
|
255
|
+
} else {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// If user isn't trying to select multiple menu options or closeOnSelect is true then we can assume that
|
|
262
|
+
// the menu will close or some action is triggered. In cases like that focus should move somewhere after the menu closes
|
|
263
|
+
// but we can't really know where so just make sure it doesn't get lost to the body.
|
|
264
|
+
await waitFor(() => {
|
|
265
|
+
if (document.activeElement === option) {
|
|
266
|
+
throw new Error(
|
|
267
|
+
'Expected focus after selecting an option to move away from the option.'
|
|
268
|
+
);
|
|
269
|
+
} else {
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// We'll also want to wait for focus to move away from the original submenu trigger since the entire submenu tree should
|
|
275
|
+
// close. In React 16, focus actually makes it all the way to the root menu's submenu trigger so we need check the root menu
|
|
276
|
+
if (this._isSubmenu) {
|
|
277
|
+
await waitFor(() => {
|
|
278
|
+
if (
|
|
279
|
+
document.activeElement === this.getTrigger() ||
|
|
280
|
+
this._rootMenu?.contains(document.activeElement)
|
|
281
|
+
) {
|
|
282
|
+
throw new Error(
|
|
283
|
+
'Expected focus after selecting an submenu option to move away from the original submenu trigger.'
|
|
284
|
+
);
|
|
285
|
+
} else {
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Finally wait for focus to be coerced somewhere final when the menu tree is removed from the DOM
|
|
292
|
+
await waitFor(() => {
|
|
293
|
+
if (document.activeElement === document.body) {
|
|
294
|
+
throw new Error(
|
|
295
|
+
'Expected focus to move to somewhere other than the body after selecting a menu option.'
|
|
296
|
+
);
|
|
297
|
+
} else {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
} else {
|
|
303
|
+
throw new Error("Attempted to select a option in the menu, but menu wasn't found.");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Opens the submenu. Defaults to using the interaction type set on the menu tester. The submenu
|
|
309
|
+
* trigger can be targeted via the trigger's node or the trigger's text.
|
|
310
|
+
*/
|
|
311
|
+
async openSubmenu(opts: MenuOpenSubmenuOpts): Promise<MenuTester> {
|
|
312
|
+
let {submenuTrigger, needsLongPress, interactionType = this._interactionType} = opts;
|
|
313
|
+
|
|
314
|
+
let trigger = this.getTrigger();
|
|
315
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
316
|
+
if (isDisabled) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Cannot open submenu because its parent menu's trigger "${formatTargetNode(trigger)}" is disabled.`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
if (!trigger.getAttribute('aria-controls')) {
|
|
322
|
+
await this.open({needsLongPress});
|
|
323
|
+
}
|
|
324
|
+
let menu = this.getMenu();
|
|
325
|
+
if (!menu) {
|
|
326
|
+
throw new Error(
|
|
327
|
+
`Cannot open submenu, parent menu didn't open on trigger "${formatTargetNode(trigger)}" press.`
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
if (typeof submenuTrigger === 'string') {
|
|
331
|
+
submenuTrigger = within(menu!)
|
|
332
|
+
.getByText(submenuTrigger)
|
|
333
|
+
.closest('[role=menuitem]')! as HTMLElement;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (
|
|
337
|
+
submenuTrigger.getAttribute('aria-disabled') === 'true' ||
|
|
338
|
+
submenuTrigger.hasAttribute('disabled')
|
|
339
|
+
) {
|
|
340
|
+
throw new Error(
|
|
341
|
+
`Cannot open submenu because its trigger "${formatTargetNode(submenuTrigger)}" is disabled.`
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
let submenuTriggerTester = new MenuTester({
|
|
346
|
+
user: this.user,
|
|
347
|
+
interactionType: this._interactionType,
|
|
348
|
+
root: submenuTrigger,
|
|
349
|
+
isSubmenu: true,
|
|
350
|
+
advanceTimer: this._advanceTimer,
|
|
351
|
+
rootMenu: (this._isSubmenu ? this._rootMenu : this.getMenu()) || undefined
|
|
352
|
+
});
|
|
353
|
+
if (interactionType === 'mouse') {
|
|
354
|
+
await this.user.pointer({target: submenuTrigger});
|
|
355
|
+
} else if (interactionType === 'keyboard') {
|
|
356
|
+
await this.keyboardNavigateToOption({option: submenuTrigger});
|
|
357
|
+
await this.user.keyboard('[ArrowRight]');
|
|
358
|
+
} else {
|
|
359
|
+
await submenuTriggerTester.open();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
await waitFor(() => {
|
|
363
|
+
if (submenuTriggerTester._trigger?.getAttribute('aria-expanded') !== 'true') {
|
|
364
|
+
throw new Error(
|
|
365
|
+
'aria-expanded for the submenu trigger wasn\'t changed to "true", unable to confirm the existance of the submenu'
|
|
366
|
+
);
|
|
367
|
+
} else {
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
return submenuTriggerTester;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
private async keyboardNavigateToOption(opts: {option: HTMLElement}) {
|
|
376
|
+
let {option} = opts;
|
|
377
|
+
let options = this.getOptions();
|
|
378
|
+
let targetIndex = options.findIndex(opt => opt === option || opt.contains(option));
|
|
379
|
+
|
|
380
|
+
if (targetIndex === -1) {
|
|
381
|
+
throw new Error('Option provided is not in the menu');
|
|
382
|
+
}
|
|
383
|
+
if (document.activeElement === this.getMenu()) {
|
|
384
|
+
await this.user.keyboard('[ArrowDown]');
|
|
385
|
+
}
|
|
386
|
+
let currIndex = options.indexOf(document.activeElement as HTMLElement);
|
|
387
|
+
if (currIndex === -1) {
|
|
388
|
+
throw new Error('ActiveElement is not in the menu');
|
|
389
|
+
}
|
|
390
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
391
|
+
|
|
392
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
393
|
+
await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Closes the menu.
|
|
399
|
+
*/
|
|
400
|
+
async close(): Promise<void> {
|
|
401
|
+
let menu = this.getMenu();
|
|
402
|
+
if (menu) {
|
|
403
|
+
act(() => menu.focus());
|
|
404
|
+
await this.user.keyboard('[Escape]');
|
|
405
|
+
|
|
406
|
+
await waitFor(() => {
|
|
407
|
+
if (document.activeElement !== this.getTrigger()) {
|
|
408
|
+
throw new Error(
|
|
409
|
+
`Expected the document.activeElement after closing the menu to be the menu trigger but got ${document.activeElement}`
|
|
410
|
+
);
|
|
411
|
+
} else {
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
if (document.contains(menu)) {
|
|
417
|
+
throw new Error('Expected the menu to not be in the document after closing it.');
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Returns the menu's trigger.
|
|
424
|
+
*/
|
|
425
|
+
getTrigger(): HTMLElement {
|
|
426
|
+
if (!this._trigger) {
|
|
427
|
+
throw new Error('No trigger element found for menu.');
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return this._trigger;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Returns the menu if present.
|
|
435
|
+
*/
|
|
436
|
+
getMenu(): HTMLElement | null {
|
|
437
|
+
let menuId = this.getTrigger().getAttribute('aria-controls');
|
|
438
|
+
return menuId ? document.getElementById(menuId) : null;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Returns the menu's sections if any.
|
|
443
|
+
*/
|
|
444
|
+
getSections(): HTMLElement[] {
|
|
445
|
+
let menu = this.getMenu();
|
|
446
|
+
if (menu) {
|
|
447
|
+
return within(menu).queryAllByRole('group');
|
|
448
|
+
} else {
|
|
449
|
+
return [];
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Returns the menu's options if present. Can be filtered to a subsection of the menu if provided
|
|
455
|
+
* via `element`.
|
|
456
|
+
*/
|
|
457
|
+
getOptions(opts: {element?: HTMLElement} = {}): HTMLElement[] {
|
|
458
|
+
let {element = this.getMenu()} = opts;
|
|
459
|
+
if (!element) {
|
|
460
|
+
return [];
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return Array.from(
|
|
464
|
+
element.querySelectorAll('[role=menuitem], [role=menuitemradio], [role=menuitemcheckbox]')
|
|
465
|
+
) as HTMLElement[];
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Returns the menu's submenu triggers if any.
|
|
470
|
+
*/
|
|
471
|
+
getSubmenuTriggers(): HTMLElement[] {
|
|
472
|
+
let options = this.getOptions();
|
|
473
|
+
if (options.length > 0) {
|
|
474
|
+
return options.filter(item => item.getAttribute('aria-haspopup') != null);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return [];
|
|
478
|
+
}
|
|
479
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 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} from './act';
|
|
14
|
+
import {Direction, Orientation, RadioGroupTesterOpts, UserOpts} from './types';
|
|
15
|
+
import {formatTargetNode, pressElement} from './utils';
|
|
16
|
+
import {within} from '@testing-library/dom';
|
|
17
|
+
|
|
18
|
+
interface TriggerRadioOptions {
|
|
19
|
+
/**
|
|
20
|
+
* What interaction type to use when triggering a radio. Defaults to the interaction type set on
|
|
21
|
+
* the tester.
|
|
22
|
+
*/
|
|
23
|
+
interactionType?: UserOpts['interactionType'];
|
|
24
|
+
/**
|
|
25
|
+
* The index, text, or node of the radio to toggle selection for.
|
|
26
|
+
*/
|
|
27
|
+
radio: number | string | HTMLElement;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class RadioGroupTester {
|
|
31
|
+
private user;
|
|
32
|
+
private _interactionType: UserOpts['interactionType'];
|
|
33
|
+
private _radiogroup: HTMLElement;
|
|
34
|
+
private _direction: Direction;
|
|
35
|
+
|
|
36
|
+
constructor(opts: RadioGroupTesterOpts) {
|
|
37
|
+
let {root, user, interactionType, direction} = opts;
|
|
38
|
+
this.user = user;
|
|
39
|
+
this._interactionType = interactionType || 'mouse';
|
|
40
|
+
this._direction = direction || 'ltr';
|
|
41
|
+
|
|
42
|
+
this._radiogroup = root;
|
|
43
|
+
let radiogroup = within(root).queryAllByRole('radiogroup');
|
|
44
|
+
if (radiogroup.length > 0) {
|
|
45
|
+
this._radiogroup = radiogroup[0];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set the interaction type used by the radio tester.
|
|
51
|
+
*/
|
|
52
|
+
setInteractionType(type: UserOpts['interactionType']): void {
|
|
53
|
+
this._interactionType = type;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns a radio matching the specified index or text content.
|
|
58
|
+
*/
|
|
59
|
+
findRadio(opts: {indexOrText: number | string}): HTMLElement {
|
|
60
|
+
let {indexOrText} = opts;
|
|
61
|
+
|
|
62
|
+
let radio;
|
|
63
|
+
if (typeof indexOrText === 'number') {
|
|
64
|
+
radio = this.getRadios()[indexOrText];
|
|
65
|
+
} else if (typeof indexOrText === 'string') {
|
|
66
|
+
let label = within(this.getRadioGroup()).getByText(indexOrText);
|
|
67
|
+
// Label may wrap the radio, or the actual label may be a sibling span, or the radio div could have the label within it
|
|
68
|
+
if (label) {
|
|
69
|
+
radio = within(label).queryByRole('radio');
|
|
70
|
+
if (!radio) {
|
|
71
|
+
let labelWrapper = label.closest('label');
|
|
72
|
+
if (labelWrapper) {
|
|
73
|
+
radio = within(labelWrapper).queryByRole('radio');
|
|
74
|
+
} else {
|
|
75
|
+
radio = label.closest('[role=radio]');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return radio;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private async keyboardNavigateToRadio(opts: {radio: HTMLElement; orientation?: Orientation}) {
|
|
85
|
+
let {radio, orientation = 'vertical'} = opts;
|
|
86
|
+
let radios = this.getRadios();
|
|
87
|
+
radios = radios.filter(
|
|
88
|
+
radio => !(radio.hasAttribute('disabled') || radio.getAttribute('aria-disabled') === 'true')
|
|
89
|
+
);
|
|
90
|
+
if (radios.length === 0) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'Radio group doesnt have any non-disabled radios. Please double check your radio group.'
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let targetIndex = radios.indexOf(radio);
|
|
97
|
+
if (targetIndex === -1) {
|
|
98
|
+
throw new Error('Radio provided is not in the radio group.');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!this.getRadioGroup().contains(document.activeElement)) {
|
|
102
|
+
let selectedRadio = this.getSelectedRadio();
|
|
103
|
+
if (selectedRadio != null) {
|
|
104
|
+
act(() => selectedRadio.focus());
|
|
105
|
+
} else {
|
|
106
|
+
act(() => radios[0]?.focus());
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let currIndex = radios.indexOf(document.activeElement as HTMLElement);
|
|
111
|
+
if (currIndex === -1) {
|
|
112
|
+
throw new Error('Active element is not in the radio group.');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let arrowUp = 'ArrowUp';
|
|
116
|
+
let arrowDown = 'ArrowDown';
|
|
117
|
+
if (orientation === 'horizontal') {
|
|
118
|
+
if (this._direction === 'ltr') {
|
|
119
|
+
arrowUp = 'ArrowLeft';
|
|
120
|
+
arrowDown = 'ArrowRight';
|
|
121
|
+
} else {
|
|
122
|
+
arrowUp = 'ArrowRight';
|
|
123
|
+
arrowDown = 'ArrowLeft';
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let movementDirection = targetIndex > currIndex ? 'down' : 'up';
|
|
128
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
129
|
+
await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Triggers the specified radio. Defaults to using the interaction type set on the radio tester.
|
|
135
|
+
*/
|
|
136
|
+
async triggerRadio(opts: TriggerRadioOptions): Promise<void> {
|
|
137
|
+
let {radio, interactionType = this._interactionType} = opts;
|
|
138
|
+
|
|
139
|
+
if (typeof radio === 'string' || typeof radio === 'number') {
|
|
140
|
+
radio = this.findRadio({indexOrText: radio});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!radio) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Target radio "${formatTargetNode(opts.radio)}" not found in the radio group.`
|
|
146
|
+
);
|
|
147
|
+
} else if (radio.hasAttribute('disabled')) {
|
|
148
|
+
throw new Error(`Target radio "${formatTargetNode(opts.radio)}" is disabled.`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (interactionType === 'keyboard') {
|
|
152
|
+
let radioOrientation = this._radiogroup.getAttribute('aria-orientation') || 'horizontal';
|
|
153
|
+
await this.keyboardNavigateToRadio({radio, orientation: radioOrientation as Orientation});
|
|
154
|
+
} else {
|
|
155
|
+
await pressElement(this.user, radio, interactionType);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Returns the radiogroup.
|
|
161
|
+
*/
|
|
162
|
+
getRadioGroup(): HTMLElement {
|
|
163
|
+
return this._radiogroup;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Returns the radios.
|
|
168
|
+
*/
|
|
169
|
+
getRadios(): HTMLElement[] {
|
|
170
|
+
return within(this.getRadioGroup()).queryAllByRole('radio');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Returns the currently selected radio in the radiogroup if any.
|
|
175
|
+
*/
|
|
176
|
+
getSelectedRadio(): HTMLElement | null {
|
|
177
|
+
return this.getRadios().find(radio => (radio as HTMLInputElement).checked) || null;
|
|
178
|
+
}
|
|
179
|
+
}
|