@react-aria/test-utils 1.0.0-alpha.1 → 1.0.0-alpha.3
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 +220 -0
- package/dist/menu.main.js.map +1 -0
- package/dist/menu.mjs +215 -0
- package/dist/menu.module.js +215 -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 +187 -0
- package/dist/table.main.js.map +1 -0
- package/dist/table.mjs +182 -0
- package/dist/table.module.js +182 -0
- package/dist/table.module.js.map +1 -0
- package/dist/testSetup.main.js +2 -6
- package/dist/testSetup.main.js.map +1 -1
- package/dist/testSetup.mjs +2 -6
- package/dist/testSetup.module.js +2 -6
- package/dist/testSetup.module.js.map +1 -1
- package/dist/types.d.ts +186 -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 +5 -5
- 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 +308 -0
- package/src/select.ts +158 -0
- package/src/table.ts +273 -0
- package/src/testSetup.ts +2 -6
- package/src/user.ts +73 -0
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,308 @@
|
|
|
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
|
+
isSubmenu?: boolean
|
|
20
|
+
}
|
|
21
|
+
export class MenuTester {
|
|
22
|
+
private user;
|
|
23
|
+
private _interactionType: UserOpts['interactionType'];
|
|
24
|
+
private _advanceTimer: UserOpts['advanceTimer'];
|
|
25
|
+
private _trigger: HTMLElement | undefined;
|
|
26
|
+
private _isSubmenu: boolean = false;
|
|
27
|
+
|
|
28
|
+
constructor(opts: MenuOptions) {
|
|
29
|
+
let {root, user, interactionType, advanceTimer, isSubmenu} = opts;
|
|
30
|
+
this.user = user;
|
|
31
|
+
this._interactionType = interactionType || 'mouse';
|
|
32
|
+
this._advanceTimer = advanceTimer;
|
|
33
|
+
|
|
34
|
+
// Handle case where a submenu trigger is provided to the tester
|
|
35
|
+
if (root.getAttribute('role') === 'menuitem') {
|
|
36
|
+
this._trigger = root;
|
|
37
|
+
} else {
|
|
38
|
+
// Handle case where element provided is a wrapper of the trigger button
|
|
39
|
+
let trigger = within(root).queryByRole('button');
|
|
40
|
+
if (trigger) {
|
|
41
|
+
this._trigger = trigger;
|
|
42
|
+
} else {
|
|
43
|
+
this._trigger = root;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this._isSubmenu = isSubmenu || false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setInteractionType = (type: UserOpts['interactionType']) => {
|
|
51
|
+
this._interactionType = type;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// 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
|
|
55
|
+
// One difference will be that it supports long press as well
|
|
56
|
+
open = async (opts: {needsLongPress?: boolean, interactionType?: UserOpts['interactionType'], direction?: 'up' | 'down'} = {}) => {
|
|
57
|
+
let {
|
|
58
|
+
needsLongPress,
|
|
59
|
+
interactionType = this._interactionType,
|
|
60
|
+
direction
|
|
61
|
+
} = opts;
|
|
62
|
+
let trigger = this.trigger;
|
|
63
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
64
|
+
if (interactionType === 'mouse' || interactionType === 'touch') {
|
|
65
|
+
if (needsLongPress) {
|
|
66
|
+
if (this._advanceTimer == null) {
|
|
67
|
+
throw new Error('No advanceTimers provided for long press.');
|
|
68
|
+
}
|
|
69
|
+
let pointerType = interactionType === 'mouse' ? 'mouse' : 'touch';
|
|
70
|
+
await triggerLongPress({element: trigger, advanceTimer: this._advanceTimer, pointerOpts: {pointerType}});
|
|
71
|
+
} else if (interactionType === 'mouse') {
|
|
72
|
+
await this.user.click(trigger);
|
|
73
|
+
} else {
|
|
74
|
+
await this.user.pointer({target: trigger, keys: '[TouchA]'});
|
|
75
|
+
}
|
|
76
|
+
} else if (interactionType === 'keyboard' && !isDisabled) {
|
|
77
|
+
if (direction === 'up') {
|
|
78
|
+
act(() => trigger.focus());
|
|
79
|
+
await this.user.keyboard('[ArrowUp]');
|
|
80
|
+
} else if (direction === 'down') {
|
|
81
|
+
act(() => trigger.focus());
|
|
82
|
+
await this.user.keyboard('[ArrowDown]');
|
|
83
|
+
} else {
|
|
84
|
+
act(() => trigger.focus());
|
|
85
|
+
await this.user.keyboard('[Enter]');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await waitFor(() => {
|
|
90
|
+
if (trigger.getAttribute('aria-controls') == null && !isDisabled) {
|
|
91
|
+
throw new Error('No aria-controls found on menu trigger element.');
|
|
92
|
+
} else {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
if (!isDisabled) {
|
|
97
|
+
let menuId = trigger.getAttribute('aria-controls');
|
|
98
|
+
await waitFor(() => {
|
|
99
|
+
if (!menuId || document.getElementById(menuId) == null) {
|
|
100
|
+
throw new Error(`Menu with id of ${menuId} not found in document.`);
|
|
101
|
+
} else {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// TODO: also very similar to select, barring potential long press support
|
|
109
|
+
// Close on select is also kinda specific?
|
|
110
|
+
selectOption = async (opts: {
|
|
111
|
+
option?: HTMLElement,
|
|
112
|
+
optionText?: string,
|
|
113
|
+
menuSelectionMode?: 'single' | 'multiple',
|
|
114
|
+
needsLongPress?: boolean,
|
|
115
|
+
closesOnSelect?: boolean,
|
|
116
|
+
interactionType?: UserOpts['interactionType'],
|
|
117
|
+
keyboardActivation?: 'Space' | 'Enter'
|
|
118
|
+
}) => {
|
|
119
|
+
let {
|
|
120
|
+
optionText,
|
|
121
|
+
menuSelectionMode = 'single',
|
|
122
|
+
needsLongPress,
|
|
123
|
+
closesOnSelect = true,
|
|
124
|
+
option,
|
|
125
|
+
interactionType = this._interactionType,
|
|
126
|
+
keyboardActivation = 'Enter'
|
|
127
|
+
} = opts;
|
|
128
|
+
let trigger = this.trigger;
|
|
129
|
+
|
|
130
|
+
if (!trigger.getAttribute('aria-controls') && !trigger.hasAttribute('aria-expanded')) {
|
|
131
|
+
await this.open({needsLongPress});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let menu = this.menu;
|
|
135
|
+
if (menu) {
|
|
136
|
+
if (!option && optionText) {
|
|
137
|
+
option = (within(menu!).getByText(optionText).closest('[role=menuitem], [role=menuitemradio], [role=menuitemcheckbox]'))! as HTMLElement;
|
|
138
|
+
}
|
|
139
|
+
if (!option) {
|
|
140
|
+
throw new Error('No option found in the menu.');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (interactionType === 'keyboard') {
|
|
144
|
+
if (document.activeElement !== menu || !menu.contains(document.activeElement)) {
|
|
145
|
+
act(() => menu.focus());
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
await this.keyboardNavigateToOption({option});
|
|
149
|
+
await this.user.keyboard(`[${keyboardActivation}]`);
|
|
150
|
+
} else {
|
|
151
|
+
if (interactionType === 'mouse') {
|
|
152
|
+
await this.user.click(option);
|
|
153
|
+
} else {
|
|
154
|
+
await this.user.pointer({target: option, keys: '[TouchA]'});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
act(() => {jest.runAllTimers();});
|
|
158
|
+
|
|
159
|
+
if (option && option.getAttribute('href') == null && option.getAttribute('aria-haspopup') == null && menuSelectionMode === 'single' && closesOnSelect && keyboardActivation !== 'Space' && !this._isSubmenu) {
|
|
160
|
+
await waitFor(() => {
|
|
161
|
+
if (document.activeElement !== trigger) {
|
|
162
|
+
throw new Error(`Expected the document.activeElement after selecting an option to be the menu trigger but got ${document.activeElement}`);
|
|
163
|
+
} else {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
if (document.contains(menu)) {
|
|
169
|
+
throw new Error('Expected menu element to not be in the document after selecting an option');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
throw new Error("Attempted to select a option in the menu, but menu wasn't found.");
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// TODO: update this to remove needsLongPress if we wanna make the user call open first always
|
|
178
|
+
openSubmenu = async (opts: {submenuTrigger?: HTMLElement, submenuTriggerText?: string, needsLongPress?: boolean, interactionType?: UserOpts['interactionType']}): Promise<MenuTester | null> => {
|
|
179
|
+
let {
|
|
180
|
+
submenuTrigger,
|
|
181
|
+
submenuTriggerText,
|
|
182
|
+
needsLongPress,
|
|
183
|
+
interactionType = this._interactionType
|
|
184
|
+
} = opts;
|
|
185
|
+
|
|
186
|
+
let trigger = this.trigger;
|
|
187
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
188
|
+
if (!trigger.getAttribute('aria-controls') && !isDisabled) {
|
|
189
|
+
await this.open({needsLongPress});
|
|
190
|
+
}
|
|
191
|
+
if (!isDisabled) {
|
|
192
|
+
let menu = this.menu;
|
|
193
|
+
if (menu) {
|
|
194
|
+
let submenu;
|
|
195
|
+
if (submenuTrigger) {
|
|
196
|
+
submenu = submenuTrigger;
|
|
197
|
+
} else if (submenuTriggerText) {
|
|
198
|
+
submenu = within(menu).getByText(submenuTriggerText);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let submenuTriggerTester = new MenuTester({user: this.user, interactionType: this._interactionType, root: submenu, isSubmenu: true});
|
|
202
|
+
if (interactionType === 'mouse') {
|
|
203
|
+
await this.user.pointer({target: submenu});
|
|
204
|
+
act(() => {jest.runAllTimers();});
|
|
205
|
+
} else if (interactionType === 'keyboard') {
|
|
206
|
+
await this.keyboardNavigateToOption({option: submenu});
|
|
207
|
+
await this.user.keyboard('[ArrowRight]');
|
|
208
|
+
act(() => {jest.runAllTimers();});
|
|
209
|
+
} else {
|
|
210
|
+
await submenuTriggerTester.open();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
return submenuTriggerTester;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return null;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
keyboardNavigateToOption = async (opts: {option: HTMLElement}) => {
|
|
222
|
+
let {option} = opts;
|
|
223
|
+
let options = this.options;
|
|
224
|
+
let targetIndex = options.indexOf(option);
|
|
225
|
+
if (targetIndex === -1) {
|
|
226
|
+
throw new Error('Option provided is not in the menu');
|
|
227
|
+
}
|
|
228
|
+
if (document.activeElement === this.menu) {
|
|
229
|
+
await this.user.keyboard('[ArrowDown]');
|
|
230
|
+
}
|
|
231
|
+
let currIndex = options.indexOf(document.activeElement as HTMLElement);
|
|
232
|
+
if (targetIndex === -1) {
|
|
233
|
+
throw new Error('ActiveElement is not in the menu');
|
|
234
|
+
}
|
|
235
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
236
|
+
|
|
237
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
238
|
+
await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
close = async () => {
|
|
244
|
+
let menu = this.menu;
|
|
245
|
+
if (menu) {
|
|
246
|
+
act(() => menu.focus());
|
|
247
|
+
await this.user.keyboard('[Escape]');
|
|
248
|
+
|
|
249
|
+
await waitFor(() => {
|
|
250
|
+
if (document.activeElement !== this.trigger) {
|
|
251
|
+
throw new Error(`Expected the document.activeElement after closing the menu to be the menu trigger but got ${document.activeElement}`);
|
|
252
|
+
} else {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
if (document.contains(menu)) {
|
|
258
|
+
throw new Error('Expected the menu to not be in the document after closing it.');
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
get trigger() {
|
|
264
|
+
if (!this._trigger) {
|
|
265
|
+
throw new Error('No trigger element found for menu.');
|
|
266
|
+
}
|
|
267
|
+
return this._trigger;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
get menu() {
|
|
271
|
+
let menuId = this.trigger.getAttribute('aria-controls');
|
|
272
|
+
return menuId ? document.getElementById(menuId) : undefined;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
get options(): HTMLElement[] {
|
|
276
|
+
let menu = this.menu;
|
|
277
|
+
let options: HTMLElement[] = [];
|
|
278
|
+
if (menu) {
|
|
279
|
+
options = within(menu).queryAllByRole('menuitem');
|
|
280
|
+
if (options.length === 0) {
|
|
281
|
+
options = within(menu).queryAllByRole('menuitemradio');
|
|
282
|
+
if (options.length === 0) {
|
|
283
|
+
options = within(menu).queryAllByRole('menuitemcheckbox');
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return options;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
get sections() {
|
|
292
|
+
let menu = this.menu;
|
|
293
|
+
if (menu) {
|
|
294
|
+
return within(menu).queryAllByRole('group');
|
|
295
|
+
} else {
|
|
296
|
+
return [];
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
get submenuTriggers() {
|
|
301
|
+
let options = this.options;
|
|
302
|
+
if (options.length > 0) {
|
|
303
|
+
return this.options.filter(item => item.getAttribute('aria-haspopup') != null);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return [];
|
|
307
|
+
}
|
|
308
|
+
}
|
package/src/select.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
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 SelectOptions extends UserOpts, BaseTesterOpts {
|
|
17
|
+
// TODO: I think the type grabbed from the testing library dist for UserEvent is breaking the build, will need to figure out a better place to grab from
|
|
18
|
+
user?: any
|
|
19
|
+
}
|
|
20
|
+
export class SelectTester {
|
|
21
|
+
private user;
|
|
22
|
+
private _interactionType: UserOpts['interactionType'];
|
|
23
|
+
private _trigger: HTMLElement;
|
|
24
|
+
|
|
25
|
+
constructor(opts: SelectOptions) {
|
|
26
|
+
let {root, user, interactionType} = opts;
|
|
27
|
+
this.user = user;
|
|
28
|
+
this._interactionType = interactionType || 'mouse';
|
|
29
|
+
// Handle case where the wrapper element is provided rather than the Select's button (aka RAC)
|
|
30
|
+
let triggerButton = within(root).queryByRole('button');
|
|
31
|
+
if (triggerButton == null) {
|
|
32
|
+
triggerButton = root;
|
|
33
|
+
}
|
|
34
|
+
this._trigger = triggerButton;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setInteractionType = (type: UserOpts['interactionType']) => {
|
|
38
|
+
this._interactionType = type;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
open = async (opts: {interactionType?: UserOpts['interactionType']} = {}) => {
|
|
42
|
+
let {
|
|
43
|
+
interactionType = this._interactionType
|
|
44
|
+
} = opts;
|
|
45
|
+
let trigger = this.trigger;
|
|
46
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
47
|
+
|
|
48
|
+
if (interactionType === 'mouse') {
|
|
49
|
+
await this.user.click(this._trigger);
|
|
50
|
+
} else if (interactionType === 'keyboard') {
|
|
51
|
+
act(() => trigger.focus());
|
|
52
|
+
await this.user.keyboard('[Enter]');
|
|
53
|
+
} else if (interactionType === 'touch') {
|
|
54
|
+
await this.user.pointer({target: this._trigger, keys: '[TouchA]'});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
await waitFor(() => {
|
|
58
|
+
if (!isDisabled && trigger.getAttribute('aria-controls') == null) {
|
|
59
|
+
throw new Error('No aria-controls found on select element trigger.');
|
|
60
|
+
} else {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
let listBoxId = trigger.getAttribute('aria-controls');
|
|
65
|
+
await waitFor(() => {
|
|
66
|
+
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {
|
|
67
|
+
throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
68
|
+
} else {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
selectOption = async (opts: {optionText: string, interactionType?: UserOpts['interactionType']}) => {
|
|
75
|
+
let {
|
|
76
|
+
optionText,
|
|
77
|
+
interactionType = this._interactionType
|
|
78
|
+
} = opts || {};
|
|
79
|
+
let trigger = this.trigger;
|
|
80
|
+
if (!trigger.getAttribute('aria-controls')) {
|
|
81
|
+
await this.open();
|
|
82
|
+
}
|
|
83
|
+
let listbox = this.listbox;
|
|
84
|
+
if (listbox) {
|
|
85
|
+
let option = within(listbox).getByText(optionText);
|
|
86
|
+
if (interactionType === 'keyboard') {
|
|
87
|
+
if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) {
|
|
88
|
+
act(() => listbox.focus());
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// TODO: this simulates typeahead, do we want to add a helper util for that? Not sure if users would really need that for
|
|
92
|
+
// their test
|
|
93
|
+
await this.user.keyboard(optionText);
|
|
94
|
+
await this.user.keyboard('[Enter]');
|
|
95
|
+
} else {
|
|
96
|
+
// TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)
|
|
97
|
+
if (interactionType === 'mouse') {
|
|
98
|
+
await this.user.click(option);
|
|
99
|
+
} else {
|
|
100
|
+
await this.user.pointer({target: option, keys: '[TouchA]'});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (option.getAttribute('href') == null) {
|
|
105
|
+
await waitFor(() => {
|
|
106
|
+
if (document.activeElement !== this._trigger) {
|
|
107
|
+
throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);
|
|
108
|
+
} else {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (document.contains(listbox)) {
|
|
114
|
+
throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
close = async () => {
|
|
121
|
+
let listbox = this.listbox;
|
|
122
|
+
if (listbox) {
|
|
123
|
+
act(() => listbox.focus());
|
|
124
|
+
await this.user.keyboard('[Escape]');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
await waitFor(() => {
|
|
128
|
+
if (document.activeElement !== this._trigger) {
|
|
129
|
+
throw new Error(`Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`);
|
|
130
|
+
} else {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
if (listbox && document.contains(listbox)) {
|
|
136
|
+
throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
get trigger() {
|
|
141
|
+
return this._trigger;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
get listbox() {
|
|
145
|
+
let listBoxId = this.trigger.getAttribute('aria-controls');
|
|
146
|
+
return listBoxId ? document.getElementById(listBoxId) : undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get options() {
|
|
150
|
+
let listbox = this.listbox;
|
|
151
|
+
return listbox ? within(listbox).queryAllByRole('option') : [];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
get sections() {
|
|
155
|
+
let listbox = this.listbox;
|
|
156
|
+
return listbox ? within(listbox).queryAllByRole('group') : [];
|
|
157
|
+
}
|
|
158
|
+
}
|