@react-aria/test-utils 1.0.0-nightly.5042 → 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
|
@@ -0,0 +1,165 @@
|
|
|
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 {CheckboxGroupTesterOpts, UserOpts} from './types';
|
|
15
|
+
import {formatTargetNode, pressElement} from './utils';
|
|
16
|
+
import {within} from '@testing-library/dom';
|
|
17
|
+
|
|
18
|
+
interface TriggerCheckboxOptions {
|
|
19
|
+
/**
|
|
20
|
+
* What interaction type to use when triggering a checkbox. Defaults to the interaction type set
|
|
21
|
+
* on the tester.
|
|
22
|
+
*/
|
|
23
|
+
interactionType?: UserOpts['interactionType'];
|
|
24
|
+
/**
|
|
25
|
+
* The index, text, or node of the checkbox to toggle selection for.
|
|
26
|
+
*/
|
|
27
|
+
checkbox: number | string | HTMLElement;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class CheckboxGroupTester {
|
|
31
|
+
private user;
|
|
32
|
+
private _interactionType: UserOpts['interactionType'];
|
|
33
|
+
private _checkboxgroup: HTMLElement;
|
|
34
|
+
|
|
35
|
+
constructor(opts: CheckboxGroupTesterOpts) {
|
|
36
|
+
let {root, user, interactionType} = opts;
|
|
37
|
+
this.user = user;
|
|
38
|
+
this._interactionType = interactionType || 'mouse';
|
|
39
|
+
|
|
40
|
+
this._checkboxgroup = root;
|
|
41
|
+
let checkboxgroup = within(root).queryAllByRole('group');
|
|
42
|
+
if (checkboxgroup.length > 0) {
|
|
43
|
+
this._checkboxgroup = checkboxgroup[0];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Set the interaction type used by the checkbox group tester.
|
|
49
|
+
*/
|
|
50
|
+
setInteractionType(type: UserOpts['interactionType']): void {
|
|
51
|
+
this._interactionType = type;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Returns a checkbox matching the specified index or text content.
|
|
56
|
+
*/
|
|
57
|
+
findCheckbox(opts: {indexOrText: number | string}): HTMLElement {
|
|
58
|
+
let {indexOrText} = opts;
|
|
59
|
+
|
|
60
|
+
let checkbox;
|
|
61
|
+
if (typeof indexOrText === 'number') {
|
|
62
|
+
checkbox = this.getCheckboxes()[indexOrText];
|
|
63
|
+
} else if (typeof indexOrText === 'string') {
|
|
64
|
+
let label = within(this.getCheckboxGroup()).getByText(indexOrText);
|
|
65
|
+
|
|
66
|
+
// Label may wrap the checkbox, or the actual label may be a sibling span, or the checkbox div could have the label within it
|
|
67
|
+
if (label) {
|
|
68
|
+
checkbox = within(label).queryByRole('checkbox');
|
|
69
|
+
if (!checkbox) {
|
|
70
|
+
let labelWrapper = label.closest('label');
|
|
71
|
+
if (labelWrapper) {
|
|
72
|
+
checkbox = within(labelWrapper).queryByRole('checkbox');
|
|
73
|
+
} else {
|
|
74
|
+
checkbox = label.closest('[role=checkbox]');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return checkbox;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private async keyboardNavigateToCheckbox(opts: {checkbox: HTMLElement}) {
|
|
84
|
+
let {checkbox} = opts;
|
|
85
|
+
let checkboxes = this.getCheckboxes();
|
|
86
|
+
checkboxes = checkboxes.filter(
|
|
87
|
+
checkbox =>
|
|
88
|
+
!(checkbox.hasAttribute('disabled') || checkbox.getAttribute('aria-disabled') === 'true')
|
|
89
|
+
);
|
|
90
|
+
if (checkboxes.length === 0) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'Checkbox group doesnt have any non-disabled checkboxes. Please double check your checkbox group.'
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let targetIndex = checkboxes.indexOf(checkbox);
|
|
97
|
+
if (targetIndex === -1) {
|
|
98
|
+
throw new Error('Checkbox provided is not in the checkbox group.');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!this.getCheckboxGroup().contains(document.activeElement)) {
|
|
102
|
+
act(() => checkboxes[0].focus());
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let currIndex = checkboxes.indexOf(document.activeElement as HTMLElement);
|
|
106
|
+
if (currIndex === -1) {
|
|
107
|
+
throw new Error('Active element is not in the checkbox group.');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
111
|
+
await this.user.tab({shift: targetIndex < currIndex});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Toggles the specified checkbox. Defaults to using the interaction type set on the checkbox
|
|
117
|
+
* tester.
|
|
118
|
+
*/
|
|
119
|
+
async toggleCheckbox(opts: TriggerCheckboxOptions): Promise<void> {
|
|
120
|
+
let {checkbox, interactionType = this._interactionType} = opts;
|
|
121
|
+
|
|
122
|
+
if (typeof checkbox === 'string' || typeof checkbox === 'number') {
|
|
123
|
+
checkbox = this.findCheckbox({indexOrText: checkbox});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!checkbox) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`Target checkbox "${formatTargetNode(opts.checkbox)}" not found in the checkboxgroup.`
|
|
129
|
+
);
|
|
130
|
+
} else if (checkbox.hasAttribute('disabled')) {
|
|
131
|
+
throw new Error(`Target checkbox "${formatTargetNode(opts.checkbox)}" is disabled.`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (interactionType === 'keyboard') {
|
|
135
|
+
await this.keyboardNavigateToCheckbox({checkbox});
|
|
136
|
+
await this.user.keyboard('[Space]');
|
|
137
|
+
} else {
|
|
138
|
+
await pressElement(this.user, checkbox, interactionType);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Returns the checkboxgroup.
|
|
144
|
+
*/
|
|
145
|
+
getCheckboxGroup(): HTMLElement {
|
|
146
|
+
return this._checkboxgroup;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Returns the checkboxes.
|
|
151
|
+
*/
|
|
152
|
+
getCheckboxes(): HTMLElement[] {
|
|
153
|
+
return within(this.getCheckboxGroup()).queryAllByRole('checkbox');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Returns the currently selected checkboxes in the checkboxgroup if any.
|
|
158
|
+
*/
|
|
159
|
+
getSelectedCheckboxes(): HTMLElement[] {
|
|
160
|
+
return this.getCheckboxes().filter(
|
|
161
|
+
checkbox =>
|
|
162
|
+
(checkbox as HTMLInputElement).checked || checkbox.getAttribute('aria-checked') === 'true'
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
package/src/combobox.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
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 {ComboBoxTesterOpts, UserOpts} from './types';
|
|
15
|
+
import {formatTargetNode} from './utils';
|
|
16
|
+
import {waitFor, within} from '@testing-library/dom';
|
|
17
|
+
|
|
18
|
+
interface ComboBoxOpenOpts {
|
|
19
|
+
/**
|
|
20
|
+
* Whether the combobox opens on focus or needs to be manually opened via user action.
|
|
21
|
+
*
|
|
22
|
+
* @default 'manual'
|
|
23
|
+
*/
|
|
24
|
+
triggerBehavior?: 'focus' | 'manual';
|
|
25
|
+
/**
|
|
26
|
+
* What interaction type to use when opening the combobox. Defaults to the interaction type set on
|
|
27
|
+
* the tester.
|
|
28
|
+
*/
|
|
29
|
+
interactionType?: UserOpts['interactionType'];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ComboBoxSelectOpts extends ComboBoxOpenOpts {
|
|
33
|
+
/**
|
|
34
|
+
* The index, text, or node of the option to select. Option nodes can be sourced via
|
|
35
|
+
* `getOptions()`.
|
|
36
|
+
*/
|
|
37
|
+
option: number | string | HTMLElement;
|
|
38
|
+
/**
|
|
39
|
+
* Whether or not the combobox closes on selection. Defaults to `true` for single select
|
|
40
|
+
* comboboxes and `false` for multi-select comboboxes.
|
|
41
|
+
*/
|
|
42
|
+
closesOnSelect?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class ComboBoxTester {
|
|
46
|
+
private user;
|
|
47
|
+
private _interactionType: UserOpts['interactionType'];
|
|
48
|
+
private _combobox: HTMLElement;
|
|
49
|
+
private _trigger: HTMLElement;
|
|
50
|
+
|
|
51
|
+
constructor(opts: ComboBoxTesterOpts) {
|
|
52
|
+
let {root, trigger, user, interactionType} = opts;
|
|
53
|
+
this.user = user;
|
|
54
|
+
this._interactionType = interactionType || 'mouse';
|
|
55
|
+
|
|
56
|
+
// 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
|
|
57
|
+
// query their combobox/combobox wrapper (in the case of RSP) which they then pass to thhis
|
|
58
|
+
this._combobox = root;
|
|
59
|
+
let combobox = within(root).queryByRole('combobox');
|
|
60
|
+
if (combobox) {
|
|
61
|
+
this._combobox = combobox;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 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)
|
|
65
|
+
if (trigger) {
|
|
66
|
+
this._trigger = trigger;
|
|
67
|
+
} else {
|
|
68
|
+
let buttons = within(root).queryAllByRole('button', {hidden: true});
|
|
69
|
+
|
|
70
|
+
if (buttons.length === 1) {
|
|
71
|
+
trigger = buttons[0];
|
|
72
|
+
} else if (buttons.length > 1) {
|
|
73
|
+
trigger = buttons.find(button => button.hasAttribute('aria-haspopup'));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// For cases like https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/ where the combobox
|
|
77
|
+
// is also the trigger button
|
|
78
|
+
this._trigger = trigger || this._combobox;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Set the interaction type used by the combobox tester.
|
|
84
|
+
*/
|
|
85
|
+
setInteractionType(type: UserOpts['interactionType']): void {
|
|
86
|
+
this._interactionType = type;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Opens the combobox dropdown. Defaults to using the interaction type set on the combobox tester.
|
|
91
|
+
*/
|
|
92
|
+
async open(opts: ComboBoxOpenOpts = {}): Promise<void> {
|
|
93
|
+
let {triggerBehavior = 'manual', interactionType = this._interactionType} = opts;
|
|
94
|
+
let trigger = this.getTrigger();
|
|
95
|
+
let combobox = this.getCombobox();
|
|
96
|
+
let isDisabled = trigger!.hasAttribute('disabled');
|
|
97
|
+
|
|
98
|
+
if (interactionType === 'mouse') {
|
|
99
|
+
if (triggerBehavior === 'focus') {
|
|
100
|
+
await this.user.click(combobox);
|
|
101
|
+
} else {
|
|
102
|
+
await this.user.click(trigger);
|
|
103
|
+
}
|
|
104
|
+
} else if (interactionType === 'keyboard') {
|
|
105
|
+
act(() => combobox.focus());
|
|
106
|
+
if (triggerBehavior !== 'focus') {
|
|
107
|
+
await this.user.keyboard('{ArrowDown}');
|
|
108
|
+
}
|
|
109
|
+
} else if (interactionType === 'touch') {
|
|
110
|
+
if (triggerBehavior === 'focus') {
|
|
111
|
+
await this.user.pointer({target: combobox, keys: '[TouchA]'});
|
|
112
|
+
} else {
|
|
113
|
+
await this.user.pointer({target: trigger, keys: '[TouchA]'});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
await waitFor(() => {
|
|
118
|
+
if (!isDisabled && combobox.getAttribute('aria-controls') == null) {
|
|
119
|
+
throw new Error('No aria-controls found on combobox trigger element.');
|
|
120
|
+
} else {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
let listBoxId = combobox.getAttribute('aria-controls');
|
|
125
|
+
await waitFor(() => {
|
|
126
|
+
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {
|
|
127
|
+
throw new Error(`Listbox with id of ${listBoxId} not found in document.`);
|
|
128
|
+
} else {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Returns an option matching the specified index or text content.
|
|
136
|
+
*/
|
|
137
|
+
findOption(opts: {indexOrText: number | string}): HTMLElement {
|
|
138
|
+
let {indexOrText} = opts;
|
|
139
|
+
|
|
140
|
+
let option;
|
|
141
|
+
let options = this.getOptions();
|
|
142
|
+
let listbox = this.getListbox();
|
|
143
|
+
|
|
144
|
+
if (typeof indexOrText === 'number') {
|
|
145
|
+
option = options[indexOrText];
|
|
146
|
+
} else if (typeof indexOrText === 'string' && listbox != null) {
|
|
147
|
+
option = within(listbox!).getByText(indexOrText).closest('[role=option]')! as HTMLElement;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return option;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async keyboardNavigateToOption(opts: {option: HTMLElement}) {
|
|
154
|
+
let {option} = opts;
|
|
155
|
+
let combobox = this.getCombobox();
|
|
156
|
+
let options = this.getOptions();
|
|
157
|
+
let targetIndex = options.findIndex(opt => opt === option || opt.contains(option));
|
|
158
|
+
if (targetIndex === -1) {
|
|
159
|
+
throw new Error('Option provided is not in the combobox listbox.');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let getCurrentIndex = () => {
|
|
163
|
+
let id = combobox.getAttribute('aria-activedescendant');
|
|
164
|
+
if (!id) {
|
|
165
|
+
return -1;
|
|
166
|
+
}
|
|
167
|
+
return options.findIndex(opt => opt.id === id);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
if (getCurrentIndex() === -1) {
|
|
171
|
+
await this.user.keyboard('[ArrowDown]');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
let currIndex = getCurrentIndex();
|
|
175
|
+
if (currIndex === -1) {
|
|
176
|
+
throw new Error('Could not determine the current option in the combobox listbox.');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
180
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
181
|
+
await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Toggles the selection of the desired combobox option if possible. Defaults to using the
|
|
187
|
+
* interaction type set on the combobox tester. If necessary, will open the combobox dropdown
|
|
188
|
+
* beforehand. The desired option can be targeted via the option's node, the option's text, or the
|
|
189
|
+
* option's index.
|
|
190
|
+
*/
|
|
191
|
+
async toggleOptionSelection(opts: ComboBoxSelectOpts): Promise<void> {
|
|
192
|
+
let {option, triggerBehavior, interactionType = this._interactionType, closesOnSelect} = opts;
|
|
193
|
+
if (!this.getCombobox().getAttribute('aria-controls')) {
|
|
194
|
+
await this.open({triggerBehavior});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let listbox = this.getListbox();
|
|
198
|
+
if (!listbox) {
|
|
199
|
+
throw new Error("Combobox's listbox not found.");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (typeof option === 'string' || typeof option === 'number') {
|
|
203
|
+
option = this.findOption({indexOrText: option});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!option) {
|
|
207
|
+
throw new Error(`Target option "${formatTargetNode(opts.option)}" not found in the listbox.`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let isMultiSelect = listbox.getAttribute('aria-multiselectable') === 'true';
|
|
211
|
+
closesOnSelect = closesOnSelect ?? !isMultiSelect;
|
|
212
|
+
|
|
213
|
+
if (interactionType === 'keyboard') {
|
|
214
|
+
await this.keyboardNavigateToOption({option});
|
|
215
|
+
await this.user.keyboard('[Enter]');
|
|
216
|
+
} else if (interactionType === 'mouse') {
|
|
217
|
+
await this.user.click(option);
|
|
218
|
+
} else {
|
|
219
|
+
await this.user.pointer({target: option, keys: '[TouchA]'});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (closesOnSelect && option.getAttribute('href') == null) {
|
|
223
|
+
await waitFor(() => {
|
|
224
|
+
if (document.contains(listbox)) {
|
|
225
|
+
throw new Error(
|
|
226
|
+
'Expected listbox element to not be in the document after selecting an option'
|
|
227
|
+
);
|
|
228
|
+
} else {
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Closes the combobox dropdown.
|
|
237
|
+
*/
|
|
238
|
+
async close(): Promise<void> {
|
|
239
|
+
let listbox = this.getListbox();
|
|
240
|
+
if (listbox) {
|
|
241
|
+
act(() => this.getCombobox().focus());
|
|
242
|
+
await this.user.keyboard('[Escape]');
|
|
243
|
+
|
|
244
|
+
await waitFor(() => {
|
|
245
|
+
if (document.contains(listbox)) {
|
|
246
|
+
throw new Error(
|
|
247
|
+
'Expected listbox element to not be in the document after selecting an option'
|
|
248
|
+
);
|
|
249
|
+
} else {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Returns the combobox.
|
|
258
|
+
*/
|
|
259
|
+
getCombobox(): HTMLElement {
|
|
260
|
+
return this._combobox;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Returns the combobox trigger button.
|
|
265
|
+
*/
|
|
266
|
+
getTrigger(): HTMLElement {
|
|
267
|
+
return this._trigger;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Returns the combobox's listbox if present.
|
|
272
|
+
*/
|
|
273
|
+
getListbox(): HTMLElement | null {
|
|
274
|
+
let listBoxId = this.getCombobox().getAttribute('aria-controls');
|
|
275
|
+
return listBoxId ? document.getElementById(listBoxId) || null : null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Returns the combobox's sections if present.
|
|
280
|
+
*/
|
|
281
|
+
getSections(): HTMLElement[] {
|
|
282
|
+
let listbox = this.getListbox();
|
|
283
|
+
return listbox ? within(listbox).queryAllByRole('group') : [];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Returns the combobox's options if present. Can be filtered to a subsection of the listbox if
|
|
288
|
+
* provided via `element`.
|
|
289
|
+
*/
|
|
290
|
+
getOptions(opts: {element?: HTMLElement} = {}): HTMLElement[] {
|
|
291
|
+
let {element = this.getListbox()} = opts;
|
|
292
|
+
let options = [];
|
|
293
|
+
if (element) {
|
|
294
|
+
options = within(element).queryAllByRole('option');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return options;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Returns the currently focused option in the combobox's dropdown if any.
|
|
302
|
+
*/
|
|
303
|
+
getFocusedOption(): HTMLElement | null {
|
|
304
|
+
let focusedOptionId = this.getCombobox().getAttribute('aria-activedescendant');
|
|
305
|
+
return focusedOptionId ? document.getElementById(focusedOptionId) : null;
|
|
306
|
+
}
|
|
307
|
+
}
|
package/src/dialog.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
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 {DialogTesterOpts, UserOpts} from './types';
|
|
15
|
+
import {waitFor, within} from '@testing-library/dom';
|
|
16
|
+
|
|
17
|
+
interface DialogOpenOpts {
|
|
18
|
+
/**
|
|
19
|
+
* What interaction type to use when opening the dialog. Defaults to the interaction type set on
|
|
20
|
+
* the tester.
|
|
21
|
+
*/
|
|
22
|
+
interactionType?: UserOpts['interactionType'];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class DialogTester {
|
|
26
|
+
private user;
|
|
27
|
+
private _interactionType: UserOpts['interactionType'];
|
|
28
|
+
private _trigger: HTMLElement | undefined;
|
|
29
|
+
private _dialog: HTMLElement | undefined;
|
|
30
|
+
private _overlayType: DialogTesterOpts['overlayType'];
|
|
31
|
+
|
|
32
|
+
constructor(opts: DialogTesterOpts) {
|
|
33
|
+
let {root, user, interactionType, overlayType} = opts;
|
|
34
|
+
this.user = user;
|
|
35
|
+
this._interactionType = interactionType || 'mouse';
|
|
36
|
+
this._overlayType = overlayType || 'modal';
|
|
37
|
+
|
|
38
|
+
// Handle case where element provided is a wrapper of the trigger button.
|
|
39
|
+
let buttons = within(root).queryAllByRole('button');
|
|
40
|
+
let triggerButton: HTMLElement | undefined;
|
|
41
|
+
if (buttons.length === 0) {
|
|
42
|
+
triggerButton = root;
|
|
43
|
+
} else if (buttons.length === 1) {
|
|
44
|
+
triggerButton = buttons[0];
|
|
45
|
+
} else {
|
|
46
|
+
triggerButton = buttons.find(button => button.hasAttribute('aria-haspopup'));
|
|
47
|
+
}
|
|
48
|
+
this._trigger = triggerButton ?? root;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Set the interaction type used by the dialog tester.
|
|
53
|
+
*/
|
|
54
|
+
setInteractionType(type: UserOpts['interactionType']): void {
|
|
55
|
+
this._interactionType = type;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Opens the dialog. Defaults to using the interaction type set on the dialog tester.
|
|
60
|
+
*/
|
|
61
|
+
async open(opts: DialogOpenOpts = {}): Promise<void> {
|
|
62
|
+
let {interactionType = this._interactionType} = opts;
|
|
63
|
+
let trigger = this.getTrigger();
|
|
64
|
+
if (!trigger.hasAttribute('disabled')) {
|
|
65
|
+
if (interactionType === 'mouse') {
|
|
66
|
+
await this.user.click(trigger);
|
|
67
|
+
} else if (interactionType === 'touch') {
|
|
68
|
+
await this.user.pointer({target: trigger, keys: '[TouchA]'});
|
|
69
|
+
} else if (interactionType === 'keyboard') {
|
|
70
|
+
act(() => trigger.focus());
|
|
71
|
+
await this.user.keyboard('[Enter]');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (this._overlayType === 'popover') {
|
|
75
|
+
await waitFor(() => {
|
|
76
|
+
if (trigger.getAttribute('aria-controls') == null) {
|
|
77
|
+
throw new Error('No aria-controls found on dialog trigger element.');
|
|
78
|
+
} else {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
let dialogId = trigger.getAttribute('aria-controls');
|
|
84
|
+
await waitFor(() => {
|
|
85
|
+
if (!dialogId || document.getElementById(dialogId) == null) {
|
|
86
|
+
throw new Error(`Dialog with id of ${dialogId} not found in document.`);
|
|
87
|
+
} else {
|
|
88
|
+
this._dialog = document.getElementById(dialogId)!;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
} else {
|
|
93
|
+
let dialog;
|
|
94
|
+
await waitFor(() => {
|
|
95
|
+
dialog = document.querySelector('[role=dialog], [role=alertdialog]');
|
|
96
|
+
if (dialog == null) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
'No dialog of type role="dialog" or role="alertdialog" found after pressing the trigger.'
|
|
99
|
+
);
|
|
100
|
+
} else {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
if (
|
|
106
|
+
dialog &&
|
|
107
|
+
document.activeElement !== this._trigger &&
|
|
108
|
+
dialog.contains(document.activeElement)
|
|
109
|
+
) {
|
|
110
|
+
this._dialog = dialog;
|
|
111
|
+
} else {
|
|
112
|
+
throw new Error(
|
|
113
|
+
'New modal dialog doesnt contain the active element OR the active element is still the trigger. Uncertain if the proper modal dialog was found'
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Closes the dialog via the Escape key.
|
|
122
|
+
*/
|
|
123
|
+
async close(): Promise<void> {
|
|
124
|
+
let dialog = this._dialog;
|
|
125
|
+
if (dialog) {
|
|
126
|
+
await this.user.keyboard('[Escape]');
|
|
127
|
+
await waitFor(() => {
|
|
128
|
+
if (document.contains(dialog)) {
|
|
129
|
+
throw new Error('Expected the dialog to not be in the document after closing it.');
|
|
130
|
+
} else {
|
|
131
|
+
this._dialog = undefined;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Returns the dialog's trigger.
|
|
140
|
+
*/
|
|
141
|
+
getTrigger(): HTMLElement {
|
|
142
|
+
if (!this._trigger) {
|
|
143
|
+
throw new Error('No trigger element found for dialog.');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return this._trigger;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Returns the dialog if present.
|
|
151
|
+
*/
|
|
152
|
+
getDialog(): HTMLElement | null {
|
|
153
|
+
return this._dialog && document.contains(this._dialog) ? this._dialog : null;
|
|
154
|
+
}
|
|
155
|
+
}
|