@react-aria/test-utils 1.0.0-alpha.3 → 1.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/combobox.main.js +116 -82
- package/dist/combobox.main.js.map +1 -1
- package/dist/combobox.mjs +117 -83
- package/dist/combobox.module.js +117 -83
- package/dist/combobox.module.js.map +1 -1
- package/dist/events.main.js.map +1 -1
- package/dist/events.module.js.map +1 -1
- package/dist/gridlist.main.js +102 -59
- package/dist/gridlist.main.js.map +1 -1
- package/dist/gridlist.mjs +103 -60
- package/dist/gridlist.module.js +103 -60
- package/dist/gridlist.module.js.map +1 -1
- package/dist/listbox.main.js +135 -0
- package/dist/listbox.main.js.map +1 -0
- package/dist/listbox.mjs +130 -0
- package/dist/listbox.module.js +130 -0
- package/dist/listbox.module.js.map +1 -0
- package/dist/main.js.map +1 -1
- package/dist/menu.main.js +195 -161
- package/dist/menu.main.js.map +1 -1
- package/dist/menu.mjs +196 -162
- package/dist/menu.module.js +196 -162
- package/dist/menu.module.js.map +1 -1
- package/dist/module.js.map +1 -1
- package/dist/select.main.js +116 -70
- package/dist/select.main.js.map +1 -1
- package/dist/select.mjs +117 -71
- package/dist/select.module.js +117 -71
- package/dist/select.module.js.map +1 -1
- package/dist/table.main.js +170 -139
- package/dist/table.main.js.map +1 -1
- package/dist/table.mjs +171 -140
- package/dist/table.module.js +171 -140
- package/dist/table.module.js.map +1 -1
- package/dist/tabs.main.js +133 -0
- package/dist/tabs.main.js.map +1 -0
- package/dist/tabs.mjs +128 -0
- package/dist/tabs.module.js +128 -0
- package/dist/tabs.module.js.map +1 -0
- package/dist/tree.main.js +165 -0
- package/dist/tree.main.js.map +1 -0
- package/dist/tree.mjs +160 -0
- package/dist/tree.module.js +160 -0
- package/dist/tree.module.js.map +1 -0
- package/dist/types.d.ts +607 -145
- package/dist/types.d.ts.map +1 -1
- package/dist/user.main.js +15 -4
- package/dist/user.main.js.map +1 -1
- package/dist/user.mjs +15 -4
- package/dist/user.module.js +15 -4
- package/dist/user.module.js.map +1 -1
- package/package.json +2 -3
- package/src/combobox.ts +105 -36
- package/src/events.ts +1 -1
- package/src/gridlist.ts +146 -59
- package/src/index.ts +1 -1
- package/src/listbox.ts +226 -0
- package/src/menu.ts +152 -62
- package/src/select.ts +137 -44
- package/src/table.ts +129 -64
- package/src/tabs.ts +198 -0
- package/src/tree.ts +248 -0
- package/src/types.ts +133 -0
- package/src/user.ts +62 -37
package/src/tree.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
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, within} from '@testing-library/react';
|
|
14
|
+
import {BaseGridRowInteractionOpts, GridRowActionOpts, ToggleGridRowOpts, TreeTesterOpts, UserOpts} from './types';
|
|
15
|
+
import {pressElement, triggerLongPress} from './events';
|
|
16
|
+
|
|
17
|
+
interface TreeToggleExpansionOpts extends BaseGridRowInteractionOpts {}
|
|
18
|
+
interface TreeToggleRowOpts extends ToggleGridRowOpts {}
|
|
19
|
+
interface TreeRowActionOpts extends GridRowActionOpts {}
|
|
20
|
+
|
|
21
|
+
// TODO: this ended up being pretty much the same as gridlist, refactor so it extends from gridlist
|
|
22
|
+
export class TreeTester {
|
|
23
|
+
private user;
|
|
24
|
+
private _interactionType: UserOpts['interactionType'];
|
|
25
|
+
private _advanceTimer: UserOpts['advanceTimer'];
|
|
26
|
+
private _tree: HTMLElement;
|
|
27
|
+
|
|
28
|
+
constructor(opts: TreeTesterOpts) {
|
|
29
|
+
let {root, user, interactionType, advanceTimer} = opts;
|
|
30
|
+
this.user = user;
|
|
31
|
+
this._interactionType = interactionType || 'mouse';
|
|
32
|
+
this._advanceTimer = advanceTimer;
|
|
33
|
+
this._tree = root;
|
|
34
|
+
// TODO: should all helpers do this?
|
|
35
|
+
let tree = within(root).queryByRole('treegrid');
|
|
36
|
+
if (root.getAttribute('role') !== 'treegrid' && tree) {
|
|
37
|
+
this._tree = tree;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Set the interaction type used by the tree tester.
|
|
43
|
+
*/
|
|
44
|
+
setInteractionType(type: UserOpts['interactionType']) {
|
|
45
|
+
this._interactionType = type;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns a row matching the specified index or text content.
|
|
50
|
+
*/
|
|
51
|
+
findRow(opts: {rowIndexOrText: number | string}): HTMLElement {
|
|
52
|
+
let {
|
|
53
|
+
rowIndexOrText
|
|
54
|
+
} = opts;
|
|
55
|
+
|
|
56
|
+
let row;
|
|
57
|
+
if (typeof rowIndexOrText === 'number') {
|
|
58
|
+
row = this.rows[rowIndexOrText];
|
|
59
|
+
} else if (typeof rowIndexOrText === 'string') {
|
|
60
|
+
row = (within(this.tree!).getByText(rowIndexOrText).closest('[role=row]'))! as HTMLElement;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return row;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// TODO: RTL
|
|
67
|
+
private async keyboardNavigateToRow(opts: {row: HTMLElement}) {
|
|
68
|
+
let {row} = opts;
|
|
69
|
+
let rows = this.rows;
|
|
70
|
+
let targetIndex = rows.indexOf(row);
|
|
71
|
+
if (targetIndex === -1) {
|
|
72
|
+
throw new Error('Option provided is not in the tree');
|
|
73
|
+
}
|
|
74
|
+
if (document.activeElement === this.tree) {
|
|
75
|
+
await this.user.keyboard('[ArrowDown]');
|
|
76
|
+
} else if (this._tree.contains(document.activeElement) && document.activeElement!.getAttribute('role') !== 'row') {
|
|
77
|
+
do {
|
|
78
|
+
await this.user.keyboard('[ArrowLeft]');
|
|
79
|
+
} while (document.activeElement!.getAttribute('role') !== 'row');
|
|
80
|
+
}
|
|
81
|
+
let currIndex = rows.indexOf(document.activeElement as HTMLElement);
|
|
82
|
+
if (currIndex === -1) {
|
|
83
|
+
throw new Error('ActiveElement is not in the tree');
|
|
84
|
+
}
|
|
85
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {
|
|
88
|
+
await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Toggles the selection for the specified tree row. Defaults to using the interaction type set on the tree tester.
|
|
94
|
+
*/
|
|
95
|
+
async toggleRowSelection(opts: TreeToggleRowOpts) {
|
|
96
|
+
let {
|
|
97
|
+
row,
|
|
98
|
+
needsLongPress,
|
|
99
|
+
checkboxSelection = true,
|
|
100
|
+
interactionType = this._interactionType
|
|
101
|
+
} = opts;
|
|
102
|
+
|
|
103
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
104
|
+
row = this.findRow({rowIndexOrText: row});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!row) {
|
|
108
|
+
throw new Error('Target row not found in the tree.');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let rowCheckbox = within(row).queryByRole('checkbox');
|
|
112
|
+
|
|
113
|
+
// TODO: we early return here because the checkbox can't be keyboard navigated to if the row is disabled usually
|
|
114
|
+
// but we may to check for disabledBehavior (aka if the disable row gets skipped when keyboard navigating or not)
|
|
115
|
+
if (interactionType === 'keyboard' && (rowCheckbox?.getAttribute('disabled') === '' || row?.getAttribute('aria-disabled') === 'true')) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// this would be better than the check to do nothing in events.ts
|
|
120
|
+
// also, it'd be good to be able to trigger selection on the row instead of having to go to the checkbox directly
|
|
121
|
+
if (interactionType === 'keyboard' && !checkboxSelection) {
|
|
122
|
+
await this.keyboardNavigateToRow({row});
|
|
123
|
+
await this.user.keyboard('{Space}');
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (rowCheckbox && checkboxSelection) {
|
|
127
|
+
await pressElement(this.user, rowCheckbox, interactionType);
|
|
128
|
+
} else {
|
|
129
|
+
let cell = within(row).getAllByRole('gridcell')[0];
|
|
130
|
+
if (needsLongPress && interactionType === 'touch') {
|
|
131
|
+
if (this._advanceTimer == null) {
|
|
132
|
+
throw new Error('No advanceTimers provided for long press.');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Note that long press interactions with rows is strictly touch only for grid rows
|
|
136
|
+
await triggerLongPress({element: cell, advanceTimer: this._advanceTimer, pointerOpts: {pointerType: 'touch'}});
|
|
137
|
+
} else {
|
|
138
|
+
await pressElement(this.user, cell, interactionType);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Toggles the expansion for the specified tree row. Defaults to using the interaction type set on the tree tester.
|
|
145
|
+
*/
|
|
146
|
+
async toggleRowExpansion(opts: TreeToggleExpansionOpts) {
|
|
147
|
+
let {
|
|
148
|
+
row,
|
|
149
|
+
interactionType = this._interactionType
|
|
150
|
+
} = opts;
|
|
151
|
+
if (!this.tree.contains(document.activeElement)) {
|
|
152
|
+
await act(async () => {
|
|
153
|
+
this.tree.focus();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
158
|
+
row = this.findRow({rowIndexOrText: row});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!row) {
|
|
162
|
+
throw new Error('Target row not found in the tree.');
|
|
163
|
+
} else if (row.getAttribute('aria-expanded') == null) {
|
|
164
|
+
throw new Error('Target row is not expandable.');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (interactionType === 'mouse' || interactionType === 'touch') {
|
|
168
|
+
let rowExpander = within(row).getAllByRole('button')[0]; // what happens if the button is not first? how can we differentiate?
|
|
169
|
+
await pressElement(this.user, rowExpander, interactionType);
|
|
170
|
+
} else if (interactionType === 'keyboard') {
|
|
171
|
+
if (row?.getAttribute('aria-disabled') === 'true') {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
await this.keyboardNavigateToRow({row});
|
|
176
|
+
if (row.getAttribute('aria-expanded') === 'true') {
|
|
177
|
+
await this.user.keyboard('[ArrowLeft]');
|
|
178
|
+
} else {
|
|
179
|
+
await this.user.keyboard('[ArrowRight]');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Triggers the action for the specified tree row. Defaults to using the interaction type set on the tree tester.
|
|
186
|
+
*/
|
|
187
|
+
async triggerRowAction(opts: TreeRowActionOpts) {
|
|
188
|
+
let {
|
|
189
|
+
row,
|
|
190
|
+
needsDoubleClick,
|
|
191
|
+
interactionType = this._interactionType
|
|
192
|
+
} = opts;
|
|
193
|
+
|
|
194
|
+
if (typeof row === 'string' || typeof row === 'number') {
|
|
195
|
+
row = this.findRow({rowIndexOrText: row});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!row) {
|
|
199
|
+
throw new Error('Target row not found in the tree.');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (needsDoubleClick) {
|
|
203
|
+
await this.user.dblClick(row);
|
|
204
|
+
} else if (interactionType === 'keyboard') {
|
|
205
|
+
if (row?.getAttribute('aria-disabled') === 'true') {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) {
|
|
210
|
+
act(() => this._tree.focus());
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
await this.keyboardNavigateToRow({row});
|
|
214
|
+
await this.user.keyboard('[Enter]');
|
|
215
|
+
} else {
|
|
216
|
+
await pressElement(this.user, row, interactionType);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Returns the tree.
|
|
222
|
+
*/
|
|
223
|
+
get tree(): HTMLElement {
|
|
224
|
+
return this._tree;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Returns the tree's rows if any.
|
|
229
|
+
*/
|
|
230
|
+
get rows(): HTMLElement[] {
|
|
231
|
+
return within(this?.tree).queryAllByRole('row');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Returns the tree's selected rows if any.
|
|
236
|
+
*/
|
|
237
|
+
get selectedRows(): HTMLElement[] {
|
|
238
|
+
return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Returns the tree's cells if any. Can be filtered against a specific row if provided via `element`.
|
|
243
|
+
*/
|
|
244
|
+
cells(opts: {element?: HTMLElement} = {}): HTMLElement[] {
|
|
245
|
+
let {element = this.tree} = opts;
|
|
246
|
+
return within(element).queryAllByRole('gridcell');
|
|
247
|
+
}
|
|
248
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
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
|
+
export type Orientation = 'horizontal' | 'vertical';
|
|
14
|
+
export type Direction = 'ltr' | 'rtl';
|
|
15
|
+
|
|
16
|
+
// https://github.com/testing-library/dom-testing-library/issues/939#issuecomment-830771708 is an interesting way of allowing users to configure the timers
|
|
17
|
+
// curent way is like https://testing-library.com/docs/user-event/options/#advancetimers,
|
|
18
|
+
export interface UserOpts {
|
|
19
|
+
/**
|
|
20
|
+
* The interaction type (mouse, touch, keyboard) that the test util user will use when interacting with a component. This can be overridden
|
|
21
|
+
* at the aria pattern tester level if needed.
|
|
22
|
+
* @default mouse
|
|
23
|
+
*/
|
|
24
|
+
interactionType?: 'mouse' | 'touch' | 'keyboard',
|
|
25
|
+
// If using fake timers user should provide something like (time) => jest.advanceTimersByTime(time))}
|
|
26
|
+
// A real timer user would pass async () => await new Promise((resolve) => setTimeout(resolve, waitTime))
|
|
27
|
+
// Time is in ms.
|
|
28
|
+
/**
|
|
29
|
+
* A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table). This can be overridden
|
|
30
|
+
* at the aria pattern tester level if needed.
|
|
31
|
+
*/
|
|
32
|
+
advanceTimer?: (time?: number) => void | Promise<unknown>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BaseTesterOpts extends UserOpts {
|
|
36
|
+
/** @private */
|
|
37
|
+
user?: any,
|
|
38
|
+
/** The base element for the given tester (e.g. the table, menu trigger button, etc). */
|
|
39
|
+
root: HTMLElement
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ComboBoxTesterOpts extends BaseTesterOpts {
|
|
43
|
+
/**
|
|
44
|
+
* 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),
|
|
45
|
+
* will automatically search for the combobox element within.
|
|
46
|
+
*/
|
|
47
|
+
root: HTMLElement,
|
|
48
|
+
/**
|
|
49
|
+
* The node of the combobox trigger button if any. If not provided, we will try to automatically use any button
|
|
50
|
+
* within the `root` provided or that the `root` serves as the trigger.
|
|
51
|
+
*/
|
|
52
|
+
trigger?: HTMLElement
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface GridListTesterOpts extends BaseTesterOpts {}
|
|
56
|
+
|
|
57
|
+
export interface ListBoxTesterOpts extends BaseTesterOpts {
|
|
58
|
+
/**
|
|
59
|
+
* A function used by the test utils to advance timers during interactions.
|
|
60
|
+
*/
|
|
61
|
+
advanceTimer?: UserOpts['advanceTimer']
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface MenuTesterOpts extends BaseTesterOpts {
|
|
65
|
+
/**
|
|
66
|
+
* The trigger element for the menu.
|
|
67
|
+
*/
|
|
68
|
+
root: HTMLElement,
|
|
69
|
+
/**
|
|
70
|
+
* Whether the current menu is a submenu.
|
|
71
|
+
*/
|
|
72
|
+
isSubmenu?: boolean
|
|
73
|
+
}
|
|
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
|
+
|
|
83
|
+
export interface TableTesterOpts extends BaseTesterOpts {
|
|
84
|
+
/**
|
|
85
|
+
* A function used by the test utils to advance timers during interactions.
|
|
86
|
+
*/
|
|
87
|
+
advanceTimer?: UserOpts['advanceTimer']
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface TabsTesterOpts extends BaseTesterOpts {
|
|
91
|
+
/**
|
|
92
|
+
* The horizontal layout direction, typically affected by locale.
|
|
93
|
+
* @default 'ltr'
|
|
94
|
+
*/
|
|
95
|
+
direction?: Direction
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface TreeTesterOpts extends BaseTesterOpts {
|
|
99
|
+
/**
|
|
100
|
+
* A function used by the test utils to advance timers during interactions.
|
|
101
|
+
*/
|
|
102
|
+
advanceTimer?: UserOpts['advanceTimer']
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface BaseGridRowInteractionOpts {
|
|
106
|
+
/**
|
|
107
|
+
* The index, text, or node of the row to target.
|
|
108
|
+
*/
|
|
109
|
+
row: number | string | HTMLElement,
|
|
110
|
+
/**
|
|
111
|
+
* What interaction type to use when interacting with the row. Defaults to the interaction type set on the tester.
|
|
112
|
+
*/
|
|
113
|
+
interactionType?: UserOpts['interactionType']
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ToggleGridRowOpts extends BaseGridRowInteractionOpts {
|
|
117
|
+
/**
|
|
118
|
+
* Whether the row needs to be long pressed to be selected. Depends on the components implementation.
|
|
119
|
+
*/
|
|
120
|
+
needsLongPress?: boolean,
|
|
121
|
+
/**
|
|
122
|
+
* Whether the checkbox should be used to select the row. If false, will attempt to select the row via press.
|
|
123
|
+
* @default 'true'
|
|
124
|
+
*/
|
|
125
|
+
checkboxSelection?: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface GridRowActionOpts extends BaseGridRowInteractionOpts {
|
|
129
|
+
/**
|
|
130
|
+
* Whether or not the row needs a double click to trigger the row action. Depends on the components implementation.
|
|
131
|
+
*/
|
|
132
|
+
needsDoubleClick?: boolean
|
|
133
|
+
}
|
package/src/user.ts
CHANGED
|
@@ -10,54 +10,76 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
13
|
+
import {ComboBoxTester} from './combobox';
|
|
14
|
+
import {
|
|
15
|
+
ComboBoxTesterOpts,
|
|
16
|
+
GridListTesterOpts,
|
|
17
|
+
ListBoxTesterOpts,
|
|
18
|
+
MenuTesterOpts,
|
|
19
|
+
SelectTesterOpts,
|
|
20
|
+
TableTesterOpts,
|
|
21
|
+
TabsTesterOpts,
|
|
22
|
+
TreeTesterOpts,
|
|
23
|
+
UserOpts
|
|
24
|
+
} from './types';
|
|
25
|
+
import {GridListTester} from './gridlist';
|
|
26
|
+
import {ListBoxTester} from './listbox';
|
|
27
|
+
import {MenuTester} from './menu';
|
|
16
28
|
import {pointerMap} from './';
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
29
|
+
import {SelectTester} from './select';
|
|
30
|
+
import {TableTester} from './table';
|
|
31
|
+
import {TabsTester} from './tabs';
|
|
32
|
+
import {TreeTester} from './tree';
|
|
19
33
|
import userEvent from '@testing-library/user-event';
|
|
20
34
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export interface BaseTesterOpts {
|
|
32
|
-
// The base element for the given tester (e.g. the table, menu trigger, etc)
|
|
33
|
-
root: HTMLElement
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let keyToUtil = {'Select': SelectTester, 'Table': TableTester, 'Menu': MenuTester, 'ComboBox': ComboBoxTester, 'GridList': GridListTester} as const;
|
|
35
|
+
let keyToUtil = {
|
|
36
|
+
'Select': SelectTester,
|
|
37
|
+
'Table': TableTester,
|
|
38
|
+
'Menu': MenuTester,
|
|
39
|
+
'ComboBox': ComboBoxTester,
|
|
40
|
+
'GridList': GridListTester,
|
|
41
|
+
'ListBox': ListBoxTester,
|
|
42
|
+
'Tabs': TabsTester,
|
|
43
|
+
'Tree': TreeTester
|
|
44
|
+
} as const;
|
|
37
45
|
export type PatternNames = keyof typeof keyToUtil;
|
|
38
46
|
|
|
39
47
|
// Conditional type: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
|
|
40
|
-
type
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
type Tester<T> =
|
|
49
|
+
T extends 'ComboBox' ? ComboBoxTester :
|
|
50
|
+
T extends 'GridList' ? GridListTester :
|
|
51
|
+
T extends 'ListBox' ? ListBoxTester :
|
|
52
|
+
T extends 'Menu' ? MenuTester :
|
|
53
|
+
T extends 'Select' ? SelectTester :
|
|
54
|
+
T extends 'Table' ? TableTester :
|
|
55
|
+
T extends 'Tabs' ? TabsTester :
|
|
56
|
+
T extends 'Tree' ? TreeTester :
|
|
57
|
+
never;
|
|
47
58
|
|
|
48
|
-
type
|
|
49
|
-
T extends '
|
|
50
|
-
T extends '
|
|
51
|
-
T extends '
|
|
52
|
-
T extends '
|
|
53
|
-
T extends '
|
|
59
|
+
type TesterOpts<T> =
|
|
60
|
+
T extends 'ComboBox' ? ComboBoxTesterOpts :
|
|
61
|
+
T extends 'GridList' ? GridListTesterOpts :
|
|
62
|
+
T extends 'ListBox' ? ListBoxTesterOpts :
|
|
63
|
+
T extends 'Menu' ? MenuTesterOpts :
|
|
64
|
+
T extends 'Select' ? SelectTesterOpts :
|
|
65
|
+
T extends 'Table' ? TableTesterOpts :
|
|
66
|
+
T extends 'Tabs' ? TabsTesterOpts :
|
|
67
|
+
T extends 'Tree' ? TreeTesterOpts :
|
|
54
68
|
never;
|
|
55
69
|
|
|
56
70
|
let defaultAdvanceTimer = async (waitTime: number | undefined) => await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
57
71
|
|
|
58
72
|
export class User {
|
|
59
|
-
user;
|
|
73
|
+
private user;
|
|
74
|
+
/**
|
|
75
|
+
* The interaction type (mouse, touch, keyboard) that the test util user will use when interacting with a component. This can be overridden
|
|
76
|
+
* at the aria pattern util level if needed.
|
|
77
|
+
* @default mouse
|
|
78
|
+
*/
|
|
60
79
|
interactionType: UserOpts['interactionType'];
|
|
80
|
+
/**
|
|
81
|
+
* A function used by the test utils to advance timers during interactions. Required for certain aria patterns (e.g. table).
|
|
82
|
+
*/
|
|
61
83
|
advanceTimer: UserOpts['advanceTimer'];
|
|
62
84
|
|
|
63
85
|
constructor(opts: UserOpts = {}) {
|
|
@@ -67,7 +89,10 @@ export class User {
|
|
|
67
89
|
this.advanceTimer = advanceTimer || defaultAdvanceTimer;
|
|
68
90
|
}
|
|
69
91
|
|
|
70
|
-
|
|
71
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Creates an aria pattern tester, inheriting the options provided to the original user.
|
|
94
|
+
*/
|
|
95
|
+
createTester<T extends PatternNames>(patternName: T, opts: TesterOpts<T>): Tester<T> {
|
|
96
|
+
return new (keyToUtil)[patternName]({interactionType: this.interactionType, advanceTimer: this.advanceTimer, ...opts, user: this.user}) as Tester<T>;
|
|
72
97
|
}
|
|
73
98
|
}
|