@react-aria/test-utils 1.0.0-nightly.5041 → 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.
Files changed (124) hide show
  1. package/README.md +70 -0
  2. package/dist/import.mjs +6 -4
  3. package/dist/main.js +15 -23
  4. package/dist/main.js.map +1 -1
  5. package/dist/module.js +6 -4
  6. package/dist/module.js.map +1 -1
  7. package/dist/private/act.cjs +33 -0
  8. package/dist/private/act.cjs.map +1 -0
  9. package/dist/private/act.js +28 -0
  10. package/dist/private/act.js.map +1 -0
  11. package/dist/private/checkboxgroup.cjs +107 -0
  12. package/dist/private/checkboxgroup.cjs.map +1 -0
  13. package/dist/private/checkboxgroup.js +102 -0
  14. package/dist/private/checkboxgroup.js.map +1 -0
  15. package/dist/private/combobox.cjs +199 -0
  16. package/dist/private/combobox.cjs.map +1 -0
  17. package/dist/private/combobox.js +194 -0
  18. package/dist/private/combobox.js.map +1 -0
  19. package/dist/private/dialog.cjs +110 -0
  20. package/dist/private/dialog.cjs.map +1 -0
  21. package/dist/private/dialog.js +105 -0
  22. package/dist/private/dialog.js.map +1 -0
  23. package/dist/private/gridlist.cjs +173 -0
  24. package/dist/private/gridlist.cjs.map +1 -0
  25. package/dist/private/gridlist.js +168 -0
  26. package/dist/private/gridlist.js.map +1 -0
  27. package/dist/private/listbox.cjs +163 -0
  28. package/dist/private/listbox.cjs.map +1 -0
  29. package/dist/private/listbox.js +158 -0
  30. package/dist/private/listbox.js.map +1 -0
  31. package/dist/private/menu.cjs +265 -0
  32. package/dist/private/menu.cjs.map +1 -0
  33. package/dist/private/menu.js +260 -0
  34. package/dist/private/menu.js.map +1 -0
  35. package/dist/private/radiogroup.cjs +122 -0
  36. package/dist/private/radiogroup.cjs.map +1 -0
  37. package/dist/private/radiogroup.js +117 -0
  38. package/dist/private/radiogroup.js.map +1 -0
  39. package/dist/private/select.cjs +169 -0
  40. package/dist/private/select.cjs.map +1 -0
  41. package/dist/private/select.js +164 -0
  42. package/dist/private/select.js.map +1 -0
  43. package/dist/private/table.cjs +346 -0
  44. package/dist/private/table.cjs.map +1 -0
  45. package/dist/private/table.js +341 -0
  46. package/dist/private/table.js.map +1 -0
  47. package/dist/private/tabs.cjs +131 -0
  48. package/dist/private/tabs.cjs.map +1 -0
  49. package/dist/private/tabs.js +126 -0
  50. package/dist/private/tabs.js.map +1 -0
  51. package/dist/private/testSetup.cjs +87 -0
  52. package/dist/private/testSetup.cjs.map +1 -0
  53. package/dist/private/testSetup.js +81 -0
  54. package/dist/private/testSetup.js.map +1 -0
  55. package/dist/private/tree.cjs +181 -0
  56. package/dist/private/tree.cjs.map +1 -0
  57. package/dist/private/tree.js +176 -0
  58. package/dist/private/tree.js.map +1 -0
  59. package/dist/private/user.cjs +85 -0
  60. package/dist/private/user.cjs.map +1 -0
  61. package/dist/private/user.js +76 -0
  62. package/dist/private/user.js.map +1 -0
  63. package/dist/{userEventMaps.main.js → private/userEventMaps.cjs} +3 -3
  64. package/dist/private/userEventMaps.cjs.map +1 -0
  65. package/dist/{userEventMaps.mjs → private/userEventMaps.js} +3 -3
  66. package/dist/private/userEventMaps.js.map +1 -0
  67. package/dist/private/utils.cjs +136 -0
  68. package/dist/private/utils.cjs.map +1 -0
  69. package/dist/private/utils.js +127 -0
  70. package/dist/private/utils.js.map +1 -0
  71. package/dist/types/src/act.d.ts +4 -0
  72. package/dist/types/src/checkboxgroup.d.ts +47 -0
  73. package/dist/types/src/combobox.d.ts +87 -0
  74. package/dist/types/src/dialog.d.ts +37 -0
  75. package/dist/types/src/events.d.ts +25 -0
  76. package/dist/types/src/gridlist.d.ts +56 -0
  77. package/dist/types/src/index.d.ts +16 -0
  78. package/dist/types/src/listbox.d.ts +91 -0
  79. package/dist/types/src/menu.d.ts +112 -0
  80. package/dist/types/src/radiogroup.d.ts +47 -0
  81. package/dist/types/src/select.d.ts +74 -0
  82. package/dist/types/src/table.d.ts +120 -0
  83. package/dist/types/src/tabs.d.ts +59 -0
  84. package/dist/types/src/testSetup.d.ts +6 -0
  85. package/dist/types/src/tree.d.ts +62 -0
  86. package/dist/types/src/types.d.ts +143 -0
  87. package/dist/types/src/user.d.ts +49 -0
  88. package/dist/types/src/userEventMaps.d.ts +2 -0
  89. package/dist/types/src/utils.d.ts +29 -0
  90. package/package.json +26 -18
  91. package/src/act.ts +35 -0
  92. package/src/checkboxgroup.ts +165 -0
  93. package/src/combobox.ts +307 -0
  94. package/src/dialog.ts +155 -0
  95. package/src/gridlist.ts +278 -0
  96. package/src/index.ts +17 -3
  97. package/src/listbox.ts +300 -0
  98. package/src/menu.ts +479 -0
  99. package/src/radiogroup.ts +179 -0
  100. package/src/select.ts +273 -0
  101. package/src/table.ts +589 -0
  102. package/src/tabs.ts +204 -0
  103. package/src/testSetup.ts +41 -36
  104. package/src/tree.ts +290 -0
  105. package/src/types.ts +171 -0
  106. package/src/user.ts +153 -0
  107. package/src/userEventMaps.ts +1 -1
  108. package/src/utils.ts +155 -0
  109. package/dist/events.main.js +0 -37
  110. package/dist/events.main.js.map +0 -1
  111. package/dist/events.mjs +0 -31
  112. package/dist/events.module.js +0 -31
  113. package/dist/events.module.js.map +0 -1
  114. package/dist/testSetup.main.js +0 -82
  115. package/dist/testSetup.main.js.map +0 -1
  116. package/dist/testSetup.mjs +0 -76
  117. package/dist/testSetup.module.js +0 -76
  118. package/dist/testSetup.module.js.map +0 -1
  119. package/dist/types.d.ts +0 -16
  120. package/dist/types.d.ts.map +0 -1
  121. package/dist/userEventMaps.main.js.map +0 -1
  122. package/dist/userEventMaps.module.js +0 -38
  123. package/dist/userEventMaps.module.js.map +0 -1
  124. package/src/events.ts +0 -28
