@react-aria/test-utils 1.0.0-alpha.0 → 1.0.0-alpha.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 (58) hide show
  1. package/dist/combobox.main.js +137 -0
  2. package/dist/combobox.main.js.map +1 -0
  3. package/dist/combobox.mjs +132 -0
  4. package/dist/combobox.module.js +132 -0
  5. package/dist/combobox.module.js.map +1 -0
  6. package/dist/events.main.js +25 -10
  7. package/dist/events.main.js.map +1 -1
  8. package/dist/events.mjs +26 -11
  9. package/dist/events.module.js +25 -10
  10. package/dist/events.module.js.map +1 -1
  11. package/dist/gridlist.main.js +97 -0
  12. package/dist/gridlist.main.js.map +1 -0
  13. package/dist/gridlist.mjs +92 -0
  14. package/dist/gridlist.module.js +92 -0
  15. package/dist/gridlist.module.js.map +1 -0
  16. package/dist/import.mjs +4 -2
  17. package/dist/main.js +10 -18
  18. package/dist/main.js.map +1 -1
  19. package/dist/menu.main.js +176 -0
  20. package/dist/menu.main.js.map +1 -0
  21. package/dist/menu.mjs +171 -0
  22. package/dist/menu.module.js +171 -0
  23. package/dist/menu.module.js.map +1 -0
  24. package/dist/module.js +4 -2
  25. package/dist/module.js.map +1 -1
  26. package/dist/select.main.js +113 -0
  27. package/dist/select.main.js.map +1 -0
  28. package/dist/select.mjs +108 -0
  29. package/dist/select.module.js +108 -0
  30. package/dist/select.module.js.map +1 -0
  31. package/dist/table.main.js +191 -0
  32. package/dist/table.main.js.map +1 -0
  33. package/dist/table.mjs +186 -0
  34. package/dist/table.module.js +186 -0
  35. package/dist/table.module.js.map +1 -0
  36. package/dist/testSetup.main.js +1 -1
  37. package/dist/testSetup.mjs +2 -2
  38. package/dist/testSetup.module.js +1 -1
  39. package/dist/types.d.ts +180 -4
  40. package/dist/types.d.ts.map +1 -1
  41. package/dist/user.main.js +65 -0
  42. package/dist/user.main.js.map +1 -0
  43. package/dist/user.mjs +56 -0
  44. package/dist/user.module.js +56 -0
  45. package/dist/user.module.js.map +1 -0
  46. package/dist/userEventMaps.main.js +15 -15
  47. package/dist/userEventMaps.mjs +16 -16
  48. package/dist/userEventMaps.module.js +15 -15
  49. package/package.json +6 -7
  50. package/src/combobox.ts +188 -0
  51. package/src/events.ts +28 -8
  52. package/src/gridlist.ts +116 -0
  53. package/src/index.ts +6 -3
  54. package/src/menu.ts +246 -0
  55. package/src/select.ts +158 -0
  56. package/src/table.ts +282 -0
  57. package/src/user.ts +73 -0
  58. package/LICENSE +0 -201
