@react-aria/test-utils 1.0.0-alpha.1 → 1.0.0-alpha.2
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/dist/combobox.main.js +137 -0
- package/dist/combobox.main.js.map +1 -0
- package/dist/combobox.mjs +132 -0
- package/dist/combobox.module.js +132 -0
- package/dist/combobox.module.js.map +1 -0
- package/dist/events.main.js +25 -10
- package/dist/events.main.js.map +1 -1
- package/dist/events.mjs +25 -10
- package/dist/events.module.js +25 -10
- package/dist/events.module.js.map +1 -1
- package/dist/gridlist.main.js +97 -0
- package/dist/gridlist.main.js.map +1 -0
- package/dist/gridlist.mjs +92 -0
- package/dist/gridlist.module.js +92 -0
- package/dist/gridlist.module.js.map +1 -0
- package/dist/import.mjs +4 -2
- package/dist/main.js +10 -18
- package/dist/main.js.map +1 -1
- package/dist/menu.main.js +176 -0
- package/dist/menu.main.js.map +1 -0
- package/dist/menu.mjs +171 -0
- package/dist/menu.module.js +171 -0
- package/dist/menu.module.js.map +1 -0
- package/dist/module.js +4 -2
- package/dist/module.js.map +1 -1
- package/dist/select.main.js +113 -0
- package/dist/select.main.js.map +1 -0
- package/dist/select.mjs +108 -0
- package/dist/select.module.js +108 -0
- package/dist/select.module.js.map +1 -0
- package/dist/table.main.js +191 -0
- package/dist/table.main.js.map +1 -0
- package/dist/table.mjs +186 -0
- package/dist/table.module.js +186 -0
- package/dist/table.module.js.map +1 -0
- package/dist/types.d.ts +180 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/user.main.js +65 -0
- package/dist/user.main.js.map +1 -0
- package/dist/user.mjs +56 -0
- package/dist/user.module.js +56 -0
- package/dist/user.module.js.map +1 -0
- package/package.json +6 -7
- package/src/combobox.ts +188 -0
- package/src/events.ts +28 -8
- package/src/gridlist.ts +116 -0
- package/src/index.ts +6 -3
- package/src/menu.ts +246 -0
- package/src/select.ts +158 -0
- package/src/table.ts +282 -0
- package/src/user.ts +73 -0
- package/LICENSE +0 -201
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/test-utils",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "Testing utils for react-aria patterns",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -25,13 +25,12 @@
|
|
|
25
25
|
"@swc/helpers": "^0.5.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@testing-library/react": "^
|
|
28
|
+
"@testing-library/react": "^15.0.7",
|
|
29
29
|
"@testing-library/user-event": "^13.0.0 || ^14.0.0",
|
|
30
|
-
"jest": "^
|
|
31
|
-
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
|
|
30
|
+
"jest": "^29.5.0",
|
|
31
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
}
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/combobox.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
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
|
+
}
|
package/src/events.ts
CHANGED
|
@@ -11,18 +11,38 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {act, fireEvent} from '@testing-library/react';
|
|
14
|
+
import {UserOpts} from './user';
|
|
14
15
|
|
|
15
16
|
export const DEFAULT_LONG_PRESS_TIME = 500;
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Simulates a "long press" event on a element.
|
|
19
|
-
* @param
|
|
20
|
-
* @param opts -
|
|
20
|
+
* @param opts - Options for the long press.
|
|
21
|
+
* @param opts.element - Element to long press.
|
|
22
|
+
* @param opts.advanceTimer - Function that when called advances the timers in your test suite by a specific amount of time(ms).
|
|
23
|
+
* @param opts.pointeropts - Options to pass to the simulated event. Defaults to mouse. See https://testing-library.com/docs/dom-testing-library/api-events/#fireevent for more info.
|
|
21
24
|
*/
|
|
22
|
-
export function triggerLongPress(element: HTMLElement,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
fireEvent.
|
|
25
|
+
export async function triggerLongPress(opts: {element: HTMLElement, advanceTimer: (time?: number) => void | Promise<unknown>, pointerOpts?: {}}) {
|
|
26
|
+
// TODO: note that this only works if the code from installPointerEvent is called somewhere in the test BEFORE the
|
|
27
|
+
// render. Perhaps we should rely on the user setting that up since I'm not sure there is a great way to set that up here in the
|
|
28
|
+
// util before first render. Will need to document it well
|
|
29
|
+
let {element, advanceTimer, pointerOpts = {}} = opts;
|
|
30
|
+
await fireEvent.pointerDown(element, {pointerType: 'mouse', ...pointerOpts});
|
|
31
|
+
await act(async () => await advanceTimer(DEFAULT_LONG_PRESS_TIME));
|
|
32
|
+
await fireEvent.pointerUp(element, {pointerType: 'mouse', ...pointerOpts});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export async function pressElement(user, element: HTMLElement, interactionType: UserOpts['interactionType']) {
|
|
37
|
+
if (interactionType === 'mouse') {
|
|
38
|
+
await user.click(element);
|
|
39
|
+
} else if (interactionType === 'keyboard') {
|
|
40
|
+
// TODO: For the keyboard flow, I wonder if it would be reasonable to just do fireEvent directly on the obtained row node or if we should
|
|
41
|
+
// stick to simulting an actual user's keyboard operations as closely as possible
|
|
42
|
+
// There are problems when using this approach though, actions like trying to trigger the select all checkbox and stuff behave oddly.
|
|
43
|
+
act(() => element.focus());
|
|
44
|
+
await user.keyboard('[Space]');
|
|
45
|
+
} else if (interactionType === 'touch') {
|
|
46
|
+
await user.pointer({target: element, keys: '[TouchA]'});
|
|
47
|
+
}
|
|
28
48
|
}
|
package/src/gridlist.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
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, within} from '@testing-library/react';
|
|
14
|
+
import {BaseTesterOpts, UserOpts} from './user';
|
|
15
|
+
import {pressElement} from './events';
|
|
16
|
+
|
|
17
|
+
export interface GridListOptions extends UserOpts, BaseTesterOpts {
|
|
18
|
+
user: any
|
|
19
|
+
}
|
|
20
|
+
export class GridListTester {
|
|
21
|
+
private user;
|
|
22
|
+
private _interactionType: UserOpts['interactionType'];
|
|
23
|
+
private _gridlist: HTMLElement;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
constructor(opts: GridListOptions) {
|
|
27
|
+
let {root, user, interactionType} = opts;
|
|
28
|
+
this.user = user;
|
|
29
|
+
this._interactionType = interactionType || 'mouse';
|
|
30
|
+
this._gridlist = root;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setInteractionType = (type: UserOpts['interactionType']) => {
|
|
34
|
+
this._interactionType = type;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// TODO: support long press? This is also pretty much the same as table's toggleRowSelection so maybe can share
|
|
38
|
+
// For now, don't include long press, see if people need it or if we should just expose long press as a separate util if it isn't very common
|
|
39
|
+
// If the current way of passing in the user specified advance timers is ok, then I'd be find including long press
|
|
40
|
+
// Maybe also support an option to force the click to happen on a specific part of the element (checkbox or row). That way
|
|
41
|
+
// the user can test a specific type of interaction?
|
|
42
|
+
toggleRowSelection = async (opts: {index?: number, text?: string, interactionType?: UserOpts['interactionType']} = {}) => {
|
|
43
|
+
let {index, text, interactionType = this._interactionType} = opts;
|
|
44
|
+
|
|
45
|
+
let row = this.findRow({index, text});
|
|
46
|
+
let rowCheckbox = within(row).queryByRole('checkbox');
|
|
47
|
+
if (rowCheckbox) {
|
|
48
|
+
await pressElement(this.user, rowCheckbox, interactionType);
|
|
49
|
+
} else {
|
|
50
|
+
let cell = within(row).getAllByRole('gridcell')[0];
|
|
51
|
+
await pressElement(this.user, cell, interactionType);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// TODO: pretty much the same as table except it uses this.gridlist. Make common between the two by accepting an option for
|
|
56
|
+
// an element?
|
|
57
|
+
findRow = (opts: {index?: number, text?: string}) => {
|
|
58
|
+
let {
|
|
59
|
+
index,
|
|
60
|
+
text
|
|
61
|
+
} = opts;
|
|
62
|
+
|
|
63
|
+
let row;
|
|
64
|
+
if (index != null) {
|
|
65
|
+
row = this.rows[index];
|
|
66
|
+
} else if (text != null) {
|
|
67
|
+
row = within(this?.gridlist).getByText(text);
|
|
68
|
+
while (row && row.getAttribute('role') !== 'row') {
|
|
69
|
+
row = row.parentElement;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return row;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// TODO: There is a more difficult use case where the row has/behaves as link, don't think we have a good way to determine that unless the
|
|
77
|
+
// user specificlly tells us
|
|
78
|
+
triggerRowAction = async (opts: {index?: number, text?: string, needsDoubleClick?: boolean, interactionType?: UserOpts['interactionType']}) => {
|
|
79
|
+
let {
|
|
80
|
+
index,
|
|
81
|
+
text,
|
|
82
|
+
needsDoubleClick,
|
|
83
|
+
interactionType = this._interactionType
|
|
84
|
+
} = opts;
|
|
85
|
+
|
|
86
|
+
let row = this.findRow({index, text});
|
|
87
|
+
if (row) {
|
|
88
|
+
if (needsDoubleClick) {
|
|
89
|
+
await this.user.dblClick(row);
|
|
90
|
+
} else if (interactionType === 'keyboard') {
|
|
91
|
+
act(() => row.focus());
|
|
92
|
+
await this.user.keyboard('[Enter]');
|
|
93
|
+
} else {
|
|
94
|
+
await pressElement(this.user, row, interactionType);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// TODO: do we really need this getter? Theoretically the user already has the reference to the gridlist
|
|
100
|
+
get gridlist() {
|
|
101
|
+
return this._gridlist;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get rows() {
|
|
105
|
+
return within(this?.gridlist).queryAllByRole('row');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get selectedRows() {
|
|
109
|
+
return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
cells = (opts: {element?: HTMLElement} = {}) => {
|
|
113
|
+
let {element} = opts;
|
|
114
|
+
return within(element || this.gridlist).queryAllByRole('gridcell');
|
|
115
|
+
};
|
|
116
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
13
|
+
export {triggerLongPress} from './events';
|
|
14
|
+
export {installMouseEvent, installPointerEvent} from './testSetup';
|
|
15
|
+
export {pointerMap} from './userEventMaps';
|
|
16
|
+
export {User} from './user';
|
|
17
|
+
|
|
18
|
+
export type {UserOpts} from './user';
|
package/src/menu.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
import {triggerLongPress} from './events';
|
|
16
|
+
|
|
17
|
+
export interface MenuOptions extends UserOpts, BaseTesterOpts {
|
|
18
|
+
user: any
|
|
19
|
+
}
|
|
20
|
+
export class MenuTester {
|
|
21
|
+
private user;
|
|
22
|
+
private _interactionType: UserOpts['interactionType'];
|
|
23
|
+
private _advanceTimer: UserOpts['advanceTimer'];
|
|
24
|
+
private _trigger: HTMLElement;
|
|
25
|
+
|
|
26
|
+
constructor(opts: MenuOptions) {
|
|
27
|
+
let {root, user, interactionType, advanceTimer} = opts;
|
|
28
|
+
this.user = user;
|
|
29
|
+
this._interactionType = interactionType || 'mouse';
|
|
30
|
+
this._advanceTimer = advanceTimer;
|
|
31
|
+
|
|
32
|
+
// Handle case where a submenu trigger is provided to the tester
|
|
33
|
+
if (root.getAttribute('role') === 'menuitem') {
|
|
34
|
+
this._trigger = root;
|
|
35
|
+
} else {
|
|
36
|
+
// Handle case where element provided is a wrapper of the trigger button
|
|
37
|
+
let trigger = within(root).queryByRole('button');
|
|
38
|
+
if (trigger) {
|
|
39
|
+
this._trigger = trigger;
|
|
40
|
+
} else {
|
|
41
|
+
this._trigger = root;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
setInteractionType = (type: UserOpts['interactionType']) => {
|
|
47
|
+
this._interactionType = type;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// TODO: this has been common to select as well, maybe make select use it? Or make a generic method. Will need to make error messages generic
|
|
51
|
+
// One difference will be that it supports long press as well
|
|
52
|
+
open = async (opts: {needsLongPress?: boolean, interactionType?: UserOpts['interactionType']} = {}) => {
|
|
53
|
+
let {
|
|
54
|
+
needsLongPress,
|
|
55
|
+
interactionType = this._interactionType
|
|
56
|
+
} = opts;
|
|
57
|
+
|
|
58
|
+
let trigger = this.trigger;
|
|
59
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
60
|
+
if (interactionType === 'mouse' || interactionType === 'touch') {
|
|
61
|
+
if (needsLongPress) {
|
|
62
|
+
if (this._advanceTimer == null) {
|
|
63
|
+
throw new Error('No advanceTimers provided for long press.');
|
|
64
|
+
}
|
|
65
|
+
let pointerType = interactionType === 'mouse' ? 'mouse' : 'touch';
|
|
66
|
+
await triggerLongPress({element: trigger, advanceTimer: this._advanceTimer, pointerOpts: {pointerType}});
|
|
67
|
+
} else if (interactionType === 'mouse') {
|
|
68
|
+
await this.user.click(trigger);
|
|
69
|
+
} else {
|
|
70
|
+
await this.user.pointer({target: trigger, keys: '[TouchA]'});
|
|
71
|
+
}
|
|
72
|
+
} else if (interactionType === 'keyboard' && !isDisabled) {
|
|
73
|
+
act(() => trigger.focus());
|
|
74
|
+
await this.user.keyboard('[Enter]');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
await waitFor(() => {
|
|
78
|
+
if (trigger.getAttribute('aria-controls') == null && !isDisabled) {
|
|
79
|
+
throw new Error('No aria-controls found on menu trigger element.');
|
|
80
|
+
} else {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
if (!isDisabled) {
|
|
85
|
+
let menuId = trigger.getAttribute('aria-controls');
|
|
86
|
+
await waitFor(() => {
|
|
87
|
+
if (!menuId || document.getElementById(menuId) == null) {
|
|
88
|
+
throw new Error(`Menu with id of ${menuId} not found in document.`);
|
|
89
|
+
} else {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// TODO: also very similar to select, barring potential long press support
|
|
97
|
+
// Close on select is also kinda specific?
|
|
98
|
+
selectOption = async (opts: {option?: HTMLElement, optionText?: string, menuSelectionMode?: 'single' | 'multiple', needsLongPress?: boolean, closesOnSelect?: boolean, interactionType?: UserOpts['interactionType']}) => {
|
|
99
|
+
let {
|
|
100
|
+
optionText,
|
|
101
|
+
menuSelectionMode = 'single',
|
|
102
|
+
needsLongPress,
|
|
103
|
+
closesOnSelect = true,
|
|
104
|
+
option,
|
|
105
|
+
interactionType = this._interactionType
|
|
106
|
+
} = opts;
|
|
107
|
+
let trigger = this.trigger;
|
|
108
|
+
if (!trigger.getAttribute('aria-controls')) {
|
|
109
|
+
await this.open({needsLongPress});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let menu = this.menu;
|
|
113
|
+
if (menu) {
|
|
114
|
+
if (!option && optionText) {
|
|
115
|
+
option = within(menu).getByText(optionText);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (interactionType === 'keyboard') {
|
|
119
|
+
if (document.activeElement !== menu || !menu.contains(document.activeElement)) {
|
|
120
|
+
act(() => menu.focus());
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
await this.user.keyboard(optionText);
|
|
124
|
+
await this.user.keyboard('[Enter]');
|
|
125
|
+
} else {
|
|
126
|
+
if (interactionType === 'mouse') {
|
|
127
|
+
await this.user.click(option);
|
|
128
|
+
} else {
|
|
129
|
+
await this.user.pointer({target: option, keys: '[TouchA]'});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (option && option.getAttribute('href') == null && option.getAttribute('aria-haspopup') == null && menuSelectionMode === 'single' && closesOnSelect) {
|
|
134
|
+
await waitFor(() => {
|
|
135
|
+
if (document.activeElement !== trigger) {
|
|
136
|
+
throw new Error(`Expected the document.activeElement after selecting an option to be the menu trigger but got ${document.activeElement}`);
|
|
137
|
+
} else {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
if (document.contains(menu)) {
|
|
143
|
+
throw new Error('Expected menu element to not be in the document after selecting an option');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
throw new Error("Attempted to select a option in the menu, but menu wasn't found.");
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// TODO: update this to remove needsLongPress if we wanna make the user call open first always
|
|
152
|
+
openSubmenu = async (opts: {submenuTrigger?: HTMLElement, submenuTriggerText?: string, needsLongPress?: boolean, interactionType?: UserOpts['interactionType']}): Promise<MenuTester | null> => {
|
|
153
|
+
let {
|
|
154
|
+
submenuTrigger,
|
|
155
|
+
submenuTriggerText,
|
|
156
|
+
needsLongPress,
|
|
157
|
+
interactionType = this._interactionType
|
|
158
|
+
} = opts;
|
|
159
|
+
let trigger = this.trigger;
|
|
160
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
161
|
+
if (!trigger.getAttribute('aria-controls') && !isDisabled) {
|
|
162
|
+
await this.open({needsLongPress});
|
|
163
|
+
}
|
|
164
|
+
if (!isDisabled) {
|
|
165
|
+
let menu = this.menu;
|
|
166
|
+
if (menu) {
|
|
167
|
+
let submenu;
|
|
168
|
+
if (submenuTrigger) {
|
|
169
|
+
submenu = submenuTrigger;
|
|
170
|
+
} else if (submenuTriggerText) {
|
|
171
|
+
submenu = within(menu).getByText(submenuTriggerText);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
let submenuTriggerTester = new MenuTester({user: this.user, interactionType: interactionType, root: submenu});
|
|
175
|
+
await submenuTriggerTester.open();
|
|
176
|
+
|
|
177
|
+
return submenuTriggerTester;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return null;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
close = async () => {
|
|
185
|
+
let menu = this.menu;
|
|
186
|
+
if (menu) {
|
|
187
|
+
act(() => menu.focus());
|
|
188
|
+
await this.user.keyboard('[Escape]');
|
|
189
|
+
|
|
190
|
+
await waitFor(() => {
|
|
191
|
+
if (document.activeElement !== this.trigger) {
|
|
192
|
+
throw new Error(`Expected the document.activeElement after closing the menu to be the menu trigger but got ${document.activeElement}`);
|
|
193
|
+
} else {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
if (document.contains(menu)) {
|
|
199
|
+
throw new Error('Expected the menu to not be in the document after closing it.');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
get trigger() {
|
|
205
|
+
return this._trigger;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
get menu() {
|
|
209
|
+
let menuId = this.trigger.getAttribute('aria-controls');
|
|
210
|
+
return menuId ? document.getElementById(menuId) : undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
get options(): HTMLElement[] | never[] {
|
|
214
|
+
let menu = this.menu;
|
|
215
|
+
let options = [];
|
|
216
|
+
if (menu) {
|
|
217
|
+
options = within(menu).queryAllByRole('menuitem');
|
|
218
|
+
if (options.length === 0) {
|
|
219
|
+
options = within(menu).queryAllByRole('menuitemradio');
|
|
220
|
+
if (options.length === 0) {
|
|
221
|
+
options = within(menu).queryAllByRole('menuitemcheckbox');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return options;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
get sections() {
|
|
230
|
+
let menu = this.menu;
|
|
231
|
+
if (menu) {
|
|
232
|
+
return within(menu).queryAllByRole('group');
|
|
233
|
+
} else {
|
|
234
|
+
return [];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
get submenuTriggers() {
|
|
239
|
+
let options = this.options;
|
|
240
|
+
if (options.length > 0) {
|
|
241
|
+
return this.options.filter(item => item.getAttribute('aria-haspopup') != null);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return [];
|
|
245
|
+
}
|
|
246
|
+
}
|