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

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 (43) hide show
  1. package/dist/checkboxgroup.main.js +104 -0
  2. package/dist/checkboxgroup.main.js.map +1 -0
  3. package/dist/checkboxgroup.mjs +99 -0
  4. package/dist/checkboxgroup.module.js +99 -0
  5. package/dist/checkboxgroup.module.js.map +1 -0
  6. package/dist/dialog.main.js +105 -0
  7. package/dist/dialog.main.js.map +1 -0
  8. package/dist/dialog.mjs +100 -0
  9. package/dist/dialog.module.js +100 -0
  10. package/dist/dialog.module.js.map +1 -0
  11. package/dist/main.js.map +1 -1
  12. package/dist/module.js.map +1 -1
  13. package/dist/radiogroup.main.js +123 -0
  14. package/dist/radiogroup.main.js.map +1 -0
  15. package/dist/radiogroup.mjs +118 -0
  16. package/dist/radiogroup.module.js +118 -0
  17. package/dist/radiogroup.module.js.map +1 -0
  18. package/dist/tabs.main.js +5 -3
  19. package/dist/tabs.main.js.map +1 -1
  20. package/dist/tabs.mjs +5 -3
  21. package/dist/tabs.module.js +5 -3
  22. package/dist/tabs.module.js.map +1 -1
  23. package/dist/testSetup.main.js +29 -28
  24. package/dist/testSetup.main.js.map +1 -1
  25. package/dist/testSetup.mjs +30 -29
  26. package/dist/testSetup.module.js +30 -29
  27. package/dist/testSetup.module.js.map +1 -1
  28. package/dist/types.d.ts +142 -13
  29. package/dist/types.d.ts.map +1 -1
  30. package/dist/user.main.js +12 -3
  31. package/dist/user.main.js.map +1 -1
  32. package/dist/user.mjs +12 -3
  33. package/dist/user.module.js +12 -3
  34. package/dist/user.module.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/checkboxgroup.ts +158 -0
  37. package/src/dialog.ts +143 -0
  38. package/src/index.ts +11 -0
  39. package/src/radiogroup.ts +176 -0
  40. package/src/tabs.ts +7 -2
  41. package/src/testSetup.ts +30 -28
  42. package/src/types.ts +21 -0
  43. package/src/user.ts +25 -7
@@ -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/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 :