@react-aria/test-utils 1.0.0-beta.1 → 1.0.0-beta.3

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 (133) hide show
  1. package/dist/import.mjs +5 -5
  2. package/dist/main.js +9 -9
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +5 -5
  5. package/dist/module.js.map +1 -1
  6. package/dist/private/checkboxgroup.cjs +104 -0
  7. package/dist/private/checkboxgroup.cjs.map +1 -0
  8. package/dist/private/checkboxgroup.js +99 -0
  9. package/dist/private/checkboxgroup.js.map +1 -0
  10. package/dist/{combobox.main.js → private/combobox.cjs} +35 -35
  11. package/dist/private/combobox.cjs.map +1 -0
  12. package/dist/{combobox.mjs → private/combobox.js} +35 -35
  13. package/dist/private/combobox.js.map +1 -0
  14. package/dist/private/dialog.cjs +105 -0
  15. package/dist/private/dialog.cjs.map +1 -0
  16. package/dist/private/dialog.js +100 -0
  17. package/dist/private/dialog.js.map +1 -0
  18. package/dist/{events.main.js → private/events.cjs} +32 -34
  19. package/dist/private/events.cjs.map +1 -0
  20. package/dist/{events.mjs → private/events.js} +29 -31
  21. package/dist/private/events.js.map +1 -0
  22. package/dist/{gridlist.main.js → private/gridlist.cjs} +27 -28
  23. package/dist/private/gridlist.cjs.map +1 -0
  24. package/dist/{gridlist.mjs → private/gridlist.js} +27 -28
  25. package/dist/private/gridlist.js.map +1 -0
  26. package/dist/{listbox.main.js → private/listbox.cjs} +24 -24
  27. package/dist/private/listbox.cjs.map +1 -0
  28. package/dist/{listbox.mjs → private/listbox.js} +24 -24
  29. package/dist/private/listbox.js.map +1 -0
  30. package/dist/{menu.main.js → private/menu.cjs} +46 -48
  31. package/dist/private/menu.cjs.map +1 -0
  32. package/dist/{menu.mjs → private/menu.js} +46 -48
  33. package/dist/private/menu.js.map +1 -0
  34. package/dist/private/radiogroup.cjs +120 -0
  35. package/dist/private/radiogroup.cjs.map +1 -0
  36. package/dist/private/radiogroup.js +115 -0
  37. package/dist/private/radiogroup.js.map +1 -0
  38. package/dist/{select.main.js → private/select.cjs} +31 -29
  39. package/dist/private/select.cjs.map +1 -0
  40. package/dist/{select.mjs → private/select.js} +31 -29
  41. package/dist/private/select.js.map +1 -0
  42. package/dist/{table.main.js → private/table.cjs} +82 -57
  43. package/dist/private/table.cjs.map +1 -0
  44. package/dist/{table.module.js → private/table.js} +82 -57
  45. package/dist/private/table.js.map +1 -0
  46. package/dist/{tabs.main.js → private/tabs.cjs} +24 -26
  47. package/dist/private/tabs.cjs.map +1 -0
  48. package/dist/{tabs.mjs → private/tabs.js} +24 -26
  49. package/dist/private/tabs.js.map +1 -0
  50. package/dist/private/testSetup.cjs +87 -0
  51. package/dist/private/testSetup.cjs.map +1 -0
  52. package/dist/private/testSetup.js +81 -0
  53. package/dist/private/testSetup.js.map +1 -0
  54. package/dist/{tree.main.js → private/tree.cjs} +34 -35
  55. package/dist/private/tree.cjs.map +1 -0
  56. package/dist/{tree.mjs → private/tree.js} +34 -35
  57. package/dist/private/tree.js.map +1 -0
  58. package/dist/private/user.cjs +85 -0
  59. package/dist/private/user.cjs.map +1 -0
  60. package/dist/private/user.js +76 -0
  61. package/dist/private/user.js.map +1 -0
  62. package/dist/{userEventMaps.main.js → private/userEventMaps.cjs} +3 -3
  63. package/dist/{userEventMaps.main.js.map → private/userEventMaps.cjs.map} +1 -1
  64. package/dist/{userEventMaps.mjs → private/userEventMaps.js} +3 -3
  65. package/dist/{userEventMaps.module.js.map → private/userEventMaps.js.map} +1 -1
  66. package/dist/types/src/checkboxgroup.d.ts +45 -0
  67. package/dist/types/src/combobox.d.ts +75 -0
  68. package/dist/types/src/dialog.d.ts +36 -0
  69. package/dist/types/src/events.d.ts +25 -0
  70. package/dist/types/src/gridlist.d.ts +51 -0
  71. package/dist/types/src/index.d.ts +16 -0
  72. package/dist/types/src/listbox.d.ts +80 -0
  73. package/dist/types/src/menu.d.ts +102 -0
  74. package/dist/types/src/radiogroup.d.ts +46 -0
  75. package/dist/types/src/select.d.ts +67 -0
  76. package/dist/types/src/table.d.ts +104 -0
  77. package/dist/types/src/tabs.d.ts +58 -0
  78. package/dist/types/src/testSetup.d.ts +6 -0
  79. package/dist/types/src/tree.d.ts +57 -0
  80. package/dist/types/src/types.d.ts +136 -0
  81. package/dist/types/src/user.d.ts +47 -0
  82. package/dist/types/src/userEventMaps.d.ts +2 -0
  83. package/package.json +7 -7
  84. package/src/checkboxgroup.ts +158 -0
  85. package/src/dialog.ts +143 -0
  86. package/src/index.ts +11 -0
  87. package/src/radiogroup.ts +176 -0
  88. package/src/select.ts +10 -2
  89. package/src/table.ts +46 -1
  90. package/src/tabs.ts +7 -2
  91. package/src/testSetup.ts +30 -28
  92. package/src/types.ts +21 -0
  93. package/src/user.ts +25 -7
  94. package/dist/combobox.main.js.map +0 -1
  95. package/dist/combobox.module.js +0 -167
  96. package/dist/combobox.module.js.map +0 -1
  97. package/dist/events.main.js.map +0 -1
  98. package/dist/events.module.js +0 -112
  99. package/dist/events.module.js.map +0 -1
  100. package/dist/gridlist.main.js.map +0 -1
  101. package/dist/gridlist.module.js +0 -149
  102. package/dist/gridlist.module.js.map +0 -1
  103. package/dist/listbox.main.js.map +0 -1
  104. package/dist/listbox.module.js +0 -138
  105. package/dist/listbox.module.js.map +0 -1
  106. package/dist/menu.main.js.map +0 -1
  107. package/dist/menu.module.js +0 -269
  108. package/dist/menu.module.js.map +0 -1
  109. package/dist/select.main.js.map +0 -1
  110. package/dist/select.module.js +0 -158
  111. package/dist/select.module.js.map +0 -1
  112. package/dist/table.main.js.map +0 -1
  113. package/dist/table.mjs +0 -290
  114. package/dist/table.module.js.map +0 -1
  115. package/dist/tabs.main.js.map +0 -1
  116. package/dist/tabs.module.js +0 -128
  117. package/dist/tabs.module.js.map +0 -1
  118. package/dist/testSetup.main.js +0 -78
  119. package/dist/testSetup.main.js.map +0 -1
  120. package/dist/testSetup.mjs +0 -72
  121. package/dist/testSetup.module.js +0 -72
  122. package/dist/testSetup.module.js.map +0 -1
  123. package/dist/tree.main.js.map +0 -1
  124. package/dist/tree.module.js +0 -179
  125. package/dist/tree.module.js.map +0 -1
  126. package/dist/types.d.ts +0 -694
  127. package/dist/types.d.ts.map +0 -1
  128. package/dist/user.main.js +0 -76
  129. package/dist/user.main.js.map +0 -1
  130. package/dist/user.mjs +0 -67
  131. package/dist/user.module.js +0 -67
  132. package/dist/user.module.js.map +0 -1
  133. package/dist/userEventMaps.module.js +0 -38