@@ -0,0 +1,56 @@
1
+ import {ComboBoxTester as $dab75f394483aa9c$export$f97e14e96d71ab3b} from "./combobox.module.js";
2
+ import {GridListTester as $5ca9e9228f508f75$export$93a85aaed9fabd83} from "./gridlist.module.js";
3
+ import {MenuTester as $74b93f0617179929$export$f73bbc9212ed861e} from "./menu.module.js";
4
+ import {pointerMap as $50ecfb18069ec897$export$7dbde2c4caaa8d35} from "./userEventMaps.module.js";
5
+ import {SelectTester as $4350df3e19e40e36$export$d1859707465446a9} from "./select.module.js";
6
+ import {TableTester as $6d1e4cf220920c37$export$4c6a9d6ae3b0086} from "./table.module.js";
7
+ import $gmkhN$testinglibraryuserevent from "@testing-library/user-event";
8
+
9
+ /*
10
+ * Copyright 2024 Adobe. All rights reserved.
11
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License. You may obtain a copy
13
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software distributed under
16
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
17
+ * OF ANY KIND, either express or implied. See the License for the specific language
18
+ * governing permissions and limitations under the License.
19
+ */
20
+
21
+
22
+
23
+
24
+
25
+
26
+ let $0d71b4e9cc4df40a$var$keyToUtil = {
27
+ 'Select': (0, $4350df3e19e40e36$export$d1859707465446a9),
28
+ 'Table': (0, $6d1e4cf220920c37$export$4c6a9d6ae3b0086),
29
+ 'Menu': (0, $74b93f0617179929$export$f73bbc9212ed861e),
30
+ 'ComboBox': (0, $dab75f394483aa9c$export$f97e14e96d71ab3b),
31
+ 'GridList': (0, $5ca9e9228f508f75$export$93a85aaed9fabd83)
32
+ };
33
+ let $0d71b4e9cc4df40a$var$defaultAdvanceTimer = async (waitTime)=>await new Promise((resolve)=>setTimeout(resolve, waitTime));
34
+ class $0d71b4e9cc4df40a$export$1f44aaf2ec115b54 {
35
+ createTester(patternName, opts) {
36
+ return new $0d71b4e9cc4df40a$var$keyToUtil[patternName]({
37
+ ...opts,
38
+ user: this.user,
39
+ interactionType: this.interactionType,
40
+ advanceTimer: this.advanceTimer
41
+ });
42
+ }
43
+ constructor(opts = {}){
44
+ let { interactionType: interactionType, advanceTimer: advanceTimer } = opts;
45
+ this.user = (0, $gmkhN$testinglibraryuserevent).setup({
46
+ delay: null,
47
+ pointerMap: $50ecfb18069ec897$export$7dbde2c4caaa8d35
48
+ });
49
+ this.interactionType = interactionType;
50
+ this.advanceTimer = advanceTimer || $0d71b4e9cc4df40a$var$defaultAdvanceTimer;
51
+ }
52
+ }
53
+
54
+
55
+ export {$0d71b4e9cc4df40a$export$1f44aaf2ec115b54 as User};
56
+ //# sourceMappingURL=user.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;;;;;;;AAyBD,IAAI,kCAAY;IAAC,UAAU,CAAA,GAAA,yCAAW;IAAG,SAAS,CAAA,GAAA,wCAAU;IAAG,QAAQ,CAAA,GAAA,yCAAS;IAAG,YAAY,CAAA,GAAA,yCAAa;IAAG,YAAY,CAAA,GAAA,yCAAa;AAAC;AAoBzI,IAAI,4CAAsB,OAAO,WAAiC,MAAM,IAAI,QAAQ,CAAC,UAAY,WAAW,SAAS;AAE9G,MAAM;IAYX,aAAqC,WAAc,EAAE,IAA2B,EAAiB;QAC/F,OAAO,IAAI,AAAC,+BAAU,CAAC,YAAY,CAAC;YAAC,GAAG,IAAI;YAAE,MAAM,IAAI,CAAC,IAAI;YAAE,iBAAiB,IAAI,CAAC,eAAe;YAAE,cAAc,IAAI,CAAC,YAAY;QAAA;IACvI;IATA,YAAY,OAAiB,CAAC,CAAC,CAAE;QAC/B,IAAI,mBAAC,eAAe,gBAAE,YAAY,EAAC,GAAG;QACtC,IAAI,CAAC,IAAI,GAAG,CAAA,GAAA,8BAAQ,EAAE,KAAK,CAAC;YAAC,OAAO;wBAAM;QAAU;QACpD,IAAI,CAAC,eAAe,GAAG;QACvB,IAAI,CAAC,YAAY,GAAG,gBAAgB;IACtC;AAKF","sources":["packages/@react-aria/test-utils/src/user.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 {ComboBoxOptions, ComboBoxTester} from './combobox';\nimport {GridListOptions, GridListTester} from './gridlist';\nimport {MenuOptions, MenuTester} from './menu';\nimport {pointerMap} from './';\nimport {SelectOptions, SelectTester} from './select';\nimport {TableOptions, TableTester} from './table';\nimport userEvent from '@testing-library/user-event';\n\n// https://github.com/testing-library/dom-testing-library/issues/939#issuecomment-830771708 is an interesting way of allowing users to configure the timers\n// curent way is like https://testing-library.com/docs/user-event/options/#advancetimers,\nexport interface UserOpts {\n interactionType?: 'mouse' | 'touch' | 'keyboard',\n // If using fake timers user should provide something like (time) => jest.advanceTimersByTime(time))}\n // A real timer user would pass async () => await new Promise((resolve) => setTimeout(resolve, waitTime))\n // Time is in ms.\n advanceTimer?: (time?: number) => void | Promise<unknown>\n}\n\nexport interface BaseTesterOpts {\n // The base element for the given tester (e.g. the table, menu trigger, etc)\n root: HTMLElement\n}\n\nlet keyToUtil = {'Select': SelectTester, 'Table': TableTester, 'Menu': MenuTester, 'ComboBox': ComboBoxTester, 'GridList': GridListTester} as const;\nexport type PatternNames = keyof typeof keyToUtil;\n\n// Conditional type: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html\ntype ObjectType<T> =\n T extends 'Select' ? SelectTester :\n T extends 'Table' ? TableTester :\n T extends 'Menu' ? MenuTester :\n T extends 'ComboBox' ? ComboBoxTester :\n T extends 'GridList' ? GridListTester :\n never;\n\ntype ObjectOptionsTypes<T> =\n T extends 'Select' ? SelectOptions :\n T extends 'Table' ? TableOptions :\n T extends 'Menu' ? MenuOptions :\n T extends 'ComboBox' ? ComboBoxOptions :\n T extends 'GridList' ? GridListOptions :\n never;\n\nlet defaultAdvanceTimer = async (waitTime: number | undefined) => await new Promise((resolve) => setTimeout(resolve, waitTime));\n\nexport class User {\n user;\n interactionType: UserOpts['interactionType'];\n advanceTimer: UserOpts['advanceTimer'];\n\n constructor(opts: UserOpts = {}) {\n let {interactionType, advanceTimer} = opts;\n this.user = userEvent.setup({delay: null, pointerMap});\n this.interactionType = interactionType;\n this.advanceTimer = advanceTimer || defaultAdvanceTimer;\n }\n\n createTester<T extends PatternNames>(patternName: T, opts: ObjectOptionsTypes<T>): ObjectType<T> {\n return new (keyToUtil)[patternName]({...opts, user: this.user, interactionType: this.interactionType, advanceTimer: this.advanceTimer}) as ObjectType<T>;\n }\n}\n"],"names":[],"version":3,"file":"user.module.js.map"}
@@ -6,36 +6,36 @@ function $parcel$export(e, n, v, s) {
6
6
  $parcel$export(module.exports, "pointerMap", () => $0e568517a25183a7$export$7dbde2c4caaa8d35);
7
7
  let $0e568517a25183a7$export$7dbde2c4caaa8d35 = [
8
8
  {
9
- name: "MouseLeft",
10
- pointerType: "mouse",
11
- button: "primary",
9
+ name: 'MouseLeft',
10
+ pointerType: 'mouse',
11
+ button: 'primary',
12
12
  height: 1,
13
13
  width: 1,
14
14
  pressure: 0.5
15
15
  },
16
16
  {
17
- name: "MouseRight",
18
- pointerType: "mouse",
19
- button: "secondary"
17
+ name: 'MouseRight',
18
+ pointerType: 'mouse',
19
+ button: 'secondary'
20
20
  },
21
21
  {
22
- name: "MouseMiddle",
23
- pointerType: "mouse",
24
- button: "auxiliary"
22
+ name: 'MouseMiddle',
23
+ pointerType: 'mouse',
24
+ button: 'auxiliary'
25
25
  },
26
26
  {
27
- name: "TouchA",
28
- pointerType: "touch",
27
+ name: 'TouchA',
28
+ pointerType: 'touch',
29
29
  height: 1,
30
30
  width: 1
31
31
  },
32
32
  {
33
- name: "TouchB",
34
- pointerType: "touch"
33
+ name: 'TouchB',
34
+ pointerType: 'touch'
35
35
  },
36
36
  {
37
- name: "TouchC",
38
- pointerType: "touch"
37
+ name: 'TouchC',
38
+ pointerType: 'touch'
39
39
  }
40
40
  ];
41
41
 
@@ -1,38 +1,38 @@
1
1
  let $50ecfb18069ec897$export$7dbde2c4caaa8d35 = [
2
2
  {
3
- name: "MouseLeft",
4
- pointerType: "mouse",
5
- button: "primary",
3
+ name: 'MouseLeft',
4
+ pointerType: 'mouse',
5
+ button: 'primary',
6
6
  height: 1,
7
7
  width: 1,
8
8
  pressure: 0.5
9
9
  },
10
10
  {
11
- name: "MouseRight",
12
- pointerType: "mouse",
13
- button: "secondary"
11
+ name: 'MouseRight',
12
+ pointerType: 'mouse',
13
+ button: 'secondary'
14
14
  },
15
15
  {
16
- name: "MouseMiddle",
17
- pointerType: "mouse",
18
- button: "auxiliary"
16
+ name: 'MouseMiddle',
17
+ pointerType: 'mouse',
18
+ button: 'auxiliary'
19
19
  },
20
20
  {
21
- name: "TouchA",
22
- pointerType: "touch",
21
+ name: 'TouchA',
22
+ pointerType: 'touch',
23
23
  height: 1,
24
24
  width: 1
25
25
  },
26
26
  {
27
- name: "TouchB",
28
- pointerType: "touch"
27
+ name: 'TouchB',
28
+ pointerType: 'touch'
29
29
  },
30
30
  {
31
- name: "TouchC",
32
- pointerType: "touch"
31
+ name: 'TouchC',
32
+ pointerType: 'touch'
33
33
  }
34
34
  ];
35
35
 
36
36
 
37
37
  export {$50ecfb18069ec897$export$7dbde2c4caaa8d35 as pointerMap};
38
- //# sourceMappingURL=userEventMaps.mjs.map
38
+ //# sourceMappingURL=userEventMaps.module.js.map
@@ -1,35 +1,35 @@
1
1
  let $50ecfb18069ec897$export$7dbde2c4caaa8d35 = [
2
2
  {
3
- name: "MouseLeft",
4
- pointerType: "mouse",
5
- button: "primary",
3
+ name: 'MouseLeft',
4
+ pointerType: 'mouse',
5
+ button: 'primary',
6
6
  height: 1,
7
7
  width: 1,
8
8
  pressure: 0.5
9
9
  },
10
10
  {
11
- name: "MouseRight",
12
- pointerType: "mouse",
13
- button: "secondary"
11
+ name: 'MouseRight',
12
+ pointerType: 'mouse',
13
+ button: 'secondary'
14
14
  },
15
15
  {
16
- name: "MouseMiddle",
17
- pointerType: "mouse",
18
- button: "auxiliary"
16
+ name: 'MouseMiddle',
17
+ pointerType: 'mouse',
18
+ button: 'auxiliary'
19
19
  },
20
20
  {
21
- name: "TouchA",
22
- pointerType: "touch",
21
+ name: 'TouchA',
22
+ pointerType: 'touch',
23
23
  height: 1,
24
24
  width: 1
25
25
  },
26
26
  {
27
- name: "TouchB",
28
- pointerType: "touch"
27
+ name: 'TouchB',
28
+ pointerType: 'touch'
29
29
  },
30
30
  {
31
- name: "TouchC",
32
- pointerType: "touch"
31
+ name: 'TouchC',
32
+ pointerType: 'touch'
33
33
  }
34
34
  ];
35
35
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-aria/test-utils",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.2",
4
4
  "description": "Testing utils for react-aria patterns",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/main.js",
@@ -25,13 +25,12 @@
25
25
  "@swc/helpers": "^0.5.0"
26
26
  },
