@react-aria/test-utils 1.0.0-alpha.0 → 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.
Files changed (58) hide show
  1. package/dist/combobox.main.js +137 -0
  2. package/dist/combobox.main.js.map +1 -0
  3. package/dist/combobox.mjs +132 -0
  4. package/dist/combobox.module.js +132 -0
  5. package/dist/combobox.module.js.map +1 -0
  6. package/dist/events.main.js +25 -10
  7. package/dist/events.main.js.map +1 -1
  8. package/dist/events.mjs +26 -11
  9. package/dist/events.module.js +25 -10
  10. package/dist/events.module.js.map +1 -1
  11. package/dist/gridlist.main.js +97 -0
  12. package/dist/gridlist.main.js.map +1 -0
  13. package/dist/gridlist.mjs +92 -0
  14. package/dist/gridlist.module.js +92 -0
  15. package/dist/gridlist.module.js.map +1 -0
  16. package/dist/import.mjs +4 -2
  17. package/dist/main.js +10 -18
  18. package/dist/main.js.map +1 -1
  19. package/dist/menu.main.js +176 -0
  20. package/dist/menu.main.js.map +1 -0
  21. package/dist/menu.mjs +171 -0
  22. package/dist/menu.module.js +171 -0
  23. package/dist/menu.module.js.map +1 -0
  24. package/dist/module.js +4 -2
  25. package/dist/module.js.map +1 -1
  26. package/dist/select.main.js +113 -0
  27. package/dist/select.main.js.map +1 -0
  28. package/dist/select.mjs +108 -0
  29. package/dist/select.module.js +108 -0
  30. package/dist/select.module.js.map +1 -0
  31. package/dist/table.main.js +191 -0
  32. package/dist/table.main.js.map +1 -0
  33. package/dist/table.mjs +186 -0
  34. package/dist/table.module.js +186 -0
  35. package/dist/table.module.js.map +1 -0
  36. package/dist/testSetup.main.js +1 -1
  37. package/dist/testSetup.mjs +2 -2
  38. package/dist/testSetup.module.js +1 -1
  39. package/dist/types.d.ts +180 -4
  40. package/dist/types.d.ts.map +1 -1
  41. package/dist/user.main.js +65 -0
  42. package/dist/user.main.js.map +1 -0
  43. package/dist/user.mjs +56 -0
  44. package/dist/user.module.js +56 -0
  45. package/dist/user.module.js.map +1 -0
  46. package/dist/userEventMaps.main.js +15 -15
  47. package/dist/userEventMaps.mjs +16 -16
  48. package/dist/userEventMaps.module.js +15 -15
  49. package/package.json +6 -7
  50. package/src/combobox.ts +188 -0
  51. package/src/events.ts +28 -8
  52. package/src/gridlist.ts +116 -0
  53. package/src/index.ts +6 -3
  54. package/src/menu.ts +246 -0
  55. package/src/select.ts +158 -0
  56. package/src/table.ts +282 -0
  57. package/src/user.ts +73 -0
  58. package/LICENSE +0 -201
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
+ }
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
+ }
package/src/table.ts ADDED
@@ -0,0 +1,282 @@
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, fireEvent, waitFor, within} from '@testing-library/react';
14
+ import {BaseTesterOpts, UserOpts} from './user';
15
+ import {pressElement, triggerLongPress} from './events';
16
+ export interface TableOptions extends UserOpts, BaseTesterOpts {
17
+ user: any,
18
+ advanceTimer: UserOpts['advanceTimer']
19
+ }
20
+
21
+ // TODO: Previously used logic like https://github.com/testing-library/react-testing-library/blame/c63b873072d62c858959c2a19e68f8e2cc0b11be/src/pure.js#L16
22
+ // but https://github.com/testing-library/dom-testing-library/issues/987#issuecomment-891901804 indicates that it may falsely indicate that fake timers are enabled
23
+ // when they aren't
24
+ export class TableTester {
25
+ private user;
26
+ private _interactionType: UserOpts['interactionType'];
27
+ private _advanceTimer: UserOpts['advanceTimer'];
28
+ private _table: HTMLElement;
29
+
30
+ constructor(opts: TableOptions) {
31
+ let {root, user, interactionType, advanceTimer} = opts;
32
+ this.user = user;
33
+ this._interactionType = interactionType || 'mouse';
34
+ this._advanceTimer = advanceTimer;
35
+ this._table = root;
36
+ }
37
+
38
+ setInteractionType = (type: UserOpts['interactionType']) => {
39
+ this._interactionType = type;
40
+ };
41
+
42
+ toggleRowSelection = async (opts: {index?: number, text?: string, needsLongPress?: boolean, interactionType?: UserOpts['interactionType']} = {}) => {
43
+ let {
44
+ index,
45
+ text,
46
+ needsLongPress,
47
+ interactionType = this._interactionType
48
+ } = opts;
49
+
50
+ let row = this.findRow({index, text});
51
+ let rowCheckbox = within(row).queryByRole('checkbox');
52
+ if (rowCheckbox) {
53
+ await pressElement(this.user, rowCheckbox, interactionType);
54
+ } else {
55
+ let cell = within(row).getAllByRole('gridcell')[0];
56
+ if (needsLongPress && interactionType === 'touch') {
57
+ if (this._advanceTimer == null) {
58
+ throw new Error('No advanceTimers provided for long press.');
59
+ }
60
+
61
+ // Note that long press interactions with rows is strictly touch only for grid rows
62
+ await triggerLongPress({element: cell, advanceTimer: this._advanceTimer, pointerOpts: {pointerType: 'touch'}});
63
+ // TODO: interestingly enough, we need to do a followup click otherwise future row selections may not fire properly?
64
+ // To reproduce, try removing this, forcing toggleRowSelection to hit "needsLongPress ? await triggerLongPress(cell) : await action(cell);" and
65
+ // run Table.test's "should support long press to enter selection mode on touch" test to see what happens
66
+ await fireEvent.click(cell);
67
+ } else {
68
+ await pressElement(this.user, cell, interactionType);
69
+ }
70
+ }
71
+
72
+ // Handle cases where the table may transition in response to the row selection/deselection
73
+ await act(async () => {
74
+ if (this._advanceTimer == null) {
75
+ throw new Error('No advanceTimers provided for table transition.');
76
+ }
77
+
78
+ await this._advanceTimer(200);
79
+ });
80
+ };
81
+
82
+ toggleSort = async (opts: {index?: number, text?: string, interactionType?: UserOpts['interactionType']} = {}) => {
83
+ let {
84
+ index,
85
+ text,
86
+ interactionType = this._interactionType
87
+ } = opts;
88
+
89
+ let columnheader;
90
+ if (index != null) {
91
+ columnheader = this.columns[index];
92
+ } else if (text != null) {
93
+ columnheader = within(this.rowGroups[0]).getByText(text);
94
+ while (columnheader && !/columnheader/.test(columnheader.getAttribute('role'))) {
95
+ columnheader = columnheader.parentElement;
96
+ }
97
+ }
98
+
99
+ let menuButton = within(columnheader).queryByRole('button');
100
+ if (menuButton) {
101
+ let currentSort = columnheader.getAttribute('aria-sort');
102
+ // TODO: Focus management is all kinda of messed up if I just use .focus and Space to open the sort menu. Seems like
103
+ // the focused key doesn't get properly set to the desired column header. Have to do this strange flow where I focus the
104
+ // column header except if the active element is already the menu button within the column header
105
+ if (interactionType === 'keyboard' && document.activeElement !== menuButton) {
106
+ await pressElement(this.user, columnheader, interactionType);
107
+ } else {
108
+ await pressElement(this.user, menuButton, interactionType);
109
+ }
110
+
111
+ await waitFor(() => {
112
+ if (menuButton.getAttribute('aria-controls') == null) {
113
+ throw new Error('No aria-controls found on table column dropdown menu trigger element.');
114
+ } else {
115
+ return true;
116
+ }
117
+ });
118
+
119
+ let menuId = menuButton.getAttribute('aria-controls');
120
+ await waitFor(() => {
121
+ if (!menuId || document.getElementById(menuId) == null) {
122
+ throw new Error(`Table column header menu with id of ${menuId} not found in document.`);
123
+ } else {
124
+ return true;
125
+ }
126
+ });
127
+
128
+ if (menuId) {
129
+ let menu = document.getElementById(menuId);
130
+ if (menu) {
131
+ if (currentSort === 'ascending') {
132
+ await pressElement(this.user, within(menu).getAllByRole('menuitem')[1], interactionType);
133
+ } else {
134
+ await pressElement(this.user, within(menu).getAllByRole('menuitem')[0], interactionType);
135
+ }
136
+
137
+ await waitFor(() => {
138
+ if (document.contains(menu)) {
139
+ throw new Error('Expected table column menu listbox to not be in the document after selecting an option');
140
+ } else {
141
+ return true;
142
+ }
143
+ });
144
+ }
145
+ }
146
+
147
+ // Handle cases where the table may transition in response to the row selection/deselection
148
+ await act(async () => {
149
+ if (this._advanceTimer == null) {
150
+ throw new Error('No advanceTimers provided for table transition.');
151
+ }
152
+
153
+ await this._advanceTimer(200);
154
+ });
155
+
156
+ await waitFor(() => {
157
+ if (document.activeElement !== menuButton) {
158
+ throw new Error(`Expected the document.activeElement to be the table column menu button but got ${document.activeElement}`);
159
+ } else {
160
+ return true;
161
+ }
162
+ });
163
+ } else {
164
+ await pressElement(this.user, columnheader, interactionType);
165
+ }
166
+ };
167
+ // TODO: should there be a util for triggering a row action? Perhaps there should be but it would rely on the user teling us the config of the
168
+ // table. Maybe we could rely on the user knowing to trigger a press/double click? We could have the user pass in "needsDoubleClick"
169
+ // It is also iffy if there is any row selected because then the table is in selectionMode and the below actions will simply toggle row selection
170
+ triggerRowAction = async (opts: {index?: number, text?: string, needsDoubleClick?: boolean, interactionType?: UserOpts['interactionType']} = {}) => {
171
+ let {
172
+ index,
173
+ text,
174
+ needsDoubleClick,
175
+ interactionType = this._interactionType
176
+ } = opts;
177
+
178
+ let row = this.findRow({index, text});
179
+ if (row) {
180
+ if (needsDoubleClick) {
181
+ await this.user.dblClick(row);
182
+ } else if (interactionType === 'keyboard') {
183
+ act(() => row.focus());
184
+ await this.user.keyboard('[Enter]');
185
+ } else {
186
+ await pressElement(this.user, row, interactionType);
187
+ }
188
+ }
189
+ };
190
+
191
+ // TODO: should there be utils for drag and drop and column resizing? For column resizing, I'm not entirely convinced that users will be doing that in their tests.
192
+ // For DnD, it might be tricky to do for keyboard DnD since we wouldn't know what valid drop zones there are... Similarly, for simulating mouse drag and drop the coordinates depend
193
+ // on the mocks the user sets up for their row height/etc.
194
+ // Additionally, should we also support keyboard navigation/typeahead? Those felt like they could be very easily replicated by the user via user.keyboard already and don't really
195
+ // add much value if we provide that to them
196
+
197
+ toggleSelectAll = async (opts: {interactionType?: UserOpts['interactionType']} = {}) => {
198
+ let {
199
+ interactionType = this._interactionType
200
+ } = opts;
201
+ let checkbox = within(this.table).getByLabelText('Select All');
202
+ if (interactionType === 'keyboard') {
203
+ // TODO: using the .focus -> trigger keyboard Enter approach doesn't work for some reason, for now just trigger select all with click.
204
+ await this.user.click(checkbox);
205
+ } else {
206
+ await pressElement(this.user, checkbox, interactionType);
207
+ }
208
+ };
209
+
210
+ findRow = (opts: {index?: number, text?: string} = {}) => {
211
+ let {
212
+ index,
213
+ text
214
+ } = opts;
215
+
216
+ let row;
217
+ let rows = this.rows;
218
+ let bodyRowGroup = this.rowGroups[1];
219
+ if (index != null) {
220
+ row = rows[index];
221
+ } else if (text != null) {
222
+ row = within(bodyRowGroup).getByText(text);
223
+ while (row && row.getAttribute('role') !== 'row') {
224
+ row = row.parentElement;
225
+ }
226
+ }
227
+
228
+ return row;
229
+ };
230
+
231
+ findCell = (opts: {text: string}) => {
232
+ let {
233
+ text
234
+ } = opts;
235
+
236
+ let cell = within(this.table).getByText(text);
237
+ if (cell) {
238
+ while (cell && !/gridcell|rowheader|columnheader/.test(cell.getAttribute('role') || '')) {
239
+ if (cell.parentElement) {
240
+ cell = cell.parentElement;
241
+ } else {
242
+ break;
243
+ }
244
+ }
245
+ }
246
+
247
+ return cell;
248
+ };
249
+
250
+ get table() {
251
+ return this._table;
252
+ }
253
+
254
+ get rowGroups() {
255
+ let table = this._table;
256
+ return table ? within(table).queryAllByRole('rowgroup') : [];
257
+ }
258
+
259
+ get columns() {
260
+ let headerRowGroup = this.rowGroups[0];
261
+ return headerRowGroup ? within(headerRowGroup).queryAllByRole('columnheader') : [];
262
+ }
263
+
264
+ get rows() {
265
+ let bodyRowGroup = this.rowGroups[1];
266
+ return bodyRowGroup ? within(bodyRowGroup).queryAllByRole('row') : [];
267
+ }
268
+
269
+ get selectedRows() {
270
+ return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');
271
+ }
272
+
273
+ get rowHeaders() {
274
+ let table = this.table;
275
+ return table ? within(table).queryAllByRole('rowheader') : [];
276
+ }
277
+
278
+ get cells() {
279
+ let table = this.table;
280
+ return table ? within(table).queryAllByRole('gridcell') : [];
281
+ }
282
+ }