@@ -0,0 +1,176 @@
1
+ /*
2
+ * Copyright 2025 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, RadioGroupTesterOpts, UserOpts} from './types';
15
+ import {pressElement} from './events';
16
+
17
+ interface TriggerRadioOptions {
18
+ /**
19
+ * What interaction type to use when triggering a radio. Defaults to the interaction type set on the tester.
20
+ */
21
+ interactionType?: UserOpts['interactionType'],
22
+ /**
23
+ * The index, text, or node of the radio to toggle selection for.
24
+ */
25
+ radio: number | string | HTMLElement
26
+ }
27
+
28
+ export class RadioGroupTester {
29
+ private user;
30
+ private _interactionType: UserOpts['interactionType'];
31
+ private _radiogroup: HTMLElement;
32
+ private _direction: Direction;
33
+
34
+ constructor(opts: RadioGroupTesterOpts) {
35
+ let {root, user, interactionType, direction} = opts;
36
+ this.user = user;
37
+ this._interactionType = interactionType || 'mouse';
38
+ this._direction = direction || 'ltr';
39
+
40
+ this._radiogroup = root;
41
+ let radiogroup = within(root).queryAllByRole('radiogroup');
42
+ if (radiogroup.length > 0) {
43
+ this._radiogroup = radiogroup[0];
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Set the interaction type used by the radio tester.
49
+ */
50
+ setInteractionType(type: UserOpts['interactionType']): void {
51
+ this._interactionType = type;
52
+ }
53
+
54
+ /**
55
+ * Returns a radio matching the specified index or text content.
56
+ */
57
+ findRadio(opts: {radioIndexOrText: number | string}): HTMLElement {
58
+ let {
59
+ radioIndexOrText
60
+ } = opts;
61
+
62
+ let radio;
63
+ if (typeof radioIndexOrText === 'number') {
64
+ radio = this.radios[radioIndexOrText];
65
+ } else if (typeof radioIndexOrText === 'string') {
66
+ let label = within(this.radiogroup).getByText(radioIndexOrText);
67
+ // Label may wrap the radio, or the actual label may be a sibling span, or the radio div could have the label within it
68
+ if (label) {
69
+ radio = within(label).queryByRole('radio');
70
+ if (!radio) {
71
+ let labelWrapper = label.closest('label');
72
+ if (labelWrapper) {
73
+ radio = within(labelWrapper).queryByRole('radio');
74
+ } else {
75
+ radio = label.closest('[role=radio]');
76
+ }
77
+ }
78
+ }
79
+ }
80
+
81
+ return radio;
82
+ }
83
+
84
+ private async keyboardNavigateToRadio(opts: {radio: HTMLElement, orientation?: Orientation}) {
85
+ let {radio, orientation = 'vertical'} = opts;
86
+ let radios = this.radios;
87
+ radios = radios.filter(radio => !(radio.hasAttribute('disabled') || radio.getAttribute('aria-disabled') === 'true'));
88
+ if (radios.length === 0) {
89
+ throw new Error('Radio group doesnt have any non-disabled radios. Please double check your radio group.');
90
+ }
91
+
92
+ let targetIndex = radios.indexOf(radio);
93
+ if (targetIndex === -1) {
94
+ throw new Error('Radio provided is not in the radio group.');
95
+ }
96
+
97
+ if (!this.radiogroup.contains(document.activeElement)) {
98
+ let selectedRadio = this.selectedRadio;
99
+ if (selectedRadio != null) {
100
+ act(() => selectedRadio.focus());
101
+ } else {
102
+ act(() => radios[0]?.focus());
103
+ }
104
+ }
105
+
106
+ let currIndex = radios.indexOf(document.activeElement as HTMLElement);
107
+ if (currIndex === -1) {
108
+ throw new Error('Active element is not in the radio group.');
109
+ }
110
+
111
+ let arrowUp = 'ArrowUp';
112
+ let arrowDown = 'ArrowDown';
113
+ if (orientation === 'horizontal') {
114
+ if (this._direction === 'ltr') {
115
+ arrowUp = 'ArrowLeft';
116
+ arrowDown = 'ArrowRight';
117
+ } else {
118
+ arrowUp = 'ArrowRight';
119
+ arrowDown = 'ArrowLeft';
120
+ }
121
+ }
122
+
123
+ let movementDirection = targetIndex > currIndex ? 'down' : 'up';
124
+ for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
125
+ await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
126
+ }
127
+ };
128
+
129
+ /**
130
+ * Triggers the specified radio. Defaults to using the interaction type set on the radio tester.
131
+ */
132
+ async triggerRadio(opts: TriggerRadioOptions): Promise<void> {
133
+ let {
134
+ radio,
135
+ interactionType = this._interactionType
136
+ } = opts;
137
+
138
+ if (typeof radio === 'string' || typeof radio === 'number') {
139
+ radio = this.findRadio({radioIndexOrText: radio});
140
+ }
141
+
142
+ if (!radio) {
143
+ throw new Error('Target radio not found in the radio group.');
144
+ } else if (radio.hasAttribute('disabled')) {
145
+ throw new Error('Target radio is disabled.');
146
+ }
147
+
148
+ if (interactionType === 'keyboard') {
149
+ let radioOrientation = this._radiogroup.getAttribute('aria-orientation') || 'horizontal';
150
+ await this.keyboardNavigateToRadio({radio, orientation: radioOrientation as Orientation});
151
+ } else {
152
+ await pressElement(this.user, radio, interactionType);
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Returns the radiogroup.
158
+ */
159
+ get radiogroup(): HTMLElement {
160
+ return this._radiogroup;
161
+ }
162
+
163
+ /**
164
+ * Returns the radios.
165
+ */
166
+ get radios(): HTMLElement[] {
167
+ return within(this.radiogroup).queryAllByRole('radio');
168
+ }
169
+
170
+ /**
171
+ * Returns the currently selected radio in the radiogroup if any.
172
+ */
173
+ get selectedRadio(): HTMLElement | null {
174
+ return this.radios.find(radio => (radio as HTMLInputElement).checked) || null;
175
+ }
176
+ }
package/src/select.ts CHANGED
@@ -24,7 +24,12 @@ interface SelectTriggerOptionOpts extends SelectOpenOpts {
24
24
  /**
25
25
  * The index, text, or node of the option to select. Option nodes can be sourced via `options()`.
26
26
  */
27
- option: number | string | HTMLElement
27
+ option: number | string | HTMLElement,
28
+ /**
29
+ * Whether or not the select closes on selection. Depends on select implementation and configuration.
30
+ * @default true
31
+ */
32
+ closesOnSelect?: boolean
28
33
  }
29
34
 
30
35
  export class SelectTester {
@@ -164,6 +169,7 @@ export class SelectTester {
164
169
  async selectOption(opts: SelectTriggerOptionOpts): Promise<void> {
165
170
  let {
166
171
  option,
172
+ closesOnSelect,
167
173
  interactionType = this._interactionType
168
174
  } = opts || {};
169
175
  let trigger = this.trigger;
@@ -185,6 +191,8 @@ export class SelectTester {
185
191
  }
186
192
 
187
193
  let isMultiSelect = listbox.getAttribute('aria-multiselectable') === 'true';
194
+ let isSingleSelect = !isMultiSelect;
195
+ closesOnSelect = closesOnSelect ?? isSingleSelect;
188
196
 
189
197
  if (interactionType === 'keyboard') {
190
198
  if (option?.getAttribute('aria-disabled') === 'true') {
@@ -205,7 +213,7 @@ export class SelectTester {
205
213
  }
206
214
  }
207
215
 
208
- if (!isMultiSelect && option?.getAttribute('href') == null) {
216
+ if (closesOnSelect && option?.getAttribute('href') == null) {
209
217
  await waitFor(() => {
210
218
  if (document.activeElement !== this._trigger) {
211
219
  throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);
package/src/table.ts CHANGED
@@ -11,10 +11,11 @@
11
11
  */
12
12
 
13
13
  import {act, waitFor, within} from '@testing-library/react';
14
+ import {BaseGridRowInteractionOpts, GridRowActionOpts, TableTesterOpts, ToggleGridRowOpts, UserOpts} from './types';
14
15
  import {getAltKey, getMetaKey, pressElement, triggerLongPress} from './events';
15
- import {GridRowActionOpts, TableTesterOpts, ToggleGridRowOpts, UserOpts} from './types';
16
16
 
17
17
  interface TableToggleRowOpts extends ToggleGridRowOpts {}
18
+ interface TableToggleExpansionOpts extends BaseGridRowInteractionOpts {}
18
19
  interface TableToggleSortOpts {
19
20
  /**
20
21
  * The index, text, or node of the column to toggle selection for.
@@ -162,6 +163,50 @@ export class TableTester {
162
163
  }
163
164
  };
164
165
 
166
+ /**
167
+ * Toggles the expansion for the specified tree row. Defaults to using the interaction type set on the tree tester.
168
+ */
169
+ async toggleRowExpansion(opts: TableToggleExpansionOpts): Promise<void> {
170
+ let {
171
+ row,
172
+ interactionType = this._interactionType
173
+ } = opts;
174
+ if (!this.table.contains(document.activeElement)) {
175
+ await act(async () => {
176
+ this.table.focus();
177
+ });
178
+ }
179
+
180
+ if (typeof row === 'string' || typeof row === 'number') {
181
+ row = this.findRow({rowIndexOrText: row});
182
+ }
183
+
184
+ if (!row) {
185
+ throw new Error('Target row not found in the table.');
186
+ } else if (row.getAttribute('aria-expanded') == null) {
187
+ throw new Error('Target row is not expandable.');
188
+ }
189
+
190
+ if (interactionType === 'mouse' || interactionType === 'touch') {
191
+ let rowExpander = within(row).getAllByRole('button')[0]; // what happens if the button is not first? how can we differentiate?
192
+ await pressElement(this.user, rowExpander, interactionType);
193
+ } else if (interactionType === 'keyboard') {
194
+ if (row?.getAttribute('aria-disabled') === 'true') {
195
+ return;
196
+ }
197
+
198
+ // TODO: We always Use Option/Ctrl when keyboard navigating so selection isn't changed
199
+ // in selectionmode="replace"/highlight selection when navigating to the row that the user wants
200
+ // to expand. Discuss if this is useful or not
201
+ await this.keyboardNavigateToRow({row});
202
+ if (row.getAttribute('aria-expanded') === 'true') {
203
+ await this.user.keyboard('[ArrowLeft]');
204
+ } else {
205
+ await this.user.keyboard('[ArrowRight]');
206
+ }
207
+ }
208
+ };
209
+
165
210
  /**
166
211
  * Toggles the sort order for the specified table column. Defaults to using the interaction type set on the table tester.
167
212
  */
package/src/tabs.ts CHANGED
@@ -79,6 +79,11 @@ export class TabsTester {
79
79
  private async keyboardNavigateToTab(opts: {tab: HTMLElement, orientation?: Orientation}) {
80
80
  let {tab, orientation = 'vertical'} = opts;
81
81
  let tabs = this.tabs;
82
+ tabs = tabs.filter(tab => !(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'));
83
+ if (tabs.length === 0) {
84
+ throw new Error('Tablist doesnt have any non-disabled tabs. Please double check your tabs implementation.');
85
+ }
86
+
82
87
  let targetIndex = tabs.indexOf(tab);
83
88
  if (targetIndex === -1) {
84
89
  throw new Error('Tab provided is not in the tablist');
@@ -89,11 +94,11 @@ export class TabsTester {
89
94
  if (selectedTab != null) {
90
95
  act(() => selectedTab.focus());
91
96
  } else {
92
- act(() => tabs.find(tab => !(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'))?.focus());
97
+ act(() => tabs[0]?.focus());
93
98
  }
94
99
  }
95
100
 
96
- let currIndex = this.tabs.indexOf(document.activeElement as HTMLElement);
101
+ let currIndex = tabs.indexOf(document.activeElement as HTMLElement);
97
102
  if (currIndex === -1) {
98
103
  throw new Error('ActiveElement is not in the tablist');
99
104
  }
package/src/testSetup.ts CHANGED
@@ -35,35 +35,37 @@ export function installMouseEvent(): void {
35
35
  });
36
36
  }
37
37
 
38
+ export function definePointerEvent(): void {
39
+ // @ts-ignore
40
+ global.PointerEvent = class FakePointerEvent extends MouseEvent {
41
+ _init: {pageX: number, pageY: number, pointerType: string, pointerId: number, width: number, height: number};
42
+ constructor(name, init) {
43
+ super(name, init);
44
+ this._init = init;
45
+ }
46
+ get pointerType() {
47
+ return this._init.pointerType ?? 'mouse';
48
+ }
49
+ get pointerId() {
50
+ return this._init.pointerId;
51
+ }
52
+ get pageX() {
53
+ return this._init.pageX;
54
+ }
55
+ get pageY() {
56
+ return this._init.pageY;
57
+ }
58
+ get width() {
59
+ return this._init.width;
60
+ }
61
+ get height() {
62
+ return this._init.height;
63
+ }
64
+ };
65
+ }
66
+
38
67
  export function installPointerEvent(): void {
39
- beforeAll(() => {
40
- // @ts-ignore
41
- global.PointerEvent = class FakePointerEvent extends MouseEvent {
42
- _init: {pageX: number, pageY: number, pointerType: string, pointerId: number, width: number, height: number};
43
- constructor(name, init) {
44
- super(name, init);
45
- this._init = init;
46
- }
47
- get pointerType() {
48
- return this._init.pointerType ?? 'mouse';
49
- }
50
- get pointerId() {
51
- return this._init.pointerId;
52
- }
53
- get pageX() {
54
- return this._init.pageX;
55
- }
56
- get pageY() {
57
- return this._init.pageY;
58
- }
59
- get width() {
60
- return this._init.width;
61
- }
62
- get height() {
63
- return this._init.height;
64
- }
65
- };
66
- });
68
+ beforeAll(definePointerEvent);
67
69
  afterAll(() => {
68
70
  // @ts-ignore
69
71
  delete global.PointerEvent;
package/src/types.ts CHANGED
@@ -39,6 +39,8 @@ export interface BaseTesterOpts extends UserOpts {
39
39
  root: HTMLElement
40
40
  }
41
41
 
42
+ export interface CheckboxGroupTesterOpts extends BaseTesterOpts {}
43
+
42
44
  export interface ComboBoxTesterOpts extends BaseTesterOpts {
43
45
  /**
44
46
  * The base element for the combobox. If provided the wrapping element around the target combobox (as is the the case with a ref provided to RSP ComboBox),
@@ -52,6 +54,17 @@ export interface ComboBoxTesterOpts extends BaseTesterOpts {
52
54
  trigger?: HTMLElement
53
55
  }
54
56
 
57
+ export interface DialogTesterOpts extends BaseTesterOpts {
58
+ /**
59
+ * The trigger element for the dialog.
60
+ */
61
+ root: HTMLElement,
62
+ /**
63
+ * The overlay type of the dialog. Used to inform the tester how to find the dialog.
64
+ */
65
+ overlayType?: 'modal' | 'popover'
66
+ }
67
+
55
68
  export interface GridListTesterOpts extends BaseTesterOpts {}
56
69
 
57
70
  export interface ListBoxTesterOpts extends BaseTesterOpts {
@@ -76,6 +89,14 @@ export interface MenuTesterOpts extends BaseTesterOpts {
76
89
  rootMenu?: HTMLElement
77
90
  }
78
91
 
92
+ export interface RadioGroupTesterOpts extends BaseTesterOpts {
93
+ /**
94
+ * The horizontal layout direction, typically affected by locale.
95
+ * @default 'ltr'
96
+ */
97
+ direction?: Direction
98
+ }
99
+
79
100
  export interface SelectTesterOpts extends BaseTesterOpts {
80
101
  /**
81
102
  * The trigger element for the select. If provided the wrapping element around the target select (as is the case with a ref provided to RSP Select),
package/src/user.ts CHANGED
@@ -10,22 +10,28 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {ComboBoxTester} from './combobox';
13
+ import {CheckboxGroupTester} from './checkboxgroup';
14
14
  import {
15
+ CheckboxGroupTesterOpts,
15
16
  ComboBoxTesterOpts,
17
+ DialogTesterOpts,
16
18
  GridListTesterOpts,
17
19
  ListBoxTesterOpts,
18
20
  MenuTesterOpts,
21
+ RadioGroupTesterOpts,
19
22
  SelectTesterOpts,
20
23
  TableTesterOpts,
21
24
  TabsTesterOpts,
22
25
  TreeTesterOpts,
23
26
  UserOpts
24
27
  } from './types';
28
+ import {ComboBoxTester} from './combobox';
29
+ import {DialogTester} from './dialog';
25
30
  import {GridListTester} from './gridlist';
26
31
  import {ListBoxTester} from './listbox';
27
32
  import {MenuTester} from './menu';
28
33
  import {pointerMap} from './';
34
+ import {RadioGroupTester} from './radiogroup';
29
35
  import {SelectTester} from './select';
30
36
  import {TableTester} from './table';
31
37
  import {TabsTester} from './tabs';
@@ -33,21 +39,27 @@ import {TreeTester} from './tree';
33
39
  import userEvent from '@testing-library/user-event';
34
40
 
35
41
  let keyToUtil: {
36
- 'Select': typeof SelectTester,
37
- 'Table': typeof TableTester,
38
- 'Menu': typeof MenuTester,
42
+ 'CheckboxGroup': typeof CheckboxGroupTester,
39
43
  'ComboBox': typeof ComboBoxTester,
44
+ 'Dialog': typeof DialogTester,
40
45
  'GridList': typeof GridListTester,
41
46
  'ListBox': typeof ListBoxTester,
47
+ 'Menu': typeof MenuTester,
48
+ 'RadioGroup': typeof RadioGroupTester,
49
+ 'Select': typeof SelectTester,
50
+ 'Table': typeof TableTester,
42
51
  'Tabs': typeof TabsTester,
43
52
  'Tree': typeof TreeTester
44
53
  } = {
45
- 'Select': SelectTester,
46
- 'Table': TableTester,
47
- 'Menu': MenuTester,
54
+ 'CheckboxGroup': CheckboxGroupTester,
48
55
  'ComboBox': ComboBoxTester,
56
+ 'Dialog': DialogTester,
49
57
  'GridList': GridListTester,
50
58
  'ListBox': ListBoxTester,
59
+ 'Menu': MenuTester,
60
+ 'RadioGroup': RadioGroupTester,
61
+ 'Select': SelectTester,
62
+ 'Table': TableTester,
51
63
  'Tabs': TabsTester,
52
64
  'Tree': TreeTester
53
65
  } as const;
@@ -55,10 +67,13 @@ export type PatternNames = keyof typeof keyToUtil;
55
67
 
56
68
  // Conditional type: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
57
69
  type Tester<T> =
70
+ T extends 'CheckboxGroup' ? CheckboxGroupTester :
58
71
  T extends 'ComboBox' ? ComboBoxTester :
72
+ T extends 'Dialog' ? DialogTester :
59
73
  T extends 'GridList' ? GridListTester :
60
74
  T extends 'ListBox' ? ListBoxTester :
61
75
  T extends 'Menu' ? MenuTester :
76
+ T extends 'RadioGroup' ? RadioGroupTester :
62
77
  T extends 'Select' ? SelectTester :
63
78
  T extends 'Table' ? TableTester :
64
79
  T extends 'Tabs' ? TabsTester :
@@ -66,10 +81,13 @@ type Tester<T> =
66
81
  never;
67
82
 
68
83
  type TesterOpts<T> =
84
+ T extends 'CheckboxGroup' ? CheckboxGroupTesterOpts :
69
85
  T extends 'ComboBox' ? ComboBoxTesterOpts :
86
+ T extends 'Dialog' ? DialogTesterOpts :
70
87
  T extends 'GridList' ? GridListTesterOpts :
71
88
  T extends 'ListBox' ? ListBoxTesterOpts :
72
89
  T extends 'Menu' ? MenuTesterOpts :
90
+ T extends 'RadioGroup' ? RadioGroupTesterOpts :
73
91
  T extends 'Select' ? SelectTesterOpts :
74
92
  T extends 'Table' ? TableTesterOpts :
75
93
  T extends 'Tabs' ? TabsTesterOpts :
@@ -1 +0,0 @@
1
- {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAwBM,MAAM;IAqCX;;GAEC,GACD,mBAAmB,IAAiC,EAAQ;QAC1D,IAAI,CAAC,gB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,mBAAmB;QAE3C,8IAA8I;QAC9I,2FAA2F;QAC3F,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,WAAW,CAAA,GAAA,iCAAK,EAAE,MAAM,WAAW,CAAC;QACxC,IAAI,UACF,IAAI,CAAC,SAAS,GAAG;QAGnB,6KAA6K;QAC7K,IAAI,SACF,IAAI,CAAC,QAAQ,GAAG;aACX;YACL,IAAI,UAAU,CAAA,GAAA,iCAAK,EAAE,MAAM,cAAc,CAAC,UAAU;gBAAC,QAAQ;YAAI;YAEjE,IAAI,QAAQ,MAAM,KAAK,GACrB,UAAU,OAAO,CAAC,EAAE;iBACf,IAAI,QAAQ,MAAM,GAAG,GAC1B,UAAU,QAAQ,IAAI,CAAC,CAAA,SAAU,OAAO,YAAY,CAAC;YAGvD,qHAAqH;YACrH,6BAA6B;YAC7B,IAAI,CAAC,QAAQ,GAAG,WAAW,IAAI,CAAC,SAAS;QAC3C;IACF;AA8LF","sources":["packages/@react-aria/test-utils/src/combobox.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {act, waitFor, within} from '@testing-library/react';\nimport {ComboBoxTesterOpts, UserOpts} from './types';\n\ninterface ComboBoxOpenOpts {\n /**\n * Whether the combobox opens on focus or needs to be manually opened via user action.\n * @default 'manual'\n */\n triggerBehavior?: 'focus' | 'manual',\n /**\n * What interaction type to use when opening the combobox. Defaults to the interaction type set on the tester.\n */\n interactionType?: UserOpts['interactionType']\n}\n\ninterface ComboBoxSelectOpts extends ComboBoxOpenOpts {\n /**\n * The index, text, or node of the option to select. Option nodes can be sourced via `options()`.\n */\n option: number | string | HTMLElement\n}\n\nexport class ComboBoxTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _combobox: HTMLElement;\n private _trigger: HTMLElement;\n\n constructor(opts: ComboBoxTesterOpts) {\n let {root, trigger, user, interactionType} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n\n // Handle case where element provided is a wrapper around the combobox. The expectation is that the user at least uses a ref/data attribute to\n // query their combobox/combobox wrapper (in the case of RSP) which they then pass to thhis\n this._combobox = root;\n let combobox = within(root).queryByRole('combobox');\n if (combobox) {\n this._combobox = combobox;\n }\n\n // This is for if user need to directly set the trigger button element (aka the element provided in setElement was the combobox input or the trigger is somewhere unexpected)\n if (trigger) {\n this._trigger = trigger;\n } else {\n let buttons = within(root).queryAllByRole('button', {hidden: true});\n\n if (buttons.length === 1) {\n trigger = buttons[0];\n } else if (buttons.length > 1) {\n trigger = buttons.find(button => button.hasAttribute('aria-haspopup'));\n }\n\n // For cases like https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/ where the combobox\n // is also the trigger button\n this._trigger = trigger || this._combobox;\n }\n }\n\n /**\n * Set the interaction type used by the combobox tester.\n */\n setInteractionType(type: UserOpts['interactionType']): void {\n this._interactionType = type;\n }\n\n /**\n * Opens the combobox dropdown. Defaults to using the interaction type set on the combobox tester.\n */\n async open(opts: ComboBoxOpenOpts = {}): Promise<void> {\n let {triggerBehavior = 'manual', interactionType = this._interactionType} = opts;\n let trigger = this.trigger;\n let combobox = this.combobox;\n let isDisabled = trigger!.hasAttribute('disabled');\n\n if (interactionType === 'mouse') {\n if (triggerBehavior === 'focus') {\n await this.user.click(combobox);\n } else {\n await this.user.click(trigger);\n }\n } else if (interactionType === 'keyboard' && this._trigger != null) {\n act(() => this._trigger!.focus());\n if (triggerBehavior !== 'focus') {\n await this.user.keyboard('{ArrowDown}');\n }\n } else if (interactionType === 'touch') {\n if (triggerBehavior === 'focus') {\n await this.user.pointer({target: combobox, keys: '[TouchA]'});\n } else {\n await this.user.pointer({target: trigger, keys: '[TouchA]'});\n }\n }\n\n await waitFor(() => {\n if (!isDisabled && combobox.getAttribute('aria-controls') == null) {\n throw new Error('No aria-controls found on combobox trigger element.');\n } else {\n return true;\n }\n });\n let listBoxId = combobox.getAttribute('aria-controls');\n await waitFor(() => {\n if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {\n throw new Error(`Listbox with id of ${listBoxId} not found in document.`);\n } else {\n return true;\n }\n });\n }\n\n /**\n * Returns an option matching the specified index or text content.\n */\n findOption(opts: {optionIndexOrText: number | string}): HTMLElement {\n let {\n optionIndexOrText\n } = opts;\n\n let option;\n let options = this.options();\n let listbox = this.listbox;\n\n if (typeof optionIndexOrText === 'number') {\n option = options[optionIndexOrText];\n } else if (typeof optionIndexOrText === 'string' && listbox != null) {\n option = (within(listbox!).getByText(optionIndexOrText).closest('[role=option]'))! as HTMLElement;\n }\n\n return option;\n }\n\n /**\n * Selects the desired combobox option. Defaults to using the interaction type set on the combobox tester. If necessary, will open the combobox dropdown beforehand.\n * The desired option can be targeted via the option's node, the option's text, or the option's index.\n */\n async selectOption(opts: ComboBoxSelectOpts): Promise<void> {\n let {option, triggerBehavior, interactionType = this._interactionType} = opts;\n if (!this.combobox.getAttribute('aria-controls')) {\n await this.open({triggerBehavior});\n }\n\n let listbox = this.listbox;\n if (!listbox) {\n throw new Error('Combobox\\'s listbox not found.');\n }\n\n if (listbox) {\n if (typeof option === 'string' || typeof option === 'number') {\n option = this.findOption({optionIndexOrText: option});\n }\n\n if (!option) {\n throw new Error('Target option not found in the listbox.');\n }\n\n // TODO: keyboard method of selecting the the option is a bit tricky unless I simply simulate the user pressing the down arrow\n // the required amount of times to reach the option. For now just click the option even in keyboard mode\n if (interactionType === 'mouse' || interactionType === 'keyboard') {\n await this.user.click(option);\n } else {\n await this.user.pointer({target: option, keys: '[TouchA]'});\n }\n\n if (option.getAttribute('href') == null) {\n await waitFor(() => {\n if (document.contains(listbox)) {\n throw new Error('Expected listbox element to not be in the document after selecting an option');\n } else {\n return true;\n }\n });\n }\n } else {\n throw new Error(\"Attempted to select a option in the combobox, but the listbox wasn't found.\");\n }\n }\n\n /**\n * Closes the combobox dropdown.\n */\n async close(): Promise<void> {\n let listbox = this.listbox;\n if (listbox) {\n act(() => this.combobox.focus());\n await this.user.keyboard('[Escape]');\n\n await waitFor(() => {\n if (document.contains(listbox)) {\n throw new Error('Expected listbox element to not be in the document after selecting an option');\n } else {\n return true;\n }\n });\n }\n }\n\n /**\n * Returns the combobox.\n */\n get combobox(): HTMLElement {\n return this._combobox;\n }\n\n /**\n * Returns the combobox trigger button.\n */\n get trigger(): HTMLElement {\n return this._trigger;\n }\n\n /**\n * Returns the combobox's listbox if present.\n */\n get listbox(): HTMLElement | null {\n let listBoxId = this.combobox.getAttribute('aria-controls');\n return listBoxId ? document.getElementById(listBoxId) || null : null;\n }\n\n /**\n * Returns the combobox's sections if present.\n */\n get sections(): HTMLElement[] {\n let listbox = this.listbox;\n return listbox ? within(listbox).queryAllByRole('group') : [];\n }\n\n /**\n * Returns the combobox's options if present. Can be filtered to a subsection of the listbox if provided via `element`.\n */\n options(opts: {element?: HTMLElement} = {}): HTMLElement[] {\n let {element = this.listbox} = opts;\n let options = [];\n if (element) {\n options = within(element).queryAllByRole('option');\n }\n\n return options;\n }\n\n /**\n * Returns the currently focused option in the combobox's dropdown if any.\n */\n get focusedOption(): HTMLElement | null {\n let focusedOptionId = this.combobox.getAttribute('aria-activedescendant');\n return focusedOptionId ? document.getElementById(focusedOptionId) : null;\n }\n}\n"],"names":[],"version":3,"file":"combobox.main.js.map"}