27
27
  "peerDependencies": {
28
- "@testing-library/react": "^13.0.0 || ^14.0.0",
28
+ "@testing-library/react": "^15.0.7",
29
29
  "@testing-library/user-event": "^13.0.0 || ^14.0.0",
30
- "jest": "^27.0.0",
31
- "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
30
+ "jest": "^29.5.0",
31
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"
35
- },
36
- "gitHead": "f645f29edc1322153fd60af4640cbcab1d992dbd"
37
- }
35
+ }
36
+ }
@@ -0,0 +1,188 @@
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, waitFor, within} from '@testing-library/react';
14
+ import {BaseTesterOpts, UserOpts} from './user';
15
+
16
+ export interface ComboBoxOptions extends UserOpts, BaseTesterOpts {
17
+ user: any,
18
+ trigger?: HTMLElement
19
+ }
20
+
21
+ export class ComboBoxTester {
22
+ private user;
23
+ private _interactionType: UserOpts['interactionType'];
24
+ private _combobox: HTMLElement;
25
+ private _trigger: HTMLElement | undefined;
26
+
27
+ constructor(opts: ComboBoxOptions) {
28
+ let {root, trigger, user, interactionType} = opts;
29
+ this.user = user;
30
+ this._interactionType = interactionType || 'mouse';
31
+
32
+ // 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
33
+ // query their combobox/combobox wrapper (in the case of RSP) which they then pass to thhis
34
+ this._combobox = root;
35
+ let combobox = within(root).queryByRole('combobox');
36
+ if (combobox) {
37
+ this._combobox = combobox;
38
+ }
39
+
40
+ // 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)
41
+ if (trigger) {
42
+ this._trigger = trigger;
43
+ } else {
44
+ let trigger = within(root).queryByRole('button', {hidden: true});
45
+ if (trigger) {
46
+ this._trigger = trigger;
47
+ } else {
48
+ // For cases like https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/ where the combobox
49
+ // is also the trigger button
50
+ this._trigger = this._combobox;
51
+ }
52
+ }
53
+ }
54
+
55
+ setInteractionType = (type: UserOpts['interactionType']) => {
56
+ this._interactionType = type;
57
+ };
58
+
59
+ open = async (opts: {triggerBehavior?: 'focus' | 'manual', interactionType?: UserOpts['interactionType']} = {}) => {
60
+ let {triggerBehavior = 'manual', interactionType = this._interactionType} = opts;
61
+ let trigger = this.trigger;
62
+ let combobox = this.combobox;
63
+ let isDisabled = trigger!.hasAttribute('disabled');
64
+
65
+ if (interactionType === 'mouse') {
66
+ if (triggerBehavior === 'focus') {
67
+ await this.user.click(combobox);
68
+ } else {
69
+ await this.user.click(trigger);
70
+ }
71
+ } else if (interactionType === 'keyboard' && this._trigger != null) {
72
+ act(() => this._trigger!.focus());
73
+ if (triggerBehavior !== 'focus') {
74
+ await this.user.keyboard('{ArrowDown}');
75
+ }
76
+ } else if (interactionType === 'touch') {
77
+ if (triggerBehavior === 'focus') {
78
+ await this.user.pointer({target: combobox, keys: '[TouchA]'});
79
+ } else {
80
+ await this.user.pointer({target: trigger, keys: '[TouchA]'});
81
+ }
82
+ }
83
+
84
+ await waitFor(() => {
85
+ if (!isDisabled && combobox.getAttribute('aria-controls') == null) {
86
+ throw new Error('No aria-controls found on combobox trigger element.');
87
+ } else {
88
+ return true;
89
+ }
90
+ });
91
+ let listBoxId = combobox.getAttribute('aria-controls');
92
+ await waitFor(() => {
93
+ if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {
94
+ throw new Error(`Listbox with id of ${listBoxId} not found in document.`);
95
+ } else {
96
+ return true;
97
+ }
98
+ });
99
+ };
100
+
101
+ selectOption = async (opts: {option?: HTMLElement, optionText?: string, triggerBehavior?: 'focus' | 'manual', interactionType?: UserOpts['interactionType']} = {}) => {
102
+ let {optionText, option, triggerBehavior, interactionType = this._interactionType} = opts;
103
+ if (!this.combobox.getAttribute('aria-controls')) {
104
+ await this.open({triggerBehavior});
105
+ }
106
+
107
+ let listbox = this.listbox;
108
+ if (listbox) {
109
+ if (!option && optionText) {
110
+ option = within(listbox).getByText(optionText);
111
+ }
112
+
113
+ // TODO: keyboard method of selecting the the option is a bit tricky unless I simply simulate the user pressing the down arrow
114
+ // the required amount of times to reach the option. For now just click the option even in keyboard mode
115
+ if (interactionType === 'mouse' || interactionType === 'keyboard') {
116
+ await this.user.click(option);
117
+ } else {
118
+ await this.user.pointer({target: option, keys: '[TouchA]'});
119
+ }
120
+
121
+ if (option && option.getAttribute('href') == null) {
122
+ await waitFor(() => {
123
+ if (document.contains(listbox)) {
124
+ throw new Error('Expected listbox element to not be in the document after selecting an option');
125
+ } else {
126
+ return true;
127
+ }
128
+ });
129
+ }
130
+ } else {
131
+ throw new Error("Attempted to select a option in the combobox, but the listbox wasn't found.");
132
+ }
133
+ };
134
+
135
+ close = async () => {
136
+ let listbox = this.listbox;
137
+ if (listbox) {
138
+ act(() => this.combobox.focus());
139
+ await this.user.keyboard('[Escape]');
140
+
141
+ await waitFor(() => {
142
+ if (document.contains(listbox)) {
143
+ throw new Error('Expected listbox element to not be in the document after selecting an option');
144
+ } else {
145
+ return true;
146
+ }
147
+ });
148
+ }
149
+ };
150
+
151
+ get combobox() {
152
+ return this._combobox;
153
+ }
154
+
155
+ get trigger() {
156
+ return this._trigger;
157
+ }
158
+
159
+ get listbox() {
160
+ let listBoxId = this.combobox.getAttribute('aria-controls');
161
+ return listBoxId ? document.getElementById(listBoxId) || undefined : undefined;
162
+ }
163
+
164
+ options = (opts: {element?: HTMLElement} = {}): HTMLElement[] | never[] => {
165
+ let {element} = opts;
166
+ element = element || this.listbox;
167
+ let options = [];
168
+ if (element) {
169
+ options = within(element).queryAllByRole('option');
170
+ }
171
+
172
+ return options;
173
+ };
174
+
175
+ get sections() {
176
+ let listbox = this.listbox;
177
+ if (listbox) {
178
+ return within(listbox).queryAllByRole('group');
179
+ } else {
180
+ return [];
181
+ }
182
+ }
183
+
184
+ get focusedOption() {
185
+ let focusedOptionId = this.combobox.getAttribute('aria-activedescendant');
186
+ return focusedOptionId ? document.getElementById(focusedOptionId) : undefined;
187
+ }
188
+ }
package/src/events.ts CHANGED
@@ -11,18 +11,38 @@
11
11
  */
