@react-aria/test-utils 1.0.0-alpha.2 → 1.0.0-alpha.4

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 (71) hide show
  1. package/LICENSE +201 -0
  2. package/dist/combobox.main.js +116 -82
  3. package/dist/combobox.main.js.map +1 -1
  4. package/dist/combobox.mjs +117 -83
  5. package/dist/combobox.module.js +117 -83
  6. package/dist/combobox.module.js.map +1 -1
  7. package/dist/events.main.js.map +1 -1
  8. package/dist/events.module.js.map +1 -1
  9. package/dist/gridlist.main.js +102 -59
  10. package/dist/gridlist.main.js.map +1 -1
  11. package/dist/gridlist.mjs +103 -60
  12. package/dist/gridlist.module.js +103 -60
  13. package/dist/gridlist.module.js.map +1 -1
  14. package/dist/listbox.main.js +135 -0
  15. package/dist/listbox.main.js.map +1 -0
  16. package/dist/listbox.mjs +130 -0
  17. package/dist/listbox.module.js +130 -0
  18. package/dist/listbox.module.js.map +1 -0
  19. package/dist/main.js.map +1 -1
  20. package/dist/menu.main.js +197 -119
  21. package/dist/menu.main.js.map +1 -1
  22. package/dist/menu.mjs +198 -120
  23. package/dist/menu.module.js +198 -120
  24. package/dist/menu.module.js.map +1 -1
  25. package/dist/module.js.map +1 -1
  26. package/dist/select.main.js +116 -70
  27. package/dist/select.main.js.map +1 -1
  28. package/dist/select.mjs +117 -71
  29. package/dist/select.module.js +117 -71
  30. package/dist/select.module.js.map +1 -1
  31. package/dist/table.main.js +170 -143
  32. package/dist/table.main.js.map +1 -1
  33. package/dist/table.mjs +171 -144
  34. package/dist/table.module.js +171 -144
  35. package/dist/table.module.js.map +1 -1
  36. package/dist/tabs.main.js +133 -0
  37. package/dist/tabs.main.js.map +1 -0
  38. package/dist/tabs.mjs +128 -0
  39. package/dist/tabs.module.js +128 -0
  40. package/dist/tabs.module.js.map +1 -0
  41. package/dist/testSetup.main.js +2 -6
  42. package/dist/testSetup.main.js.map +1 -1
  43. package/dist/testSetup.mjs +2 -6
  44. package/dist/testSetup.module.js +2 -6
  45. package/dist/testSetup.module.js.map +1 -1
  46. package/dist/tree.main.js +165 -0
  47. package/dist/tree.main.js.map +1 -0
  48. package/dist/tree.mjs +160 -0
  49. package/dist/tree.module.js +160 -0
  50. package/dist/tree.module.js.map +1 -0
  51. package/dist/types.d.ts +607 -139
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/user.main.js +16 -5
  54. package/dist/user.main.js.map +1 -1
  55. package/dist/user.mjs +16 -5
  56. package/dist/user.module.js +16 -5
  57. package/dist/user.module.js.map +1 -1
  58. package/package.json +5 -5
  59. package/src/combobox.ts +105 -36
  60. package/src/events.ts +1 -1
  61. package/src/gridlist.ts +146 -59
  62. package/src/index.ts +1 -1
  63. package/src/listbox.ts +226 -0
  64. package/src/menu.ts +207 -55
  65. package/src/select.ts +137 -44
  66. package/src/table.ts +134 -78
  67. package/src/tabs.ts +198 -0
  68. package/src/testSetup.ts +2 -6
  69. package/src/tree.ts +248 -0
  70. package/src/types.ts +133 -0
  71. package/src/user.ts +62 -37
package/dist/menu.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import {triggerLongPress as $5d1eca18f92ad0e6$export$3a22a5a9bc0fd3b} from "./events.mjs";
2
- import {within as $gS6KO$within, act as $gS6KO$act, waitFor as $gS6KO$waitFor} from "@testing-library/react";
2
+ import {act as $gS6KO$act, waitFor as $gS6KO$waitFor, within as $gS6KO$within} from "@testing-library/react";
3
3
 
4
4
  /*
5
5
  * Copyright 2024 Adobe. All rights reserved.
@@ -14,144 +14,221 @@ import {within as $gS6KO$within, act as $gS6KO$act, waitFor as $gS6KO$waitFor} f
14
14
  */
15
15
 
