@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,136 @@
1
+ export type Orientation = 'horizontal' | 'vertical';
2
+ export type Direction = 'ltr' | 'rtl';
3
+ export interface UserOpts {
4
+ /**
5
+ * The interaction type (mouse, touch, keyboard) that the test util user will use when interacting with a component. This can be overridden
6
+ * at the aria pattern tester level if needed.
7
+ * @default mouse
8
+ */
9
+ interactionType?: 'mouse' | 'touch' | 'keyboard';
10
+ /**
11
+ * A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table). This can be overridden
12
+ * at the aria pattern tester level if needed.
13
+ */
14
+ advanceTimer?: (time: number) => unknown | Promise<unknown>;
15
+ }
16
+ export interface BaseTesterOpts extends UserOpts {
17
+ /** @private */
18
+ user?: any;
19
+ /** The base element for the given tester (e.g. the table, menu trigger button, etc). */
20
+ root: HTMLElement;
21
+ }
22
+ export interface CheckboxGroupTesterOpts extends BaseTesterOpts {
23
+ }
24
+ export interface ComboBoxTesterOpts extends BaseTesterOpts {
25
+ /**
26
+ * 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),
27
+ * will automatically search for the combobox element within.
28
+ */
29
+ root: HTMLElement;
30
+ /**
31
+ * The node of the combobox trigger button if any. If not provided, we will try to automatically use any button
32
+ * within the `root` provided or that the `root` serves as the trigger.
33
+ */
34
+ trigger?: HTMLElement;
35
+ }
36
+ export interface DialogTesterOpts extends BaseTesterOpts {
37
+ /**
38
+ * The trigger element for the dialog.
39
+ */
40
+ root: HTMLElement;
41
+ /**
42
+ * The overlay type of the dialog. Used to inform the tester how to find the dialog.
43
+ */
44
+ overlayType?: 'modal' | 'popover';
45
+ }
46
+ export interface GridListTesterOpts extends BaseTesterOpts {
47
+ }
48
+ export interface ListBoxTesterOpts extends BaseTesterOpts {
49
+ /**
50
+ * A function used by the test utils to advance timers during interactions.
51
+ */
52
+ advanceTimer?: UserOpts['advanceTimer'];
53
+ }
54
+ export interface MenuTesterOpts extends BaseTesterOpts {
55
+ /**
56
+ * The trigger element for the menu.
57
+ */
58
+ root: HTMLElement;
59
+ /**
60
+ * Whether the current menu is a submenu.
61
+ */
62
+ isSubmenu?: boolean;
63
+ /**
64
+ * The root menu of the menu tree. Only available if the menu is a submenu.
65
+ */
66
+ rootMenu?: HTMLElement;
67
+ }
68
+ export interface RadioGroupTesterOpts extends BaseTesterOpts {
69
+ /**
70
+ * The horizontal layout direction, typically affected by locale.
71
+ * @default 'ltr'
72
+ */
73
+ direction?: Direction;
74
+ }
75
+ export interface SelectTesterOpts extends BaseTesterOpts {
76
+ /**
77
+ * 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),
78
+ * will automatically search for the select's trigger element within.
79
+ */
80
+ root: HTMLElement;
81
+ }
82
+ export interface TableTesterOpts extends BaseTesterOpts {
83
+ /**
84
+ * A function used by the test utils to advance timers during interactions.
85
+ */
86
+ advanceTimer?: UserOpts['advanceTimer'];
87
+ }
88
+ export interface TabsTesterOpts extends BaseTesterOpts {
89
+ /**
90
+ * The horizontal layout direction, typically affected by locale.
91
+ * @default 'ltr'
92
+ */
93
+ direction?: Direction;
94
+ }
95
+ export interface TreeTesterOpts extends BaseTesterOpts {
96
+ /**
97
+ * A function used by the test utils to advance timers during interactions.
98
+ */
99
+ advanceTimer?: UserOpts['advanceTimer'];
100
+ }
101
+ export interface BaseGridRowInteractionOpts {
102
+ /**
103
+ * The index, text, or node of the row to target.
104
+ */
105
+ row: number | string | HTMLElement;
106
+ /**
107
+ * What interaction type to use when interacting with the row. Defaults to the interaction type set on the tester.
108
+ */
109
+ interactionType?: UserOpts['interactionType'];
110
+ }
111
+ export interface ToggleGridRowOpts extends BaseGridRowInteractionOpts {
112
+ /**
113
+ * Whether the row needs to be long pressed to be selected. Depends on the components implementation.
114
+ */
115
+ needsLongPress?: boolean;
116
+ /**
117
+ * Whether the checkbox should be used to select the row. If false, will attempt to select the row via press.
118
+ * @default 'true'
119
+ */
120
+ checkboxSelection?: boolean;
121
+ /**
122
+ * Whether the grid has a selectionBehavior of "toggle" or "replace" (aka highlight selection). This affects the user operations
123
+ * required to toggle row selection by adding modifier keys during user actions, useful when performing multi-row selection in a "selectionBehavior: 'replace'" grid.
124
+ * If you would like to still simulate user actions (aka press) without these modifiers keys for a "selectionBehavior: replace" grid, simply omit this option.
125
+ * See the "Selection Behavior" section of the appropriate React Aria Component docs for more information (e.g. https://react-spectrum.adobe.com/react-aria/Tree.html#selection-behavior).
126
+ *
127
+ * @default 'toggle'
128
+ */
129
+ selectionBehavior?: 'toggle' | 'replace';
130
+ }
131
+ export interface GridRowActionOpts extends BaseGridRowInteractionOpts {
132
+ /**
133
+ * Whether or not the row needs a double click to trigger the row action. Depends on the components implementation.
134
+ */
135
+ needsDoubleClick?: boolean;
136
+ }
@@ -0,0 +1,47 @@
1
+ import { CheckboxGroupTester } from './checkboxgroup';
2
+ import { CheckboxGroupTesterOpts, ComboBoxTesterOpts, DialogTesterOpts, GridListTesterOpts, ListBoxTesterOpts, MenuTesterOpts, RadioGroupTesterOpts, SelectTesterOpts, TableTesterOpts, TabsTesterOpts, TreeTesterOpts, UserOpts } from './types';
3
+ import { ComboBoxTester } from './combobox';
4
+ import { DialogTester } from './dialog';
5
+ import { GridListTester } from './gridlist';
6
+ import { ListBoxTester } from './listbox';
7
+ import { MenuTester } from './menu';
8
+ import { RadioGroupTester } from './radiogroup';
9
+ import { SelectTester } from './select';
10
+ import { TableTester } from './table';
11
+ import { TabsTester } from './tabs';
12
+ import { TreeTester } from './tree';
13
+ declare let keyToUtil: {
14
+ 'CheckboxGroup': typeof CheckboxGroupTester;
15
+ 'ComboBox': typeof ComboBoxTester;
16
+ 'Dialog': typeof DialogTester;
17
+ 'GridList': typeof GridListTester;
18
+ 'ListBox': typeof ListBoxTester;
19
+ 'Menu': typeof MenuTester;
20
+ 'RadioGroup': typeof RadioGroupTester;
21
+ 'Select': typeof SelectTester;
22
+ 'Table': typeof TableTester;
23
+ 'Tabs': typeof TabsTester;
24
+ 'Tree': typeof TreeTester;
25
+ };
26
+ export type PatternNames = keyof typeof keyToUtil;
27
+ type Tester<T> = T extends 'CheckboxGroup' ? CheckboxGroupTester : T extends 'ComboBox' ? ComboBoxTester : T extends 'Dialog' ? DialogTester : T extends 'GridList' ? GridListTester : T extends 'ListBox' ? ListBoxTester : T extends 'Menu' ? MenuTester : T extends 'RadioGroup' ? RadioGroupTester : T extends 'Select' ? SelectTester : T extends 'Table' ? TableTester : T extends 'Tabs' ? TabsTester : T extends 'Tree' ? TreeTester : never;
28
+ type TesterOpts<T> = T extends 'CheckboxGroup' ? CheckboxGroupTesterOpts : T extends 'ComboBox' ? ComboBoxTesterOpts : T extends 'Dialog' ? DialogTesterOpts : T extends 'GridList' ? GridListTesterOpts : T extends 'ListBox' ? ListBoxTesterOpts : T extends 'Menu' ? MenuTesterOpts : T extends 'RadioGroup' ? RadioGroupTesterOpts : T extends 'Select' ? SelectTesterOpts : T extends 'Table' ? TableTesterOpts : T extends 'Tabs' ? TabsTesterOpts : T extends 'Tree' ? TreeTesterOpts : never;
29
+ export declare class User {
30
+ private user;
31
+ /**
32
+ * The interaction type (mouse, touch, keyboard) that the test util user will use when interacting with a component. This can be overridden
33
+ * at the aria pattern util level if needed.
34
+ * @default mouse
35
+ */
36
+ interactionType: UserOpts['interactionType'];
37
+ /**
38
+ * A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table).
39
+ */
40
+ advanceTimer: UserOpts['advanceTimer'];
41
+ constructor(opts?: UserOpts);
42
+ /**
43
+ * Creates an aria pattern tester, inheriting the options provided to the original user.
44
+ */
45
+ createTester<T extends PatternNames>(patternName: T, opts: TesterOpts<T>): Tester<T>;
46
+ }
47
+ export {};
@@ -0,0 +1,2 @@
1
+ import { pointerKey } from '@testing-library/user-event';
2
+ export declare let pointerMap: pointerKey[];
package/package.json CHANGED
@@ -1,20 +1,17 @@
1
1
  {
2
2
  "name": "@react-aria/test-utils",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "Testing utils for react-aria patterns",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
8
  "exports": {
9
9
  "source": "./src/index.ts",
10
- "types": [
11
- "./dist/types.d.ts",
12
- "./src/index.ts"
13
- ],
10
+ "types": "./dist/types/src/index.d.ts",
14
11
  "import": "./dist/import.mjs",
15
12
  "require": "./dist/main.js"
16
13
  },
17
- "types": "dist/types.d.ts",
14
+ "types": "./dist/types/src/index.d.ts",
18
15
  "source": "src/index.ts",
19
16
  "files": [
20
17
  "dist",
@@ -37,5 +34,8 @@
37
34
  "publishConfig": {
38
35
  "access": "public"
39
36
  },
40
- "gitHead": "0bda51183baa23306342af32a82012ea0fe0f2dc"
37
+ "targets": {
38
+ "types": false
39
+ },
40
+ "gitHead": "a6999bdf494a2e9c0381a5881908328bdd22ddae"
41
41
  }
@@ -0,0 +1,158 @@
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 {CheckboxGroupTesterOpts, UserOpts} from './types';
15
+ import {pressElement} from './events';
16
+
17
+ interface TriggerCheckboxOptions {
18
+ /**
19
+ * What interaction type to use when triggering a checkbox. Defaults to the interaction type set on the tester.
20
+ */
21
+ interactionType?: UserOpts['interactionType'],
22
+ /**
23
+ * The index, text, or node of the checkbox to toggle selection for.
24
+ */
25
+ checkbox: number | string | HTMLElement
26
+ }
27
+
28
+ export class CheckboxGroupTester {
29
+ private user;
30
+ private _interactionType: UserOpts['interactionType'];
31
+ private _checkboxgroup: HTMLElement;
32
+
33
+
34
+ constructor(opts: CheckboxGroupTesterOpts) {
35
+ let {root, user, interactionType} = opts;
36
+ this.user = user;
37
+ this._interactionType = interactionType || 'mouse';
38
+
39
+ this._checkboxgroup = root;
40
+ let checkboxgroup = within(root).queryAllByRole('group');
41
+ if (checkboxgroup.length > 0) {
42
+ this._checkboxgroup = checkboxgroup[0];
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Set the interaction type used by the checkbox group tester.
48
+ */
49
+ setInteractionType(type: UserOpts['interactionType']): void {
50
+ this._interactionType = type;
51
+ }
52
+
53
+ /**
54
+ * Returns a checkbox matching the specified index or text content.
55
+ */
56
+ findCheckbox(opts: {checkboxIndexOrText: number | string}): HTMLElement {
57
+ let {
58
+ checkboxIndexOrText
59
+ } = opts;
60
+
61
+ let checkbox;
62
+ if (typeof checkboxIndexOrText === 'number') {
63
+ checkbox = this.checkboxes[checkboxIndexOrText];
64
+ } else if (typeof checkboxIndexOrText === 'string') {
65
+ let label = within(this.checkboxgroup).getByText(checkboxIndexOrText);
66
+
67
+ // Label may wrap the checkbox, or the actual label may be a sibling span, or the checkbox div could have the label within it
68
+ if (label) {
69
+ checkbox = within(label).queryByRole('checkbox');
70
+ if (!checkbox) {
71
+ let labelWrapper = label.closest('label');
72
+ if (labelWrapper) {
73
+ checkbox = within(labelWrapper).queryByRole('checkbox');
74
+ } else {
75
+ checkbox = label.closest('[role=checkbox]');
76
+ }
77
+ }
78
+ }
79
+ }
80
+
81
+ return checkbox;
82
+ }
83
+
84
+ private async keyboardNavigateToCheckbox(opts: {checkbox: HTMLElement}) {
85
+ let {checkbox} = opts;
86
+ let checkboxes = this.checkboxes;
87
+ checkboxes = checkboxes.filter(checkbox => !(checkbox.hasAttribute('disabled') || checkbox.getAttribute('aria-disabled') === 'true'));
88
+ if (checkboxes.length === 0) {
89
+ throw new Error('Checkbox group doesnt have any non-disabled checkboxes. Please double check your checkbox group.');
90
+ }
91
+
92
+ let targetIndex = checkboxes.indexOf(checkbox);
93
+ if (targetIndex === -1) {
94
+ throw new Error('Checkbox provided is not in the checkbox group.');
95
+ }
96
+
97
+ if (!this.checkboxgroup.contains(document.activeElement)) {
98
+ act(() => checkboxes[0].focus());
99
+ }
100
+
101
+ let currIndex = checkboxes.indexOf(document.activeElement as HTMLElement);
102
+ if (currIndex === -1) {
103
+ throw new Error('Active element is not in the checkbox group.');
104
+ }
105
+
106
+ for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
107
+ await this.user.tab({shift: targetIndex < currIndex});
108
+ }
109
+ };
110
+
111
+ /**
112
+ * Toggles the specified checkbox. Defaults to using the interaction type set on the checkbox tester.
113
+ */
114
+ async toggleCheckbox(opts: TriggerCheckboxOptions): Promise<void> {
115
+ let {
116
+ checkbox,
117
+ interactionType = this._interactionType
118
+ } = opts;
119
+
120
+ if (typeof checkbox === 'string' || typeof checkbox === 'number') {
121
+ checkbox = this.findCheckbox({checkboxIndexOrText: checkbox});
122
+ }
123
+
124
+ if (!checkbox) {
125
+ throw new Error('Target checkbox not found in the checkboxgroup.');
126
+ } else if (checkbox.hasAttribute('disabled')) {
127
+ throw new Error('Target checkbox is disabled.');
128
+ }
129
+
130
+ if (interactionType === 'keyboard') {
131
+ await this.keyboardNavigateToCheckbox({checkbox});
132
+ await this.user.keyboard('[Space]');
133
+ } else {
134
+ await pressElement(this.user, checkbox, interactionType);
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Returns the checkboxgroup.
140
+ */
141
+ get checkboxgroup(): HTMLElement {
142
+ return this._checkboxgroup;
143
+ }
144
+
145
+ /**
146
+ * Returns the checkboxes.
147
+ */
148
+ get checkboxes(): HTMLElement[] {
149
+ return within(this.checkboxgroup).queryAllByRole('checkbox');
150
+ }
151
+
152
+ /**
153
+ * Returns the currently selected checkboxes in the checkboxgroup if any.
154
+ */
155
+ get selectedCheckboxes(): HTMLElement[] {
156
+ return this.checkboxes.filter(checkbox => (checkbox as HTMLInputElement).checked || checkbox.getAttribute('aria-checked') === 'true');
157
+ }
158
+ }
package/src/dialog.ts ADDED
@@ -0,0 +1,143 @@
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, waitFor, within} from '@testing-library/react';
14
+ import {DialogTesterOpts, UserOpts} from './types';
15
+
16
+ interface DialogOpenOpts {
17
+ /**
18
+ * What interaction type to use when opening the dialog. Defaults to the interaction type set on the tester.
19
+ */
20
+ interactionType?: UserOpts['interactionType']
21
+ }
22
+
23
+ export class DialogTester {
24
+ private user;
25
+ private _interactionType: UserOpts['interactionType'];
26
+ private _trigger: HTMLElement | undefined;
27
+ private _dialog: HTMLElement | undefined;
28
+ private _overlayType: DialogTesterOpts['overlayType'];
29
+
30
+ constructor(opts: DialogTesterOpts) {
31
+ let {root, user, interactionType, overlayType} = opts;
32
+ this.user = user;
33
+ this._interactionType = interactionType || 'mouse';
34
+ this._overlayType = overlayType || 'modal';
35
+
36
+ // Handle case where element provided is a wrapper of the trigger button
37
+ let trigger = within(root).queryByRole('button');
38
+ if (trigger) {
39
+ this._trigger = trigger;
40
+ } else {
41
+ this._trigger = root;
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Set the interaction type used by the dialog tester.
47
+ */
48
+ setInteractionType(type: UserOpts['interactionType']): void {
49
+ this._interactionType = type;
50
+ }
51
+
52
+ /**
53
+ * Opens the dialog. Defaults to using the interaction type set on the dialog tester.
54
+ */
55
+ async open(opts: DialogOpenOpts = {}): Promise<void> {
56
+ let {
57
+ interactionType = this._interactionType
58
+ } = opts;
59
+ let trigger = this.trigger;
60
+ if (!trigger.hasAttribute('disabled')) {
61
+ if (interactionType === 'mouse') {
62
+ await this.user.click(trigger);
63
+ } else if (interactionType === 'touch') {
64
+ await this.user.pointer({target: trigger, keys: '[TouchA]'});
65
+ } else if (interactionType === 'keyboard') {
66
+ act(() => trigger.focus());
67
+ await this.user.keyboard('[Enter]');
68
+ }
69
+
70
+ if (this._overlayType === 'popover') {
71
+ await waitFor(() => {
72
+ if (trigger.getAttribute('aria-controls') == null) {
73
+ throw new Error('No aria-controls found on dialog trigger element.');
74
+ } else {
75
+ return true;
76
+ }
77
+ });
78
+
79
+ let dialogId = trigger.getAttribute('aria-controls');
80
+ await waitFor(() => {
81
+ if (!dialogId || document.getElementById(dialogId) == null) {
82
+ throw new Error(`Dialog with id of ${dialogId} not found in document.`);
83
+ } else {
84
+ this._dialog = document.getElementById(dialogId)!;
85
+ return true;
86
+ }
87
+ });
88
+ } else {
89
+ let dialog;
90
+ await waitFor(() => {
91
+ dialog = document.querySelector('[role=dialog], [role=alertdialog]');
92
+ if (dialog == null) {
93
+ throw new Error('No dialog of type role="dialog" or role="alertdialog" found after pressing the trigger.');
94
+ } else {
95
+ return true;
96
+ }
97
+ });
98
+
99
+ if (dialog && document.activeElement !== this._trigger && dialog.contains(document.activeElement)) {
100
+ this._dialog = dialog;
101
+ } else {
102
+ throw new Error('New modal dialog doesnt contain the active element OR the active element is still the trigger. Uncertain if the proper modal dialog was found');
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Closes the dialog via the Escape key.
110
+ */
111
+ async close(): Promise<void> {
112
+ let dialog = this._dialog;
113
+ if (dialog) {
114
+ await this.user.keyboard('[Escape]');
115
+ await waitFor(() => {
116
+ if (document.contains(dialog)) {
117
+ throw new Error('Expected the dialog to not be in the document after closing it.');
118
+ } else {
119
+ this._dialog = undefined;
120
+ return true;
121
+ }
122
+ });
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Returns the dialog's trigger.
128
+ */
129
+ get trigger(): HTMLElement {
130
+ if (!this._trigger) {
131
+ throw new Error('No trigger element found for dialog.');
132
+ }
133
+
134
+ return this._trigger;
135
+ }
136
+
137
+ /**
138
+ * Returns the dialog if present.
139
+ */
140
+ get dialog(): HTMLElement | null {
141
+ return this._dialog && document.contains(this._dialog) ? this._dialog : null;
142
+ }
143
+ }
package/src/index.ts CHANGED
@@ -14,5 +14,16 @@ export {triggerLongPress} from './events';
14
14
  export {installMouseEvent, installPointerEvent} from './testSetup';
15
15
  export {pointerMap} from './userEventMaps';
16
16
  export {User} from './user';
17
+ export type {CheckboxGroupTester} from './checkboxgroup';
18
+ export type {ComboBoxTester} from './combobox';
19
+ export type {DialogTester} from './dialog';
20
+ export type {GridListTester} from './gridlist';
21
+ export type {ListBoxTester} from './listbox';
22
+ export type {MenuTester} from './menu';
23
+ export type {RadioGroupTester} from './radiogroup';
24
+ export type {SelectTester} from './select';
25
+ export type {TableTester} from './table';
26
+ export type {TabsTester} from './tabs';
27
+ export type {TreeTester} from './tree';
17
28
 
18
29
  export type {UserOpts} from './types';