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