package/src/select.ts ADDED
@@ -0,0 +1,273 @@
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 {formatTargetNode} from './utils';
15
+ import {SelectTesterOpts, UserOpts} from './types';
16
+ import {waitFor, within} from '@testing-library/dom';
17
+
18
+ interface SelectOpenOpts {
19
+ /**
20
+ * What interaction type to use when opening the select. Defaults to the interaction type set on
21
+ * the tester.
22
+ */
23
+ interactionType?: UserOpts['interactionType'];
24
+ }
25
+
26
+ interface SelectTriggerOptionOpts extends SelectOpenOpts {
27
+ /**
28
+ * The index, text, or node of the option to select. Option nodes can be sourced via
29
+ * `getOptions()`.
30
+ */
31
+ option: number | string | HTMLElement;
32
+ /**
33
+ * Whether or not the select closes on selection. Depends on select implementation and
34
+ * configuration.
35
+ *
36
+ * @default true
37
+ */
38
+ closesOnSelect?: boolean;
39
+ }
40
+
41
+ export class SelectTester {
42
+ private user;
43
+ private _interactionType: UserOpts['interactionType'];
44
+ private _trigger: HTMLElement;
45
+
46
+ constructor(opts: SelectTesterOpts) {
47
+ let {root, user, interactionType} = opts;
48
+ this.user = user;
49
+ this._interactionType = interactionType || 'mouse';
50
+ // Handle case where the wrapper element is provided rather than the Select's button (aka RAC)
51
+ let buttons = within(root).queryAllByRole('button');
52
+ let triggerButton;
53
+ if (buttons.length === 0) {
54
+ triggerButton = root;
55
+ } else if (buttons.length === 1) {
56
+ triggerButton = buttons[0];
57
+ } else {
58
+ triggerButton = buttons.find(button => button.hasAttribute('aria-haspopup'));
59
+ }
60
+
61
+ this._trigger = triggerButton ?? root;
62
+ }
63
+ /**
64
+ * Set the interaction type used by the select tester.
65
+ */
66
+ setInteractionType(type: UserOpts['interactionType']): void {
67
+ this._interactionType = type;
68
+ }
69
+
70
+ /**
71
+ * Opens the select. Defaults to using the interaction type set on the select tester.
72
+ */
73
+ async open(opts: SelectOpenOpts = {}): Promise<void> {
74
+ let {interactionType = this._interactionType} = opts;
75
+ let trigger = this.getTrigger();
76
+ let isDisabled = trigger.hasAttribute('disabled');
77
+
78
+ if (interactionType === 'mouse') {
79
+ await this.user.click(this._trigger);
80
+ } else if (interactionType === 'keyboard') {
81
+ act(() => trigger.focus());
82
+ await this.user.keyboard('[Enter]');
83
+ } else if (interactionType === 'touch') {
84
+ await this.user.pointer({target: this._trigger, keys: '[TouchA]'});
85
+ }
86
+
87
+ await waitFor(() => {
88
+ if (!isDisabled && trigger.getAttribute('aria-controls') == null) {
89
+ throw new Error('No aria-controls found on select element trigger.');
90
+ } else {
91
+ return true;
92
+ }
93
+ });
94
+ let listBoxId = trigger.getAttribute('aria-controls');
95
+ await waitFor(() => {
96
+ if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {
97
+ throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
98
+ } else {
99
+ return true;
100
+ }
101
+ });
102
+ }
103
+
104
+ /**
105
+ * Closes the select.
106
+ */
107
+ async close(): Promise<void> {
108
+ let listbox = this.getListbox();
109
+ if (listbox) {
110
+ act(() => listbox.focus());
111
+ await this.user.keyboard('[Escape]');
112
+ }
113
+
114
+ await waitFor(() => {
115
+ if (document.activeElement !== this._trigger) {
116
+ throw new Error(
117
+ `Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`
118
+ );
119
+ } else {
120
+ return true;
121
+ }
122
+ });
123
+
124
+ if (listbox && document.contains(listbox)) {
125
+ throw new Error(
126
+ 'Expected the select element listbox to not be in the document after closing the dropdown.'
127
+ );
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Returns a option matching the specified index or text content.
133
+ */
134
+ findOption(opts: {indexOrText: number | string}): HTMLElement {
135
+ let {indexOrText} = opts;
136
+
137
+ let option;
138
+ let options = this.getOptions();
139
+ let listbox = this.getListbox();
140
+
141
+ if (typeof indexOrText === 'number') {
142
+ option = options[indexOrText];
143
+ } else if (typeof indexOrText === 'string' && listbox != null) {
144
+ option = within(listbox!).getByText(indexOrText).closest('[role=option]')! as HTMLElement;
145
+ }
146
+
147
+ return option;
148
+ }
149
+
150
+ private async keyboardNavigateToOption(opts: {option: HTMLElement}) {
151
+ let {option} = opts;
152
+ let options = this.getOptions();
153
+ let targetIndex = options.indexOf(option);
154
+ if (targetIndex === -1) {
155
+ throw new Error('Option provided is not in the listbox');
156
+ }
157
+ if (document.activeElement === this.getListbox()) {
158
+ await this.user.keyboard('[ArrowDown]');
159
+ }
160
+ let currIndex = options.indexOf(document.activeElement as HTMLElement);
161
+ if (currIndex === -1) {
162
+ throw new Error('ActiveElement is not in the listbox');
163
+ }
164
+ let direction = targetIndex > currIndex ? 'down' : 'up';
165
+
166
+ for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
167
+ await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Toggles the selection of the desired select option if possible. Defaults to using the
173
+ * interaction type set on the select tester. If necessary, will open the select dropdown
174
+ * beforehand. The desired option can be targeted via the option's node, the option's text, or the
175
+ * option's index.
176
+ */
177
+ async toggleOptionSelection(opts: SelectTriggerOptionOpts): Promise<void> {
178
+ let {option, closesOnSelect, interactionType = this._interactionType} = opts || {};
179
+ let trigger = this.getTrigger();
180
+ if (!trigger.getAttribute('aria-controls')) {
181
+ await this.open();
182
+ }
183
+ let listbox = this.getListbox();
184
+ if (!listbox) {
185
+ throw new Error("Select's listbox not found.");
186
+ }
187
+
188
+ if (typeof option === 'string' || typeof option === 'number') {
189
+ option = this.findOption({indexOrText: option});
190
+ }
191
+
192
+ if (!option) {
193
+ throw new Error(`Target option "${formatTargetNode(opts.option)}" not found in the listbox.`);
194
+ }
195
+
196
+ let isMultiSelect = listbox.getAttribute('aria-multiselectable') === 'true';
197
+ let isSingleSelect = !isMultiSelect;
198
+ closesOnSelect = closesOnSelect ?? isSingleSelect;
199
+
200
+ if (interactionType === 'keyboard') {
201
+ if (option?.getAttribute('aria-disabled') === 'true') {
202
+ throw new Error(`Cannot select disabled option "${formatTargetNode(opts.option)}".`);
203
+ }
204
+
205
+ if (document.activeElement !== listbox && !listbox.contains(document.activeElement)) {
206
+ act(() => listbox.focus());
207
+ }
208
+ await this.keyboardNavigateToOption({option});
209
+ await this.user.keyboard('[Enter]');
210
+ } else {
211
+ if (interactionType === 'mouse') {
212
+ await this.user.click(option);
213
+ } else {
214
+ await this.user.pointer({target: option, keys: '[TouchA]'});
215
+ }
216
+ }
217
+
218
+ if (closesOnSelect && option?.getAttribute('href') == null) {
219
+ await waitFor(() => {
220
+ if (document.activeElement !== this._trigger) {
221
+ throw new Error(
222
+ `Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`
223
+ );
224
+ } else {
225
+ return true;
226
+ }
227
+ });
228
+
229
+ if (document.contains(listbox)) {
230
+ throw new Error(
231
+ 'Expected select element listbox to not be in the document after selecting an option'
232
+ );
233
+ }
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Returns the select's options if present. Can be filtered to a subsection of the listbox if
239
+ * provided via `element`.
240
+ */
241
+ getOptions(opts: {element?: HTMLElement} = {}): HTMLElement[] {
242
+ let {element = this.getListbox()} = opts;
243
+ let options = [];
244
+ if (element) {
245
+ options = within(element).queryAllByRole('option');
246
+ }
247
+
248
+ return options;
249
+ }
250
+
251
+ /**
252
+ * Returns the select's trigger.
253
+ */
254
+ getTrigger(): HTMLElement {
255
+ return this._trigger;
256
+ }
257
+
258
+ /**
259
+ * Returns the select's listbox if present.
260
+ */
261
+ getListbox(): HTMLElement | null {
262
+ let listBoxId = this.getTrigger().getAttribute('aria-controls');
263
+ return listBoxId ? document.getElementById(listBoxId) : null;
264
+ }
265
+
266
+ /**
267
+ * Returns the select's sections if present.
268
+ */
269
+ getSections(): HTMLElement[] {
270
+ let listbox = this.getListbox();
271
+ return listbox ? within(listbox).queryAllByRole('group') : [];
272
+ }
273
+ }