12
12
 
13
13
  import {act, fireEvent} from '@testing-library/react';
14
+ import {UserOpts} from './user';
14
15
 
15
16
  export const DEFAULT_LONG_PRESS_TIME = 500;
16
17
 
17
18
  /**
18
19
  * Simulates a "long press" event on a element.
19
- * @param element - Element to long press.
20
- * @param opts - Options to pass to the simulated event. See https://testing-library.com/docs/dom-testing-library/api-events/#fireevent for more info.
20
+ * @param opts - Options for the long press.
21
+ * @param opts.element - Element to long press.
22
+ * @param opts.advanceTimer - Function that when called advances the timers in your test suite by a specific amount of time(ms).
23
+ * @param opts.pointeropts - Options to pass to the simulated event. Defaults to mouse. See https://testing-library.com/docs/dom-testing-library/api-events/#fireevent for more info.
21
24
  */
22
- export function triggerLongPress(element: HTMLElement, opts = {}): void {
23
- fireEvent.pointerDown(element, {pointerType: 'touch', ...opts});
24
- act(() => {
25
- jest.advanceTimersByTime(DEFAULT_LONG_PRESS_TIME);
26
- });
27
- fireEvent.pointerUp(element, {pointerType: 'touch', ...opts});
25
+ export async function triggerLongPress(opts: {element: HTMLElement, advanceTimer: (time?: number) => void | Promise<unknown>, pointerOpts?: {}}) {
26
+ // TODO: note that this only works if the code from installPointerEvent is called somewhere in the test BEFORE the
27
+ // render. Perhaps we should rely on the user setting that up since I'm not sure there is a great way to set that up here in the
28
+ // util before first render. Will need to document it well
29
+ let {element, advanceTimer, pointerOpts = {}} = opts;
30
+ await fireEvent.pointerDown(element, {pointerType: 'mouse', ...pointerOpts});
31
+ await act(async () => await advanceTimer(DEFAULT_LONG_PRESS_TIME));
32
+ await fireEvent.pointerUp(element, {pointerType: 'mouse', ...pointerOpts});
33
+ }
34
+
35
+
36
+ export async function pressElement(user, element: HTMLElement, interactionType: UserOpts['interactionType']) {
37
+ if (interactionType === 'mouse') {
38
+ await user.click(element);
39
+ } else if (interactionType === 'keyboard') {
40
+ // TODO: For the keyboard flow, I wonder if it would be reasonable to just do fireEvent directly on the obtained row node or if we should
41
+ // stick to simulting an actual user's keyboard operations as closely as possible
42
+ // There are problems when using this approach though, actions like trying to trigger the select all checkbox and stuff behave oddly.
43
+ act(() => element.focus());
44
+ await user.keyboard('[Space]');
45
+ } else if (interactionType === 'touch') {
46
+ await user.pointer({target: element, keys: '[TouchA]'});
47
+ }
28
48
  }
@@ -0,0 +1,116 @@
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 {BaseTesterOpts, UserOpts} from './user';
15
+ import {pressElement} from './events';
16
+
17
+ export interface GridListOptions extends UserOpts, BaseTesterOpts {
18
+ user: any
19
+ }
20
+ export class GridListTester {
21
+ private user;
22
+ private _interactionType: UserOpts['interactionType'];
23
+ private _gridlist: HTMLElement;
24
+
25
+
26
+ constructor(opts: GridListOptions) {
27
+ let {root, user, interactionType} = opts;
28
+ this.user = user;
29
+ this._interactionType = interactionType || 'mouse';
30
+ this._gridlist = root;
31
+ }
32
+
33
+ setInteractionType = (type: UserOpts['interactionType']) => {
34
+ this._interactionType = type;
35
+ };
36
+
37
+ // TODO: support long press? This is also pretty much the same as table's toggleRowSelection so maybe can share
38
+ // For now, don't include long press, see if people need it or if we should just expose long press as a separate util if it isn't very common
39
+ // If the current way of passing in the user specified advance timers is ok, then I'd be find including long press
40
+ // Maybe also support an option to force the click to happen on a specific part of the element (checkbox or row). That way
41
+ // the user can test a specific type of interaction?
42
+ toggleRowSelection = async (opts: {index?: number, text?: string, interactionType?: UserOpts['interactionType']} = {}) => {
43
+ let {index, text, interactionType = this._interactionType} = opts;
44
+
45
+ let row = this.findRow({index, text});
46
+ let rowCheckbox = within(row).queryByRole('checkbox');
47
+ if (rowCheckbox) {
48
+ await pressElement(this.user, rowCheckbox, interactionType);
49
+ } else {
50
+ let cell = within(row).getAllByRole('gridcell')[0];
51
+ await pressElement(this.user, cell, interactionType);
52
+ }
53
+ };
54
+
55
+ // TODO: pretty much the same as table except it uses this.gridlist. Make common between the two by accepting an option for
56
+ // an element?
57
+ findRow = (opts: {index?: number, text?: string}) => {
58
+ let {
59
+ index,
60
+ text
61
+ } = opts;
62
+
63
+ let row;
64
+ if (index != null) {
65
+ row = this.rows[index];
66
+ } else if (text != null) {
67
+ row = within(this?.gridlist).getByText(text);
68
+ while (row && row.getAttribute('role') !== 'row') {
69
+ row = row.parentElement;
70
+ }
71
+ }
72
+
73
+ return row;
74
+ };
75
+
76
+ // TODO: There is a more difficult use case where the row has/behaves as link, don't think we have a good way to determine that unless the
77
+ // user specificlly tells us
78
+ triggerRowAction = async (opts: {index?: number, text?: string, needsDoubleClick?: boolean, interactionType?: UserOpts['interactionType']}) => {
79
+ let {
80
+ index,
81
+ text,
82
+ needsDoubleClick,
83
+ interactionType = this._interactionType
84
+ } = opts;
85
+
86
+ let row = this.findRow({index, text});
87
+ if (row) {
88
+ if (needsDoubleClick) {
89
+ await this.user.dblClick(row);
90
+ } else if (interactionType === 'keyboard') {
91
+ act(() => row.focus());
92
+ await this.user.keyboard('[Enter]');
93
+ } else {
94
+ await pressElement(this.user, row, interactionType);
95
+ }
96
+ }
97
+ };
98
+
99
+ // TODO: do we really need this getter? Theoretically the user already has the reference to the gridlist
100
+ get gridlist() {
101
+ return this._gridlist;
102
+ }
103
+
104
+ get rows() {
105
+ return within(this?.gridlist).queryAllByRole('row');
106
+ }
107
+
108
+ get selectedRows() {
109
+ return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');
110
+ }
111
+
112
+ cells = (opts: {element?: HTMLElement} = {}) => {
113
+ let {element} = opts;
114
+ return within(element || this.gridlist).queryAllByRole('gridcell');
115
+ };
116
+ }
package/src/index.ts CHANGED
@@ -10,6 +10,9 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export * from './events';
14
- export * from './testSetup';
15
- export * from './userEventMaps';
13
+ export {triggerLongPress} from './events';
14
+ export {installMouseEvent, installPointerEvent} from './testSetup';
15
+ export {pointerMap} from './userEventMaps';
16
+ export {User} from './user';
17
+
18
+ export type {UserOpts} from './user';