@react-aria/test-utils 1.0.0-alpha.3 → 1.0.0-alpha.5

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 (67) hide show
  1. package/dist/combobox.main.js +116 -82
  2. package/dist/combobox.main.js.map +1 -1
  3. package/dist/combobox.mjs +117 -83
  4. package/dist/combobox.module.js +117 -83
  5. package/dist/combobox.module.js.map +1 -1
  6. package/dist/events.main.js +42 -4
  7. package/dist/events.main.js.map +1 -1
  8. package/dist/events.mjs +42 -4
  9. package/dist/events.module.js +42 -4
  10. package/dist/events.module.js.map +1 -1
  11. package/dist/gridlist.main.js +102 -59
  12. package/dist/gridlist.main.js.map +1 -1
  13. package/dist/gridlist.mjs +103 -60
  14. package/dist/gridlist.module.js +103 -60
  15. package/dist/gridlist.module.js.map +1 -1
  16. package/dist/listbox.main.js +135 -0
  17. package/dist/listbox.main.js.map +1 -0
  18. package/dist/listbox.mjs +130 -0
  19. package/dist/listbox.module.js +130 -0
  20. package/dist/listbox.module.js.map +1 -0
  21. package/dist/main.js.map +1 -1
  22. package/dist/menu.main.js +195 -161
  23. package/dist/menu.main.js.map +1 -1
  24. package/dist/menu.mjs +196 -162
  25. package/dist/menu.module.js +196 -162
  26. package/dist/menu.module.js.map +1 -1
  27. package/dist/module.js.map +1 -1
  28. package/dist/select.main.js +116 -70
  29. package/dist/select.main.js.map +1 -1
  30. package/dist/select.mjs +117 -71
  31. package/dist/select.module.js +117 -71
  32. package/dist/select.module.js.map +1 -1
  33. package/dist/table.main.js +166 -139
  34. package/dist/table.main.js.map +1 -1
  35. package/dist/table.mjs +167 -140
  36. package/dist/table.module.js +167 -140
  37. package/dist/table.module.js.map +1 -1
  38. package/dist/tabs.main.js +133 -0
  39. package/dist/tabs.main.js.map +1 -0
  40. package/dist/tabs.mjs +128 -0
  41. package/dist/tabs.module.js +128 -0
  42. package/dist/tabs.module.js.map +1 -0
  43. package/dist/tree.main.js +165 -0
  44. package/dist/tree.main.js.map +1 -0
  45. package/dist/tree.mjs +160 -0
  46. package/dist/tree.module.js +160 -0
  47. package/dist/tree.module.js.map +1 -0
  48. package/dist/types.d.ts +607 -145
  49. package/dist/types.d.ts.map +1 -1
  50. package/dist/user.main.js +15 -4
  51. package/dist/user.main.js.map +1 -1
  52. package/dist/user.mjs +15 -4
  53. package/dist/user.module.js +15 -4
  54. package/dist/user.module.js.map +1 -1
  55. package/package.json +3 -4
  56. package/src/combobox.ts +105 -36
  57. package/src/events.ts +29 -4
  58. package/src/gridlist.ts +146 -59
  59. package/src/index.ts +1 -1
  60. package/src/listbox.ts +226 -0
  61. package/src/menu.ts +152 -62
  62. package/src/select.ts +137 -44
  63. package/src/table.ts +130 -69
  64. package/src/tabs.ts +198 -0
  65. package/src/tree.ts +248 -0
  66. package/src/types.ts +133 -0
  67. package/src/user.ts +62 -37
package/src/table.ts CHANGED
@@ -10,24 +10,30 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {act, fireEvent, waitFor, within} from '@testing-library/react';
14
- import {BaseTesterOpts, UserOpts} from './user';
13
+ import {act, waitFor, within} from '@testing-library/react';
14
+ import {GridRowActionOpts, TableTesterOpts, ToggleGridRowOpts, UserOpts} from './types';
15
15
  import {pressElement, triggerLongPress} from './events';
