@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
package/src/table.ts
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
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 {
|
|
15
|
+
BaseGridRowInteractionOpts,
|
|
16
|
+
Direction,
|
|
17
|
+
GridRowActionOpts,
|
|
18
|
+
TableTesterOpts,
|
|
19
|
+
ToggleGridRowOpts,
|
|
20
|
+
UserOpts
|
|
21
|
+
} from './types';
|
|
22
|
+
import {formatTargetNode, getAltKey, getMetaKey, pressElement, triggerLongPress} from './utils';
|
|
23
|
+
import {waitFor, within} from '@testing-library/dom';
|
|
24
|
+
|
|
25
|
+
interface TableToggleRowOpts extends ToggleGridRowOpts {}
|
|
26
|
+
interface TableToggleExpansionOpts extends BaseGridRowInteractionOpts {}
|
|
27
|
+
interface TableToggleSortOpts {
|
|
28
|
+
/**
|
|
29
|
+
* The index, text, or node of the column to toggle selection for.
|
|
30
|
+
*/
|
|
31
|
+
column: number | string | HTMLElement;
|
|
32
|
+
/**
|
|
33
|
+
* What interaction type to use when sorting the column. Defaults to the interaction type set on
|
|
34
|
+
* the tester.
|
|
35
|
+
*/
|
|
36
|
+
interactionType?: UserOpts['interactionType'];
|
|
37
|
+
}
|
|
38
|
+
interface TableColumnHeaderActionOpts extends TableToggleSortOpts {
|
|
39
|
+
/**
|
|
40
|
+
* The index of the column header action to trigger.
|
|
41
|
+
*/
|
|
42
|
+
action: number;
|
|
43
|
+
}
|
|
44
|
+
interface TableRowActionOpts extends GridRowActionOpts {}
|
|
45
|
+
|
|
46
|
+
export class TableTester {
|
|
47
|
+
private user;
|
|
48
|
+
private _interactionType: UserOpts['interactionType'];
|
|
49
|
+
private _advanceTimer: UserOpts['advanceTimer'];
|
|
50
|
+
private _direction: Direction;
|
|
51
|
+
private _table: HTMLElement;
|
|
52
|
+
|
|
53
|
+
constructor(opts: TableTesterOpts) {
|
|
54
|
+
let {root, user, interactionType, advanceTimer, direction} = opts;
|
|
55
|
+
this.user = user;
|
|
56
|
+
this._interactionType = interactionType || 'mouse';
|
|
57
|
+
this._advanceTimer = advanceTimer;
|
|
58
|
+
this._direction = direction || 'ltr';
|
|
59
|
+
this._table = root;
|
|
60
|
+
let role = root.getAttribute('role');
|
|
61
|
+
if (role !== 'grid' && role !== 'treegrid') {
|
|
62
|
+
let table = within(root).queryByRole('grid') || within(root).queryByRole('treegrid');
|
|
63
|
+
if (table) {
|
|
64
|
+
this._table = table;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Set the interaction type used by the table tester.
|
|
71
|
+
*/
|
|
72
|
+
setInteractionType(type: UserOpts['interactionType']): void {
|
|
73
|
+
this._interactionType = type;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private async keyboardNavigateToRow(opts: {
|
|
77
|
+
row: HTMLElement;
|
|
78
|
+
selectionOnNav?: 'default' | 'none';
|
|
79
|
+
}) {
|
|
80
|
+
let {row, selectionOnNav = 'default'} = opts;
|
|
81
|
+
let altKey = getAltKey();
|
|
82
|
+
let rows = this.getRows();
|
|
83
|
+
let targetIndex = rows.indexOf(row);
|
|
84
|
+
if (targetIndex === -1) {
|
|
85
|
+
throw new Error('Row provided is not in the table');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Move focus into the table
|
|
89
|
+
if (document.activeElement !== this._table && !this._table.contains(document.activeElement)) {
|
|
90
|
+
act(() => this._table.focus());
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (document.activeElement === this._table) {
|
|
94
|
+
await this.user.keyboard('[ArrowDown]');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let rowGroups = this.getRowGroups();
|
|
98
|
+
// If focus is currently somewhere in the first row group (aka on a column), we want to keyboard navigate downwards till we reach the rows
|
|
99
|
+
if (rowGroups[0].contains(document.activeElement)) {
|
|
100
|
+
do {
|
|
101
|
+
await this.user.keyboard('[ArrowDown]');
|
|
102
|
+
} while (!rowGroups[1].contains(document.activeElement));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Move focus onto the row itself
|
|
106
|
+
let focusPrevKey = this._direction === 'rtl' ? 'ArrowRight' : 'ArrowLeft';
|
|
107
|
+
if (
|
|
108
|
+
rowGroups[1].contains(document.activeElement) &&
|
|
109
|
+
document.activeElement!.getAttribute('role') !== 'row'
|
|
110
|
+
) {
|
|
111
|
+
do {
|
|
112
|
+
await this.user.keyboard(`[${focusPrevKey}]`);
|
|
113
|
+
} while (document.activeElement!.getAttribute('role') !== 'row');
|
|
114
|
+
}
|
|
115
|
+
let currIndex = rows.indexOf(document.activeElement as HTMLElement);
|
|
116
|
+
if (currIndex === -1) {
|
|
117
|
+
throw new Error('Current active element is not on any of the table rows');
|
|
118
|
+
}
|
|
119
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
120
|
+
|
|
121
|
+
if (selectionOnNav === 'none') {
|
|
122
|
+
await this.user.keyboard(`[${altKey}>]`);
|
|
123
|
+
}
|
|
124
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
125
|
+
await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
126
|
+
}
|
|
127
|
+
if (selectionOnNav === 'none') {
|
|
128
|
+
await this.user.keyboard(`[/${altKey}]`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Toggles the selection for the specified table row. Defaults to using the interaction type set
|
|
134
|
+
* on the table tester.
|
|
135
|
+
*/
|
|
136
|
+
async toggleRowSelection(opts: TableToggleRowOpts): Promise<void> {
|
|
137
|
+
let {
|
|
138
|
+
row,
|
|
139
|
+
needsLongPress,
|
|
140
|
+
checkboxSelection = true,
|
|
141
|
+
interactionType = this._interactionType,
|
|
142
|
+
selectionBehavior = 'toggle'
|
|
143
|
+
} = opts;
|
|
144
|
+
|
|
145
|
+
let altKey = getAltKey();
|
|
146
|
+
let metaKey = getMetaKey();
|
|
147
|
+
|
|
148
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
149
|
+
row = this.findRow({indexOrText: row});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!row) {
|
|
153
|
+
throw new Error(`Target row "${formatTargetNode(opts.row)}" not found in the table.`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let rowCheckbox = within(row).queryByRole('checkbox');
|
|
157
|
+
|
|
158
|
+
if (
|
|
159
|
+
rowCheckbox?.getAttribute('disabled') === '' ||
|
|
160
|
+
row.getAttribute('aria-disabled') === 'true'
|
|
161
|
+
) {
|
|
162
|
+
throw new Error(`Cannot toggle selection on disabled row "${formatTargetNode(opts.row)}".`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (interactionType === 'keyboard' && (!checkboxSelection || !rowCheckbox)) {
|
|
166
|
+
await this.keyboardNavigateToRow({
|
|
167
|
+
row,
|
|
168
|
+
selectionOnNav: selectionBehavior === 'replace' ? 'none' : 'default'
|
|
169
|
+
});
|
|
170
|
+
if (selectionBehavior === 'replace') {
|
|
171
|
+
await this.user.keyboard(`[${altKey}>]`);
|
|
172
|
+
}
|
|
173
|
+
await this.user.keyboard('[Space]');
|
|
174
|
+
if (selectionBehavior === 'replace') {
|
|
175
|
+
await this.user.keyboard(`[/${altKey}]`);
|
|
176
|
+
}
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (rowCheckbox && checkboxSelection) {
|
|
180
|
+
await pressElement(this.user, rowCheckbox, interactionType);
|
|
181
|
+
} else {
|
|
182
|
+
let cell = within(row).getAllByRole('gridcell')[0];
|
|
183
|
+
if (needsLongPress && interactionType === 'touch') {
|
|
184
|
+
// Note that long press interactions with rows is strictly touch only for grid rows
|
|
185
|
+
await triggerLongPress({
|
|
186
|
+
element: cell,
|
|
187
|
+
advanceTimer: this._advanceTimer!,
|
|
188
|
+
pointerOpts: {pointerType: 'touch'}
|
|
189
|
+
});
|
|
190
|
+
} else {
|
|
191
|
+
if (selectionBehavior === 'replace' && interactionType !== 'touch') {
|
|
192
|
+
await this.user.keyboard(`[${metaKey}>]`);
|
|
193
|
+
}
|
|
194
|
+
await pressElement(this.user, cell, interactionType);
|
|
195
|
+
if (selectionBehavior === 'replace' && interactionType !== 'touch') {
|
|
196
|
+
await this.user.keyboard(`[/${metaKey}]`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Toggles the expansion for the specified tree row. Defaults to using the interaction type set on
|
|
204
|
+
* the tree tester.
|
|
205
|
+
*/
|
|
206
|
+
async toggleRowExpansion(opts: TableToggleExpansionOpts): Promise<void> {
|
|
207
|
+
let {row, interactionType = this._interactionType} = opts;
|
|
208
|
+
if (!this.getTable().contains(document.activeElement)) {
|
|
209
|
+
await act(async () => {
|
|
210
|
+
this.getTable().focus();
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
215
|
+
row = this.findRow({indexOrText: row});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!row) {
|
|
219
|
+
throw new Error(`Target row "${formatTargetNode(opts.row)}" not found in the table.`);
|
|
220
|
+
} else if (row.getAttribute('aria-expanded') == null) {
|
|
221
|
+
throw new Error(`Target row "${formatTargetNode(opts.row)}" is not expandable.`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (row.getAttribute('aria-disabled') === 'true') {
|
|
225
|
+
throw new Error(`Cannot toggle expansion on disabled row "${formatTargetNode(opts.row)}".`);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (interactionType === 'mouse' || interactionType === 'touch') {
|
|
229
|
+
let rowExpander = within(row).getAllByRole('button')[0]; // what happens if the button is not first? how can we differentiate?
|
|
230
|
+
await pressElement(this.user, rowExpander, interactionType);
|
|
231
|
+
} else if (interactionType === 'keyboard') {
|
|
232
|
+
// note that our keyboard navigation makes sure selection isn't changes
|
|
233
|
+
await this.keyboardNavigateToRow({row});
|
|
234
|
+
let collapseKey = this._direction === 'rtl' ? 'ArrowRight' : 'ArrowLeft';
|
|
235
|
+
let expandKey = this._direction === 'rtl' ? 'ArrowLeft' : 'ArrowRight';
|
|
236
|
+
if (row.getAttribute('aria-expanded') === 'true') {
|
|
237
|
+
await this.user.keyboard(`[${collapseKey}]`);
|
|
238
|
+
} else {
|
|
239
|
+
await this.user.keyboard(`[${expandKey}]`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Toggles the sort order for the specified table column. Defaults to using the interaction type
|
|
246
|
+
* set on the table tester.
|
|
247
|
+
*/
|
|
248
|
+
async toggleSort(opts: TableToggleSortOpts): Promise<void> {
|
|
249
|
+
let {column, interactionType = this._interactionType} = opts;
|
|
250
|
+
|
|
251
|
+
let columnheader;
|
|
252
|
+
if (typeof column === 'number') {
|
|
253
|
+
columnheader = this.getColumns()[column];
|
|
254
|
+
} else if (typeof column === 'string') {
|
|
255
|
+
columnheader = within(this.getRowGroups()[0]).getByText(column);
|
|
256
|
+
while (columnheader && !/columnheader/.test(columnheader.getAttribute('role'))) {
|
|
257
|
+
columnheader = columnheader.parentElement;
|
|
258
|
+
}
|
|
259
|
+
} else {
|
|
260
|
+
columnheader = column;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let menuButton = within(columnheader).queryByRole('button');
|
|
264
|
+
if (menuButton) {
|
|
265
|
+
let currentSort = columnheader.getAttribute('aria-sort');
|
|
266
|
+
// TODO: Focus management is all kinda of messed up if I just use .focus and Space to open the sort menu. Seems like
|
|
267
|
+
// the focused key doesn't get properly set to the desired column header. Have to do this strange flow where I focus the
|
|
268
|
+
// column header except if the active element is already the menu button within the column header
|
|
269
|
+
if (interactionType === 'keyboard' && document.activeElement !== menuButton) {
|
|
270
|
+
await pressElement(this.user, columnheader, interactionType);
|
|
271
|
+
} else {
|
|
272
|
+
await pressElement(this.user, menuButton, interactionType);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
await waitFor(() => {
|
|
276
|
+
if (menuButton.getAttribute('aria-controls') == null) {
|
|
277
|
+
throw new Error('No aria-controls found on table column dropdown menu trigger element.');
|
|
278
|
+
} else {
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
let menuId = menuButton.getAttribute('aria-controls');
|
|
284
|
+
await waitFor(() => {
|
|
285
|
+
if (!menuId || document.getElementById(menuId) == null) {
|
|
286
|
+
throw new Error(`Table column header menu with id of ${menuId} not found in document.`);
|
|
287
|
+
} else {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
if (menuId) {
|
|
293
|
+
let menu = document.getElementById(menuId);
|
|
294
|
+
if (menu) {
|
|
295
|
+
if (currentSort === 'ascending') {
|
|
296
|
+
await pressElement(
|
|
297
|
+
this.user,
|
|
298
|
+
within(menu).getAllByRole('menuitem')[1],
|
|
299
|
+
interactionType
|
|
300
|
+
);
|
|
301
|
+
} else {
|
|
302
|
+
await pressElement(
|
|
303
|
+
this.user,
|
|
304
|
+
within(menu).getAllByRole('menuitem')[0],
|
|
305
|
+
interactionType
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
await waitFor(() => {
|
|
310
|
+
if (document.contains(menu)) {
|
|
311
|
+
throw new Error(
|
|
312
|
+
'Expected table column menu listbox to not be in the document after selecting an option'
|
|
313
|
+
);
|
|
314
|
+
} else {
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Handle cases where the table may transition in response to the row selection/deselection
|
|
322
|
+
await act(async () => {
|
|
323
|
+
await this._advanceTimer!(200);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
await waitFor(() => {
|
|
327
|
+
if (document.activeElement !== menuButton) {
|
|
328
|
+
throw new Error(
|
|
329
|
+
`Expected the document.activeElement to be the table column menu button but got ${document.activeElement}`
|
|
330
|
+
);
|
|
331
|
+
} else {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
} else {
|
|
336
|
+
await pressElement(this.user, columnheader, interactionType);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Triggers an action for the specified table column menu. Defaults to using the interaction type
|
|
342
|
+
* set on the table tester.
|
|
343
|
+
*/
|
|
344
|
+
async triggerColumnHeaderAction(opts: TableColumnHeaderActionOpts): Promise<void> {
|
|
345
|
+
let {column, interactionType = this._interactionType, action} = opts;
|
|
346
|
+
|
|
347
|
+
let columnheader;
|
|
348
|
+
if (typeof column === 'number') {
|
|
349
|
+
columnheader = this.getColumns()[column];
|
|
350
|
+
} else if (typeof column === 'string') {
|
|
351
|
+
columnheader = within(this.getRowGroups()[0]).getByText(column);
|
|
352
|
+
while (columnheader && !/columnheader/.test(columnheader.getAttribute('role'))) {
|
|
353
|
+
columnheader = columnheader.parentElement;
|
|
354
|
+
}
|
|
355
|
+
} else {
|
|
356
|
+
columnheader = column;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
let menuButton = within(columnheader).queryByRole('button');
|
|
360
|
+
if (menuButton) {
|
|
361
|
+
// TODO: Focus management is all kinda of messed up if I just use .focus and Space to open the sort menu. Seems like
|
|
362
|
+
// the focused key doesn't get properly set to the desired column header. Have to do this strange flow where I focus the
|
|
363
|
+
// column header except if the active element is already the menu button within the column header
|
|
364
|
+
if (interactionType === 'keyboard' && document.activeElement !== menuButton) {
|
|
365
|
+
await pressElement(this.user, columnheader, interactionType);
|
|
366
|
+
} else {
|
|
367
|
+
await pressElement(this.user, menuButton, interactionType);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
await waitFor(() => {
|
|
371
|
+
if (menuButton.getAttribute('aria-controls') == null) {
|
|
372
|
+
throw new Error('No aria-controls found on table column dropdown menu trigger element.');
|
|
373
|
+
} else {
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
let menuId = menuButton.getAttribute('aria-controls');
|
|
379
|
+
await waitFor(() => {
|
|
380
|
+
if (!menuId || document.getElementById(menuId) == null) {
|
|
381
|
+
throw new Error(`Table column header menu with id of ${menuId} not found in document.`);
|
|
382
|
+
} else {
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
if (menuId) {
|
|
388
|
+
let menu = document.getElementById(menuId);
|
|
389
|
+
if (menu) {
|
|
390
|
+
await pressElement(
|
|
391
|
+
this.user,
|
|
392
|
+
within(menu).getAllByRole('menuitem')[action],
|
|
393
|
+
interactionType
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
await waitFor(() => {
|
|
397
|
+
if (document.contains(menu)) {
|
|
398
|
+
throw new Error(
|
|
399
|
+
'Expected table column menu listbox to not be in the document after selecting an option'
|
|
400
|
+
);
|
|
401
|
+
} else {
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Handle cases where the table may transition in response to the row selection/deselection
|
|
409
|
+
await act(async () => {
|
|
410
|
+
await this._advanceTimer!(200);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
await waitFor(() => {
|
|
414
|
+
if (document.activeElement !== menuButton) {
|
|
415
|
+
throw new Error(
|
|
416
|
+
`Expected the document.activeElement to be the table column menu button but got ${document.activeElement}`
|
|
417
|
+
);
|
|
418
|
+
} else {
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
} else {
|
|
423
|
+
throw new Error('No menu button found on table column header.');
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Triggers the action for the specified table row. Defaults to using the interaction type set on
|
|
429
|
+
* the table tester.
|
|
430
|
+
*/
|
|
431
|
+
async triggerRowAction(opts: TableRowActionOpts): Promise<void> {
|
|
432
|
+
let {row, needsDoubleClick, interactionType = this._interactionType} = opts;
|
|
433
|
+
|
|
434
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
435
|
+
row = this.findRow({indexOrText: row});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (!row) {
|
|
439
|
+
throw new Error(`Target row "${formatTargetNode(opts.row)}" not found in the table.`);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (row.getAttribute('aria-disabled') === 'true') {
|
|
443
|
+
throw new Error(`Cannot trigger row action on disabled row "${formatTargetNode(opts.row)}".`);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (needsDoubleClick) {
|
|
447
|
+
await this.user.dblClick(row);
|
|
448
|
+
} else if (interactionType === 'keyboard') {
|
|
449
|
+
await this.keyboardNavigateToRow({row, selectionOnNav: 'none'});
|
|
450
|
+
await this.user.keyboard('[Enter]');
|
|
451
|
+
} else {
|
|
452
|
+
await pressElement(this.user, row, interactionType);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// 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
|
|
457
|
+
// add much value if we provide that to them
|
|
458
|
+
/**
|
|
459
|
+
* Toggle selection for all rows in the table. Defaults to using the interaction type set on the
|
|
460
|
+
* table tester.
|
|
461
|
+
*/
|
|
462
|
+
async toggleSelectAll(opts: {interactionType?: UserOpts['interactionType']} = {}): Promise<void> {
|
|
463
|
+
let {interactionType = this._interactionType} = opts;
|
|
464
|
+
if (interactionType === 'keyboard') {
|
|
465
|
+
let metaKey = getMetaKey();
|
|
466
|
+
let table = this.getTable();
|
|
467
|
+
if (document.activeElement !== table && !table.contains(document.activeElement)) {
|
|
468
|
+
act(() => table.focus());
|
|
469
|
+
}
|
|
470
|
+
await this.user.keyboard(`[${metaKey}>]a[/${metaKey}]`);
|
|
471
|
+
} else {
|
|
472
|
+
let checkbox = within(this.getTable()).getByLabelText('Select All');
|
|
473
|
+
await pressElement(this.user, checkbox, interactionType);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Returns a row matching the specified index or text content.
|
|
479
|
+
*/
|
|
480
|
+
findRow(opts: {indexOrText: number | string}): HTMLElement {
|
|
481
|
+
let {indexOrText} = opts;
|
|
482
|
+
|
|
483
|
+
let row;
|
|
484
|
+
let rows = this.getRows();
|
|
485
|
+
let bodyRowGroup = this.getRowGroups()[1];
|
|
486
|
+
if (typeof indexOrText === 'number') {
|
|
487
|
+
row = rows[indexOrText];
|
|
488
|
+
} else if (typeof indexOrText === 'string') {
|
|
489
|
+
row = within(bodyRowGroup).getByText(indexOrText);
|
|
490
|
+
while (row && row.getAttribute('role') !== 'row') {
|
|
491
|
+
row = row.parentElement;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return row;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Returns a cell matching the specified text content.
|
|
500
|
+
*/
|
|
501
|
+
findCell(opts: {text: string}): HTMLElement {
|
|
502
|
+
let {text} = opts;
|
|
503
|
+
|
|
504
|
+
let cell = within(this.getTable()).getByText(text);
|
|
505
|
+
if (cell) {
|
|
506
|
+
while (cell && !/gridcell|rowheader|columnheader/.test(cell.getAttribute('role') || '')) {
|
|
507
|
+
if (cell.parentElement) {
|
|
508
|
+
cell = cell.parentElement;
|
|
509
|
+
} else {
|
|
510
|
+
break;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
return cell;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Returns the table.
|
|
520
|
+
*/
|
|
521
|
+
getTable(): HTMLElement {
|
|
522
|
+
return this._table;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Returns the row groups within the table.
|
|
527
|
+
*/
|
|
528
|
+
getRowGroups(): HTMLElement[] {
|
|
529
|
+
let table = this._table;
|
|
530
|
+
return table ? within(table).queryAllByRole('rowgroup') : [];
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Returns the columns within the table.
|
|
535
|
+
*/
|
|
536
|
+
getColumns(): HTMLElement[] {
|
|
537
|
+
let headerRowGroup = this.getRowGroups()[0];
|
|
538
|
+
return headerRowGroup ? within(headerRowGroup).queryAllByRole('columnheader') : [];
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Returns the rows within the table if any. Can be filtered to a specific row group if provided
|
|
543
|
+
* via `element`.
|
|
544
|
+
*/
|
|
545
|
+
getRows(opts: {element?: HTMLElement} = {}): HTMLElement[] {
|
|
546
|
+
let {element} = opts;
|
|
547
|
+
if (element != null) {
|
|
548
|
+
return within(element).queryAllByRole('row');
|
|
549
|
+
}
|
|
550
|
+
return this.getRowGroups()
|
|
551
|
+
.slice(1)
|
|
552
|
+
.flatMap(rowGroup => within(rowGroup).queryAllByRole('row'));
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Returns the currently selected rows within the table if any.
|
|
557
|
+
*/
|
|
558
|
+
getSelectedRows(): HTMLElement[] {
|
|
559
|
+
return this.getRows().filter(row => row.getAttribute('aria-selected') === 'true');
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Returns the footer rows within the table if any.
|
|
564
|
+
*/
|
|
565
|
+
getFooterRows(): HTMLElement[] {
|
|
566
|
+
let rowGroups = this.getRowGroups();
|
|
567
|
+
if (rowGroups.length < 3) {
|
|
568
|
+
return [];
|
|
569
|
+
}
|
|
570
|
+
let footerRowGroup = rowGroups.at(-1);
|
|
571
|
+
return footerRowGroup ? within(footerRowGroup).queryAllByRole('row') : [];
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Returns the row headers within the table if any.
|
|
576
|
+
*/
|
|
577
|
+
getRowHeaders(): HTMLElement[] {
|
|
578
|
+
return within(this.getTable()).queryAllByRole('rowheader');
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Returns the cells within the table if any. Can be filtered against a specific row if provided
|
|
583
|
+
* via `element`.
|
|
584
|
+
*/
|
|
585
|
+
getCells(opts: {element?: HTMLElement} = {}): HTMLElement[] {
|
|
586
|
+
let {element = this.getTable()} = opts;
|
|
587
|
+
return within(element).queryAllByRole('gridcell');
|
|
588
|
+
}
|
|
589
|
+
}
|