16
16
  class $74b93f0617179929$export$f73bbc9212ed861e {
17
- get trigger() {
18
- return this._trigger;
19
- }
20
- get menu() {
21
- let menuId = this.trigger.getAttribute('aria-controls');
22
- return menuId ? document.getElementById(menuId) : undefined;
23
- }
24
- get options() {
25
- let menu = this.menu;
26
- let options = [];
27
- if (menu) {
28
- options = (0, $gS6KO$within)(menu).queryAllByRole('menuitem');
29
- if (options.length === 0) {
30
- options = (0, $gS6KO$within)(menu).queryAllByRole('menuitemradio');
31
- if (options.length === 0) options = (0, $gS6KO$within)(menu).queryAllByRole('menuitemcheckbox');
32
- }
33
- }
34
- return options;
17
+ /**
18
+ * Set the interaction type used by the menu tester.
19
+ */ setInteractionType(type) {
20
+ this._interactionType = type;
35
21
  }
36
- get sections() {
37
- let menu = this.menu;
38
- if (menu) return (0, $gS6KO$within)(menu).queryAllByRole('group');
39
- else return [];
40
- }
41
- get submenuTriggers() {
42
- let options = this.options;
43
- if (options.length > 0) return this.options.filter((item)=>item.getAttribute('aria-haspopup') != null);
44
- return [];
45
- }
46
- constructor(opts){
47
- this.setInteractionType = (type)=>{
48
- this._interactionType = type;
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
- this.open = async (opts = {})=>{
53
- let { needsLongPress: needsLongPress, interactionType: interactionType = this._interactionType } = opts;
54
- let trigger = this.trigger;
55
- let isDisabled = trigger.hasAttribute('disabled');
56
- if (interactionType === 'mouse' || interactionType === 'touch') {
57
- if (needsLongPress) {
58
- if (this._advanceTimer == null) throw new Error('No advanceTimers provided for long press.');
59
- let pointerType = interactionType === 'mouse' ? 'mouse' : 'touch';
60
- await (0, $5d1eca18f92ad0e6$export$3a22a5a9bc0fd3b)({
61
- element: trigger,
62
- advanceTimer: this._advanceTimer,
63
- pointerOpts: {
64
- pointerType: pointerType
65
- }
66
- });
67
- } else if (interactionType === 'mouse') await this.user.click(trigger);
68
- else await this.user.pointer({
69
- target: trigger,
70
- keys: '[TouchA]'
22
+ // 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
23
+ // One difference will be that it supports long press as well
24
+ /**
25
+ * Opens the menu. Defaults to using the interaction type set on the menu tester.
26
+ */ async open(opts = {}) {
27
+ let { needsLongPress: needsLongPress, interactionType: interactionType = this._interactionType, direction: direction } = opts;
28
+ let trigger = this.trigger;
29
+ let isDisabled = trigger.hasAttribute('disabled');
30
+ if (interactionType === 'mouse' || interactionType === 'touch') {
31
+ if (needsLongPress) {
32
+ if (this._advanceTimer == null) throw new Error('No advanceTimers provided for long press.');
33
+ let pointerType = interactionType === 'mouse' ? 'mouse' : 'touch';
34
+ await (0, $5d1eca18f92ad0e6$export$3a22a5a9bc0fd3b)({
35
+ element: trigger,
36
+ advanceTimer: this._advanceTimer,
37
+ pointerOpts: {
38
+ pointerType: pointerType
39
+ }
71
40
  });
72
- } else if (interactionType === 'keyboard' && !isDisabled) {
41
+ } else if (interactionType === 'mouse') await this.user.click(trigger);
42
+ else await this.user.pointer({
43
+ target: trigger,
44
+ keys: '[TouchA]'
45
+ });
46
+ } else if (interactionType === 'keyboard' && !isDisabled) {
47
+ if (direction === 'up') {
48
+ (0, $gS6KO$act)(()=>trigger.focus());
49
+ await this.user.keyboard('[ArrowUp]');
50
+ } else if (direction === 'down') {
51
+ (0, $gS6KO$act)(()=>trigger.focus());
52
+ await this.user.keyboard('[ArrowDown]');
53
+ } else {
73
54
  (0, $gS6KO$act)(()=>trigger.focus());
74
55
  await this.user.keyboard('[Enter]');
75
56
  }
57
+ }
58
+ await (0, $gS6KO$waitFor)(()=>{
59
+ if (trigger.getAttribute('aria-controls') == null && !isDisabled) throw new Error('No aria-controls found on menu trigger element.');
60
+ else return true;
61
+ });
62
+ if (!isDisabled) {
63
+ let menuId = trigger.getAttribute('aria-controls');
76
64
  await (0, $gS6KO$waitFor)(()=>{
77
- if (trigger.getAttribute('aria-controls') == null && !isDisabled) throw new Error('No aria-controls found on menu trigger element.');
65
+ if (!menuId || document.getElementById(menuId) == null) throw new Error(`Menu with id of ${menuId} not found in document.`);
78
66
  else return true;
79
67
  });
80
- if (!isDisabled) {
81
- let menuId = trigger.getAttribute('aria-controls');
68
+ }
69
+ }
70
+ /**
71
+ * Returns a option matching the specified index or text content.
72
+ */ findOption(opts) {
73
+ let { optionIndexOrText: optionIndexOrText } = opts;
74
+ let option;
75
+ let options = this.options();
76
+ let menu = this.menu;
77
+ if (typeof optionIndexOrText === 'number') option = options[optionIndexOrText];
78
+ else if (typeof optionIndexOrText === 'string' && menu != null) option = (0, $gS6KO$within)(menu).getByText(optionIndexOrText).closest('[role=menuitem], [role=menuitemradio], [role=menuitemcheckbox]');
79
+ return option;
80
+ }
81
+ // TODO: also very similar to select, barring potential long press support
82
+ // Close on select is also kinda specific?
83
+ /**
84
+ * Selects the desired menu option. Defaults to using the interaction type set on the menu tester. If necessary, will open the menu dropdown beforehand.
85
+ * The desired option can be targeted via the option's node, the option's text, or the option's index.
86
+ */ async selectOption(opts) {
87
+ let { menuSelectionMode: menuSelectionMode = 'single', needsLongPress: needsLongPress, closesOnSelect: closesOnSelect = true, option: option, interactionType: interactionType = this._interactionType, keyboardActivation: keyboardActivation = 'Enter' } = opts;
88
+ let trigger = this.trigger;
89
+ if (!trigger.getAttribute('aria-controls') && !trigger.hasAttribute('aria-expanded')) await this.open({
90
+ needsLongPress: needsLongPress
91
+ });
92
+ let menu = this.menu;
93
+ if (!menu) throw new Error('Menu not found.');
94
+ if (menu) {
95
+ if (typeof option === 'string' || typeof option === 'number') option = this.findOption({
96
+ optionIndexOrText: option
97
+ });
98
+ if (!option) throw new Error('Target option not found in the menu.');
99
+ if (interactionType === 'keyboard') {
100
+ if ((option === null || option === void 0 ? void 0 : option.getAttribute('aria-disabled')) === 'true') return;
101
+ if (document.activeElement !== menu || !menu.contains(document.activeElement)) (0, $gS6KO$act)(()=>menu.focus());
102
+ await this.keyboardNavigateToOption({
103
+ option: option
104
+ });
105
+ await this.user.keyboard(`[${keyboardActivation}]`);
106
+ } else if (interactionType === 'mouse') await this.user.click(option);
107
+ else await this.user.pointer({
108
+ target: option,
109
+ keys: '[TouchA]'
110
+ });
111
+ (0, $gS6KO$act)(()=>{
112
+ jest.runAllTimers();
113
+ });
114
+ if (option.getAttribute('href') == null && option.getAttribute('aria-haspopup') == null && menuSelectionMode === 'single' && closesOnSelect && keyboardActivation !== 'Space' && !this._isSubmenu) {
82
115
  await (0, $gS6KO$waitFor)(()=>{
83
- if (!menuId || document.getElementById(menuId) == null) throw new Error(`Menu with id of ${menuId} not found in document.`);
116
+ if (document.activeElement !== trigger) throw new Error(`Expected the document.activeElement after selecting an option to be the menu trigger but got ${document.activeElement}`);
84
117
  else return true;
85
118
  });
119
+ if (document.contains(menu)) throw new Error('Expected menu element to not be in the document after selecting an option');
86
120
  }
87
- };
88
- // TODO: also very similar to select, barring potential long press support
89
- // Close on select is also kinda specific?
90
- this.selectOption = async (opts)=>{
91
- let { optionText: optionText, menuSelectionMode: menuSelectionMode = 'single', needsLongPress: needsLongPress, closesOnSelect: closesOnSelect = true, option: option, interactionType: interactionType = this._interactionType } = opts;
92
- let trigger = this.trigger;
93
- if (!trigger.getAttribute('aria-controls')) await this.open({
94
- needsLongPress: needsLongPress
95
- });
121
+ } else throw new Error("Attempted to select a option in the menu, but menu wasn't found.");
122
+ }
123
+ // TODO: update this to remove needsLongPress if we wanna make the user call open first always
124
+ /**
125
+ * Opens the submenu. Defaults to using the interaction type set on the menu tester. The submenu trigger can be targeted via the trigger's node or the trigger's text.
126
+ */ async openSubmenu(opts) {
127
+ let { submenuTrigger: submenuTrigger, needsLongPress: needsLongPress, interactionType: interactionType = this._interactionType } = opts;
128
+ let trigger = this.trigger;
129
+ let isDisabled = trigger.hasAttribute('disabled');
130
+ if (!trigger.getAttribute('aria-controls') && !isDisabled) await this.open({
131
+ needsLongPress: needsLongPress
132
+ });
133
+ if (!isDisabled) {
96
134
  let menu = this.menu;
97
135
  if (menu) {
98
- if (!option && optionText) option = (0, $gS6KO$within)(menu).getByText(optionText);
99
- if (interactionType === 'keyboard') {
100
- if (document.activeElement !== menu || !menu.contains(document.activeElement)) (0, $gS6KO$act)(()=>menu.focus());
101
- await this.user.keyboard(optionText);
102
- await this.user.keyboard('[Enter]');
103
- } else if (interactionType === 'mouse') await this.user.click(option);
104
- else await this.user.pointer({
105
- target: option,
106
- keys: '[TouchA]'
136
+ if (typeof submenuTrigger === 'string') submenuTrigger = (0, $gS6KO$within)(menu).getByText(submenuTrigger).closest('[role=menuitem]');
137
+ let submenuTriggerTester = new $74b93f0617179929$export$f73bbc9212ed861e({
138
+ user: this.user,
139
+ interactionType: this._interactionType,
140
+ root: submenuTrigger,
141
+ isSubmenu: true
107
142
  });
108
- if (option && option.getAttribute('href') == null && option.getAttribute('aria-haspopup') == null && menuSelectionMode === 'single' && closesOnSelect) {
109
- await (0, $gS6KO$waitFor)(()=>{
110
- if (document.activeElement !== trigger) throw new Error(`Expected the document.activeElement after selecting an option to be the menu trigger but got ${document.activeElement}`);
111
- else return true;
143
+ if (interactionType === 'mouse') {
144
+ await this.user.pointer({
145
+ target: submenuTrigger
112
146
  });
113
- if (document.contains(menu)) throw new Error('Expected menu element to not be in the document after selecting an option');
114
- }
115
- } else throw new Error("Attempted to select a option in the menu, but menu wasn't found.");
116
- };
117
- // TODO: update this to remove needsLongPress if we wanna make the user call open first always
118
- this.openSubmenu = async (opts)=>{
119
- let { submenuTrigger: submenuTrigger, submenuTriggerText: submenuTriggerText, needsLongPress: needsLongPress, interactionType: interactionType = this._interactionType } = opts;
120
- let trigger = this.trigger;
121
- let isDisabled = trigger.hasAttribute('disabled');
122
- if (!trigger.getAttribute('aria-controls') && !isDisabled) await this.open({
123
- needsLongPress: needsLongPress
124
- });
125
- if (!isDisabled) {
126
- let menu = this.menu;
127
- if (menu) {
128
- let submenu;
129
- if (submenuTrigger) submenu = submenuTrigger;
130
- else if (submenuTriggerText) submenu = (0, $gS6KO$within)(menu).getByText(submenuTriggerText);
131
- let submenuTriggerTester = new $74b93f0617179929$export$f73bbc9212ed861e({
132
- user: this.user,
133
- interactionType: interactionType,
134
- root: submenu
147
+ (0, $gS6KO$act)(()=>{
148
+ jest.runAllTimers();
149
+ });
150
+ } else if (interactionType === 'keyboard') {
151
+ await this.keyboardNavigateToOption({
152
+ option: submenuTrigger
153
+ });
154
+ await this.user.keyboard('[ArrowRight]');
155
+ (0, $gS6KO$act)(()=>{
156
+ jest.runAllTimers();
135
157
  });
136
- await submenuTriggerTester.open();
137
- return submenuTriggerTester;
138
- }
158
+ } else await submenuTriggerTester.open();
159
+ return submenuTriggerTester;
139
160
  }
140
- return null;
141
- };
142
- this.close = async ()=>{
143
- let menu = this.menu;
144
- if (menu) {
145
- (0, $gS6KO$act)(()=>menu.focus());
146
- await this.user.keyboard('[Escape]');
147
- await (0, $gS6KO$waitFor)(()=>{
148
- if (document.activeElement !== this.trigger) throw new Error(`Expected the document.activeElement after closing the menu to be the menu trigger but got ${document.activeElement}`);
149
- else return true;
150
- });
151
- if (document.contains(menu)) throw new Error('Expected the menu to not be in the document after closing it.');
161
+ }
162
+ return null;
163
+ }
164
+ async keyboardNavigateToOption(opts) {
165
+ let { option: option } = opts;
166
+ let options = this.options();
167
+ let targetIndex = options.findIndex((opt)=>opt === option || opt.contains(option));
168
+ if (targetIndex === -1) throw new Error('Option provided is not in the menu');
169
+ if (document.activeElement === this.menu) await this.user.keyboard('[ArrowDown]');
170
+ let currIndex = options.indexOf(document.activeElement);
171
+ if (currIndex === -1) throw new Error('ActiveElement is not in the menu');
172
+ let direction = targetIndex > currIndex ? 'down' : 'up';
173
+ for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
174
+ }
175
+ /**
176
+ * Closes the menu.
177
+ */ async close() {
178
+ let menu = this.menu;
179
+ if (menu) {
180
+ (0, $gS6KO$act)(()=>menu.focus());
181
+ await this.user.keyboard('[Escape]');
182
+ await (0, $gS6KO$waitFor)(()=>{
183
+ if (document.activeElement !== this.trigger) throw new Error(`Expected the document.activeElement after closing the menu to be the menu trigger but got ${document.activeElement}`);
184
+ else return true;
185
+ });
186
+ if (document.contains(menu)) throw new Error('Expected the menu to not be in the document after closing it.');
187
+ }
188
+ }
189
+ /**
190
+ * Returns the menu's trigger.
191
+ */ get trigger() {
192
+ if (!this._trigger) throw new Error('No trigger element found for menu.');
193
+ return this._trigger;
194
+ }
195
+ /**
196
+ * Returns the menu if present.
197
+ */ get menu() {
198
+ let menuId = this.trigger.getAttribute('aria-controls');
199
+ return menuId ? document.getElementById(menuId) : null;
200
+ }
201
+ /**
202
+ * Returns the menu's sections if any.
203
+ */ get sections() {
204
+ let menu = this.menu;
205
+ if (menu) return (0, $gS6KO$within)(menu).queryAllByRole('group');
206
+ else return [];
207
+ }
208
+ /**
209
+ * Returns the menu's options if present. Can be filtered to a subsection of the menu if provided via `element`.
210
+ */ options(opts = {}) {
211
+ let { element: element = this.menu } = opts;
212
+ let options = [];
213
+ if (element) {
214
+ options = (0, $gS6KO$within)(element).queryAllByRole('menuitem');
215
+ if (options.length === 0) {
216
+ options = (0, $gS6KO$within)(element).queryAllByRole('menuitemradio');
217
+ if (options.length === 0) options = (0, $gS6KO$within)(element).queryAllByRole('menuitemcheckbox');
152
218
  }
153
- };
154
- let { root: root, user: user, interactionType: interactionType, advanceTimer: advanceTimer } = opts;
219
+ }
220
+ return options;
221
+ }
222
+ /**
223
+ * Returns the menu's submenu triggers if any.
224
+ */ get submenuTriggers() {
225
+ let options = this.options();
226
+ if (options.length > 0) return options.filter((item)=>item.getAttribute('aria-haspopup') != null);
227
+ return [];
228
+ }
229
+ constructor(opts){
230
+ this._isSubmenu = false;
231
+ let { root: root, user: user, interactionType: interactionType, advanceTimer: advanceTimer, isSubmenu: isSubmenu } = opts;
155
232
  this.user = user;
156
233
  this._interactionType = interactionType || 'mouse';
157
234
  this._advanceTimer = advanceTimer;
@@ -163,6 +240,7 @@ class $74b93f0617179929$export$f73bbc9212ed861e {
163
240
  if (trigger) this._trigger = trigger;
164
241
  else this._trigger = root;
165
242
  }
243
+ this._isSubmenu = isSubmenu || false;
166
244
  }
167
245
  }
168
246