16
- export interface TableOptions extends UserOpts, BaseTesterOpts {
17
- user?: any,
18
- advanceTimer: UserOpts['advanceTimer']
16
+
17
+ interface TableToggleRowOpts extends ToggleGridRowOpts {}
18
+ interface TableToggleSortOpts {
19
+ /**
20
+ * The index, text, or node of the column to toggle selection for.
21
+ */
22
+ column: number | string | HTMLElement,
23
+ /**
24
+ * What interaction type to use when sorting the column. Defaults to the interaction type set on the tester.
25
+ */
26
+ interactionType?: UserOpts['interactionType']
19
27
  }
28
+ interface TableRowActionOpts extends GridRowActionOpts {}
20
29
 
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
30
  export class TableTester {
25
31
  private user;
26
32
  private _interactionType: UserOpts['interactionType'];
27
33
  private _advanceTimer: UserOpts['advanceTimer'];
28
34
  private _table: HTMLElement;
29
35
 
30
- constructor(opts: TableOptions) {
36
+ constructor(opts: TableTesterOpts) {
31
37
  let {root, user, interactionType, advanceTimer} = opts;
32
38
  this.user = user;
33
39
  this._interactionType = interactionType || 'mouse';
@@ -35,21 +41,43 @@ export class TableTester {
35
41
  this._table = root;
36
42
  }
37
43
 
38
- setInteractionType = (type: UserOpts['interactionType']) => {
44
+ /**
45
+ * Set the interaction type used by the table tester.
46
+ */
47
+ setInteractionType(type: UserOpts['interactionType']) {
39
48
  this._interactionType = type;
40
- };
49
+ }
41
50
 
42
- toggleRowSelection = async (opts: {index?: number, text?: string, needsLongPress?: boolean, interactionType?: UserOpts['interactionType']} = {}) => {
51
+ /**
52
+ * Toggles the selection for the specified table row. Defaults to using the interaction type set on the table tester.
53
+ */
54
+ async toggleRowSelection(opts: TableToggleRowOpts) {
43
55
  let {
44
- index,
45
- text,
56
+ row,
46
57
  needsLongPress,
58
+ checkboxSelection = true,
47
59
  interactionType = this._interactionType
48
60
  } = opts;
49
61
 
50
- let row = this.findRow({index, text});
62
+ if (typeof row === 'string' || typeof row === 'number') {
63
+ row = this.findRow({rowIndexOrText: row});
64
+ }
65
+
66
+ if (!row) {
67
+ throw new Error('Target row not found in the table.');
68
+ }
69
+
51
70
  let rowCheckbox = within(row).queryByRole('checkbox');
52
- if (rowCheckbox) {
71
+
72
+ if (interactionType === 'keyboard' && !checkboxSelection) {
73
+ // TODO: for now focus the row directly until I add keyboard navigation
74
+ await act(async () => {
75
+ row.focus();
76
+ });
77
+ await this.user.keyboard('{Space}');
78
+ return;
79
+ }
80
+ if (rowCheckbox && checkboxSelection) {
53
81
  await pressElement(this.user, rowCheckbox, interactionType);
54
82
  } else {
55
83
  let cell = within(row).getAllByRole('gridcell')[0];
@@ -60,31 +88,31 @@ export class TableTester {
60
88
 
61
89
  // Note that long press interactions with rows is strictly touch only for grid rows
62
90
  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
91
  } else {
68
92
  await pressElement(this.user, cell, interactionType);
69
93
  }
70
94
  }
71
95
  };
72
96
 
73
- toggleSort = async (opts: {index?: number, text?: string, interactionType?: UserOpts['interactionType']} = {}) => {
97
+ /**
98
+ * Toggles the sort order for the specified table column. Defaults to using the interaction type set on the table tester.
99
+ */
100
+ async toggleSort(opts: TableToggleSortOpts) {
74
101
  let {
75
- index,
76
- text,
102
+ column,
77
103
  interactionType = this._interactionType
78
104
  } = opts;
79
105
 
80
106
  let columnheader;
81
- if (index != null) {
82
- columnheader = this.columns[index];
83
- } else if (text != null) {
84
- columnheader = within(this.rowGroups[0]).getByText(text);
107
+ if (typeof column === 'number') {
108
+ columnheader = this.columns[column];
109
+ } else if (typeof column === 'string') {
110
+ columnheader = within(this.rowGroups[0]).getByText(column);
85
111
  while (columnheader && !/columnheader/.test(columnheader.getAttribute('role'))) {
86
112
  columnheader = columnheader.parentElement;
87
113
  }
114
+ } else {
115
+ columnheader = column;
88
116
  }
89
117
 
90
118
  let menuButton = within(columnheader).queryByRole('button');
@@ -154,38 +182,46 @@ export class TableTester {
154
182
  } else {
155
183
  await pressElement(this.user, columnheader, interactionType);
156
184
  }
157
- };
158
- // 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
159
- // table. Maybe we could rely on the user knowing to trigger a press/double click? We could have the user pass in "needsDoubleClick"
160
- // 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
161
- triggerRowAction = async (opts: {index?: number, text?: string, needsDoubleClick?: boolean, interactionType?: UserOpts['interactionType']} = {}) => {
185
+ }
186
+
187
+ /**
188
+ * Triggers the action for the specified table row. Defaults to using the interaction type set on the table tester.
189
+ */
190
+ async triggerRowAction(opts: TableRowActionOpts) {
162
191
  let {
163
- index,
164
- text,
192
+ row,
165
193
  needsDoubleClick,
166
194
  interactionType = this._interactionType
167
195
  } = opts;
168
196
 
169
- let row = this.findRow({index, text});
170
- if (row) {
171
- if (needsDoubleClick) {
172
- await this.user.dblClick(row);
173
- } else if (interactionType === 'keyboard') {
174
- act(() => row.focus());
175
- await this.user.keyboard('[Enter]');
176
- } else {
177
- await pressElement(this.user, row, interactionType);
178
- }
197
+ if (typeof row === 'string' || typeof row === 'number') {
198
+ row = this.findRow({rowIndexOrText: row});
179
199
  }
180
- };
200
+
201
+ if (!row) {
202
+ throw new Error('Target row not found in the table.');
203
+ }
204
+
205
+ if (needsDoubleClick) {
206
+ await this.user.dblClick(row);
207
+ } else if (interactionType === 'keyboard') {
208
+ // TODO: add keyboard navigation instead of focusing the row directly. Will need to consider if the focus in in the columns
209
+ act(() => row.focus());
210
+ await this.user.keyboard('[Enter]');
211
+ } else {
212
+ await pressElement(this.user, row, interactionType);
213
+ }
214
+ }
181
215
 
182
216
  // 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.
183
217
  // 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
184
218
  // on the mocks the user sets up for their row height/etc.
185
219
  // 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
186
220
  // add much value if we provide that to them
187
-
188
- toggleSelectAll = async (opts: {interactionType?: UserOpts['interactionType']} = {}) => {
221
+ /**
222
+ * Toggle selection for all rows in the table. Defaults to using the interaction type set on the table tester.
223
+ */
224
+ async toggleSelectAll(opts: {interactionType?: UserOpts['interactionType']} = {}) {
189
225
  let {
190
226
  interactionType = this._interactionType
191
227
  } = opts;
@@ -196,30 +232,35 @@ export class TableTester {
196
232
  } else {
197
233
  await pressElement(this.user, checkbox, interactionType);
198
234
  }
199
- };
235
+ }
200
236
 
201
- findRow = (opts: {index?: number, text?: string} = {}) => {
237
+ /**
238
+ * Returns a row matching the specified index or text content.
239
+ */
240
+ findRow(opts: {rowIndexOrText: number | string}): HTMLElement {
202
241
  let {
203
- index,
204
- text
242
+ rowIndexOrText
205
243
  } = opts;
206
244
 
207
245
  let row;
208
246
  let rows = this.rows;
209
247
  let bodyRowGroup = this.rowGroups[1];
210
- if (index != null) {
211
- row = rows[index];
212
- } else if (text != null) {
213
- row = within(bodyRowGroup).getByText(text);
248
+ if (typeof rowIndexOrText === 'number') {
249
+ row = rows[rowIndexOrText];
250
+ } else if (typeof rowIndexOrText === 'string') {
251
+ row = within(bodyRowGroup).getByText(rowIndexOrText);
214
252
  while (row && row.getAttribute('role') !== 'row') {
215
253
  row = row.parentElement;
216
254
  }
217
255
  }
218
256
 
219
257
  return row;
220
- };
258
+ }
221
259
 
222
- findCell = (opts: {text: string}) => {
260
+ /**
261
+ * Returns a cell matching the specified text content.
262
+ */
263
+ findCell(opts: {text: string}) {
223
264
  let {
224
265
  text
225
266
  } = opts;
@@ -236,38 +277,58 @@ export class TableTester {
236
277
  }
237
278
 
238
279
  return cell;
239
- };
280
+ }
240
281
 
241
- get table() {
282
+ /**
283
+ * Returns the table.
284
+ */
285
+ get table(): HTMLElement {
242
286
  return this._table;
243
287
  }
244
288
 
245
- get rowGroups() {
289
+ /**
290
+ * Returns the row groups within the table.
291
+ */
292
+ get rowGroups(): HTMLElement[] {
246
293
  let table = this._table;
247
294
  return table ? within(table).queryAllByRole('rowgroup') : [];
248
295
  }
249
296
 
250
- get columns() {
297
+ /**
298
+ * Returns the columns within the table.
299
+ */
300
+ get columns(): HTMLElement[] {
251
301
  let headerRowGroup = this.rowGroups[0];
252
302
  return headerRowGroup ? within(headerRowGroup).queryAllByRole('columnheader') : [];
253
303
  }
254
304
 
255
- get rows() {
305
+ /**
306
+ * Returns the rows within the table if any.
307
+ */
308
+ get rows(): HTMLElement[] {
256
309
  let bodyRowGroup = this.rowGroups[1];
257
310
  return bodyRowGroup ? within(bodyRowGroup).queryAllByRole('row') : [];
258
311
  }
259
312
 
260
- get selectedRows() {
313
+ /**
314
+ * Returns the currently selected rows within the table if any.
315
+ */
316
+ get selectedRows(): HTMLElement[] {
261
317
  return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');
262
318
  }
263
319
 
264
- get rowHeaders() {
265
- let table = this.table;
266
- return table ? within(table).queryAllByRole('rowheader') : [];
320
+ /**
321
+ * Returns the row headers within the table if any.
322
+ */
323
+ get rowHeaders(): HTMLElement[] {
324
+ return within(this.table).queryAllByRole('rowheader');
267
325
  }
268
326
 
269
- get cells() {
270
- let table = this.table;
271
- return table ? within(table).queryAllByRole('gridcell') : [];
327
+ /**
328
+ * Returns the cells within the table if any. Can be filtered against a specific row if provided via `element`.
329
+ */
330
+ cells(opts: {element?: HTMLElement} = {}): HTMLElement[] {
331
+ let {element = this.table} = opts;
332
+ return within(element).queryAllByRole('gridcell');
272
333
  }
273
334
  }
package/src/tabs.ts ADDED
@@ -0,0 +1,198 @@
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 {Direction, Orientation, TabsTesterOpts, UserOpts} from './types';
15
+ import {pressElement} from './events';
16
+
17
+ interface TriggerTabOptions {
18
+ /**
19
+ * What interaction type to use when triggering a tab. Defaults to the interaction type set on the tester.
20
+ */
21
+ interactionType?: UserOpts['interactionType'],
22
+ /**
23
+ * The index, text, or node of the tab to toggle selection for.
24
+ */
25
+ tab: number | string | HTMLElement,
26
+ /**
27
+ * Whether the tab needs to be activated manually rather than on focus.
28
+ */
29
+ manualActivation?: boolean
30
+ }
31
+
32
+ export class TabsTester {
33
+ private user;
34
+ private _interactionType: UserOpts['interactionType'];
35
+ private _tablist: HTMLElement;
36
+ private _direction: Direction;
37
+
38
+ constructor(opts: TabsTesterOpts) {
39
+ let {root, user, interactionType, direction} = opts;
40
+ this.user = user;
41
+ this._interactionType = interactionType || 'mouse';
42
+ this._direction = direction || 'ltr';
43
+
44
+ this._tablist = root;
45
+ let tablist = within(root).queryAllByRole('tablist');
46
+ if (tablist.length > 0) {
47
+ this._tablist = tablist[0];
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Set the interaction type used by the tabs tester.
53
+ */
54
+ setInteractionType(type: UserOpts['interactionType']) {
55
+ this._interactionType = type;
56
+ }
57
+
58
+ // TODO: This is pretty similar across most the utils, refactor to make it generic?
59
+ /**
60
+ * Returns a tab matching the specified index or text content.
61
+ */
62
+ findTab(opts: {tabIndexOrText: number | string}): HTMLElement {
63
+ let {
64
+ tabIndexOrText
65
+ } = opts;
66
+
67
+ let tab;
68
+ let tabs = this.tabs;
69
+ if (typeof tabIndexOrText === 'number') {
70
+ tab = tabs[tabIndexOrText];
71
+ } else if (typeof tabIndexOrText === 'string') {
72
+ tab = (within(this._tablist).getByText(tabIndexOrText).closest('[role=tab]'))! as HTMLElement;
73
+ }
74
+
75
+ return tab;
76
+ }
77
+
78
+ // TODO: also quite similar across more utils albeit with orientation, refactor to make generic
79
+ private async keyboardNavigateToTab(opts: {tab: HTMLElement, orientation?: Orientation}) {
80
+ let {tab, orientation = 'vertical'} = opts;
81
+ let tabs = this.tabs;
82
+ let targetIndex = tabs.indexOf(tab);
83
+ if (targetIndex === -1) {
84
+ throw new Error('Tab provided is not in the tablist');
85
+ }
86
+
87
+ if (!this._tablist.contains(document.activeElement)) {
88
+ let selectedTab = this.selectedTab;
89
+ if (selectedTab != null) {
90
+ act(() => selectedTab.focus());
91
+ } else {
92
+ act(() => tabs.find(tab => !(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'))?.focus());
93
+ }
94
+ }
95
+
96
+ let currIndex = this.tabs.indexOf(document.activeElement as HTMLElement);
97
+ if (currIndex === -1) {
98
+ throw new Error('ActiveElement is not in the tablist');
99
+ }
100
+
101
+ let arrowUp = 'ArrowUp';
102
+ let arrowDown = 'ArrowDown';
103
+ if (orientation === 'horizontal') {
104
+ if (this._direction === 'ltr') {
105
+ arrowUp = 'ArrowLeft';
106
+ arrowDown = 'ArrowRight';
107
+ } else {
108
+ arrowUp = 'ArrowRight';
109
+ arrowDown = 'ArrowLeft';
110
+ }
111
+ }
112
+
113
+ let movementDirection = targetIndex > currIndex ? 'down' : 'up';
114
+ for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
115
+ await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
116
+ }
117
+ };
118
+
119
+ /**
120
+ * Triggers the specified tab. Defaults to using the interaction type set on the tabs tester.
121
+ */
122
+ async triggerTab(opts: TriggerTabOptions) {
123
+ let {
124
+ tab,
125
+ interactionType = this._interactionType,
126
+ manualActivation
127
+ } = opts;
128
+
129
+ if (typeof tab === 'string' || typeof tab === 'number') {
130
+ tab = this.findTab({tabIndexOrText: tab});
131
+ }
132
+
133
+ if (!tab) {
134
+ throw new Error('Target tab not found in the tablist.');
135
+ } else if (tab.hasAttribute('disabled')) {
136
+ throw new Error('Target tab is disabled.');
137
+ }
138
+
139
+ if (interactionType === 'keyboard') {
140
+ if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) {
141
+ act(() => this._tablist.focus());
142
+ }
143
+
144
+ let tabsOrientation = this._tablist.getAttribute('aria-orientation') || 'horizontal';
145
+ await this.keyboardNavigateToTab({tab, orientation: tabsOrientation as Orientation});
146
+ if (manualActivation) {
147
+ await this.user.keyboard('[Enter]');
148
+ }
149
+ } else {
150
+ await pressElement(this.user, tab, interactionType);
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Returns the tablist.
156
+ */
157
+ get tablist(): HTMLElement {
158
+ return this._tablist;
159
+ }
160
+
161
+ /**
162
+ * Returns the tabpanels.
163
+ */
164
+ get tabpanels(): HTMLElement[] {
165
+ let tabpanels = [] as HTMLElement[];
166
+ for (let tab of this.tabs) {
167
+ let controlId = tab.getAttribute('aria-controls');
168
+ let panel = controlId != null ? document.getElementById(controlId) : null;
169
+ if (panel != null) {
170
+ tabpanels.push(panel);
171
+ }
172
+ }
173
+
174
+ return tabpanels;
175
+ }
176
+
177
+ /**
178
+ * Returns the tabs in the tablist.
179
+ */
180
+ get tabs(): HTMLElement[] {
181
+ return within(this.tablist).queryAllByRole('tab');
182
+ }
183
+
184
+ /**
185
+ * Returns the currently selected tab in the tablist if any.
186
+ */
187
+ get selectedTab(): HTMLElement | null {
188
+ return this.tabs.find(tab => tab.getAttribute('aria-selected') === 'true') || null;
189
+ }
190
+
191
+ /**
192
+ * Returns the currently active tabpanel if any.
193
+ */
194
+ get activeTabpanel(): HTMLElement | null {
195
+ let activeTabpanelId = this.selectedTab?.getAttribute('aria-controls');
196
+ return activeTabpanelId ? document.getElementById(activeTabpanelId) : null;
197
+ }
198
+ }