@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/dist/select.main.js
CHANGED
|
@@ -18,87 +18,133 @@ $parcel$export(module.exports, "SelectTester", () => $b97d5a42df7c75b2$export$d1
|
|
|
18
18
|
* governing permissions and limitations under the License.
|
|
19
19
|
*/
|
|
20
20
|
class $b97d5a42df7c75b2$export$d1859707465446a9 {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Set the interaction type used by the select tester.
|
|
23
|
+
*/ setInteractionType(type) {
|
|
24
|
+
this._interactionType = type;
|
|
23
25
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Opens the select. Defaults to using the interaction type set on the select tester.
|
|
28
|
+
*/ async open(opts = {}) {
|
|
29
|
+
let { interactionType: interactionType = this._interactionType } = opts;
|
|
30
|
+
let trigger = this.trigger;
|
|
31
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
32
|
+
if (interactionType === 'mouse') await this.user.click(this._trigger);
|
|
33
|
+
else if (interactionType === 'keyboard') {
|
|
34
|
+
(0, $bBwW0$testinglibraryreact.act)(()=>trigger.focus());
|
|
35
|
+
await this.user.keyboard('[Enter]');
|
|
36
|
+
} else if (interactionType === 'touch') await this.user.pointer({
|
|
37
|
+
target: this._trigger,
|
|
38
|
+
keys: '[TouchA]'
|
|
39
|
+
});
|
|
40
|
+
await (0, $bBwW0$testinglibraryreact.waitFor)(()=>{
|
|
41
|
+
if (!isDisabled && trigger.getAttribute('aria-controls') == null) throw new Error('No aria-controls found on select element trigger.');
|
|
42
|
+
else return true;
|
|
43
|
+
});
|
|
44
|
+
let listBoxId = trigger.getAttribute('aria-controls');
|
|
45
|
+
await (0, $bBwW0$testinglibraryreact.waitFor)(()=>{
|
|
46
|
+
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
47
|
+
else return true;
|
|
48
|
+
});
|
|
27
49
|
}
|
|
28
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Closes the select.
|
|
52
|
+
*/ async close() {
|
|
29
53
|
let listbox = this.listbox;
|
|
30
|
-
|
|
54
|
+
if (listbox) {
|
|
55
|
+
(0, $bBwW0$testinglibraryreact.act)(()=>listbox.focus());
|
|
56
|
+
await this.user.keyboard('[Escape]');
|
|
57
|
+
}
|
|
58
|
+
await (0, $bBwW0$testinglibraryreact.waitFor)(()=>{
|
|
59
|
+
if (document.activeElement !== this._trigger) throw new Error(`Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`);
|
|
60
|
+
else return true;
|
|
61
|
+
});
|
|
62
|
+
if (listbox && document.contains(listbox)) throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');
|
|
31
63
|
}
|
|
32
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Returns a option matching the specified index or text content.
|
|
66
|
+
*/ findOption(opts) {
|
|
67
|
+
let { optionIndexOrText: optionIndexOrText } = opts;
|
|
68
|
+
let option;
|
|
69
|
+
let options = this.options();
|
|
33
70
|
let listbox = this.listbox;
|
|
34
|
-
|
|
71
|
+
if (typeof optionIndexOrText === 'number') option = options[optionIndexOrText];
|
|
72
|
+
else if (typeof optionIndexOrText === 'string' && listbox != null) option = (0, $bBwW0$testinglibraryreact.within)(listbox).getByText(optionIndexOrText).closest('[role=option]');
|
|
73
|
+
return option;
|
|
35
74
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
75
|
+
async keyboardNavigateToOption(opts) {
|
|
76
|
+
let { option: option } = opts;
|
|
77
|
+
let options = this.options();
|
|
78
|
+
let targetIndex = options.indexOf(option);
|
|
79
|
+
if (targetIndex === -1) throw new Error('Option provided is not in the listbox');
|
|
80
|
+
if (document.activeElement === this.listbox) await this.user.keyboard('[ArrowDown]');
|
|
81
|
+
let currIndex = options.indexOf(document.activeElement);
|
|
82
|
+
if (currIndex === -1) throw new Error('ActiveElement is not in the listbox');
|
|
83
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
84
|
+
for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Selects the desired select option. Defaults to using the interaction type set on the select tester. If necessary, will open the select dropdown beforehand.
|
|
88
|
+
* The desired option can be targeted via the option's node, the option's text, or the option's index.
|
|
89
|
+
*/ async selectOption(opts) {
|
|
90
|
+
let { option: option, interactionType: interactionType = this._interactionType } = opts || {};
|
|
91
|
+
let trigger = this.trigger;
|
|
92
|
+
if (!trigger.getAttribute('aria-controls')) await this.open();
|
|
93
|
+
let listbox = this.listbox;
|
|
94
|
+
if (!listbox) throw new Error('Select\'s listbox not found.');
|
|
95
|
+
if (listbox) {
|
|
96
|
+
if (typeof option === 'string' || typeof option === 'number') option = this.findOption({
|
|
97
|
+
optionIndexOrText: option
|
|
98
|
+
});
|
|
99
|
+
if (!option) throw new Error('Target option not found in the listbox.');
|
|
100
|
+
if (interactionType === 'keyboard') {
|
|
101
|
+
if ((option === null || option === void 0 ? void 0 : option.getAttribute('aria-disabled')) === 'true') return;
|
|
102
|
+
if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) (0, $bBwW0$testinglibraryreact.act)(()=>listbox.focus());
|
|
103
|
+
await this.keyboardNavigateToOption({
|
|
104
|
+
option: option
|
|
105
|
+
});
|
|
47
106
|
await this.user.keyboard('[Enter]');
|
|
48
|
-
} else if (
|
|
49
|
-
|
|
107
|
+
} else // TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)
|
|
108
|
+
if (interactionType === 'mouse') await this.user.click(option);
|
|
109
|
+
else await this.user.pointer({
|
|
110
|
+
target: option,
|
|
50
111
|
keys: '[TouchA]'
|
|
51
112
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
let listBoxId = trigger.getAttribute('aria-controls');
|
|
57
|
-
await (0, $bBwW0$testinglibraryreact.waitFor)(()=>{
|
|
58
|
-
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
59
|
-
else return true;
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
this.selectOption = async (opts)=>{
|
|
63
|
-
let { optionText: optionText, interactionType: interactionType = this._interactionType } = opts || {};
|
|
64
|
-
let trigger = this.trigger;
|
|
65
|
-
if (!trigger.getAttribute('aria-controls')) await this.open();
|
|
66
|
-
let listbox = this.listbox;
|
|
67
|
-
if (listbox) {
|
|
68
|
-
let option = (0, $bBwW0$testinglibraryreact.within)(listbox).getByText(optionText);
|
|
69
|
-
if (interactionType === 'keyboard') {
|
|
70
|
-
if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) (0, $bBwW0$testinglibraryreact.act)(()=>listbox.focus());
|
|
71
|
-
// TODO: this simulates typeahead, do we want to add a helper util for that? Not sure if users would really need that for
|
|
72
|
-
// their test
|
|
73
|
-
await this.user.keyboard(optionText);
|
|
74
|
-
await this.user.keyboard('[Enter]');
|
|
75
|
-
} else // TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)
|
|
76
|
-
if (interactionType === 'mouse') await this.user.click(option);
|
|
77
|
-
else await this.user.pointer({
|
|
78
|
-
target: option,
|
|
79
|
-
keys: '[TouchA]'
|
|
113
|
+
if ((option === null || option === void 0 ? void 0 : option.getAttribute('href')) == null) {
|
|
114
|
+
await (0, $bBwW0$testinglibraryreact.waitFor)(()=>{
|
|
115
|
+
if (document.activeElement !== this._trigger) throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);
|
|
116
|
+
else return true;
|
|
80
117
|
});
|
|
81
|
-
if (
|
|
82
|
-
await (0, $bBwW0$testinglibraryreact.waitFor)(()=>{
|
|
83
|
-
if (document.activeElement !== this._trigger) throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);
|
|
84
|
-
else return true;
|
|
85
|
-
});
|
|
86
|
-
if (document.contains(listbox)) throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
this.close = async ()=>{
|
|
91
|
-
let listbox = this.listbox;
|
|
92
|
-
if (listbox) {
|
|
93
|
-
(0, $bBwW0$testinglibraryreact.act)(()=>listbox.focus());
|
|
94
|
-
await this.user.keyboard('[Escape]');
|
|
118
|
+
if (document.contains(listbox)) throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
95
119
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns the select's options if present. Can be filtered to a subsection of the listbox if provided via `element`.
|
|
124
|
+
*/ options(opts = {}) {
|
|
125
|
+
let { element: element = this.listbox } = opts;
|
|
126
|
+
let options = [];
|
|
127
|
+
if (element) options = (0, $bBwW0$testinglibraryreact.within)(element).queryAllByRole('option');
|
|
128
|
+
return options;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Returns the select's trigger.
|
|
132
|
+
*/ get trigger() {
|
|
133
|
+
return this._trigger;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns the select's listbox if present.
|
|
137
|
+
*/ get listbox() {
|
|
138
|
+
let listBoxId = this.trigger.getAttribute('aria-controls');
|
|
139
|
+
return listBoxId ? document.getElementById(listBoxId) : null;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Returns the select's sections if present.
|
|
143
|
+
*/ get sections() {
|
|
144
|
+
let listbox = this.listbox;
|
|
145
|
+
return listbox ? (0, $bBwW0$testinglibraryreact.within)(listbox).queryAllByRole('group') : [];
|
|
146
|
+
}
|
|
147
|
+
constructor(opts){
|
|
102
148
|
let { root: root, user: user, interactionType: interactionType } = opts;
|
|
103
149
|
this.user = user;
|
|
104
150
|
this._interactionType = interactionType || 'mouse';
|
package/dist/select.main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AASM,MAAM;IAwHX,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,QAAQ;IACtB;IAEA,IAAI,UAAU;QACZ,IAAI,YAAY,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC;QAC1C,OAAO,YAAY,SAAS,cAAc,CAAC,aAAa;IAC1D;IAEA,IAAI,UAAU;QACZ,IAAI,UAAU,IAAI,CAAC,OAAO;QAC1B,OAAO,UAAU,CAAA,GAAA,iCAAK,EAAE,SAAS,cAAc,CAAC,YAAY,EAAE;IAChE;IAEA,IAAI,WAAW;QACb,IAAI,UAAU,IAAI,CAAC,OAAO;QAC1B,OAAO,UAAU,CAAA,GAAA,iCAAK,EAAE,SAAS,cAAc,CAAC,WAAW,EAAE;IAC/D;IApIA,YAAY,IAAmB,CAAE;aAYjC,qBAAqB,CAAC;YACpB,IAAI,CAAC,gBAAgB,GAAG;QAC1B;aAEA,OAAO,OAAO,OAAwD,CAAC,CAAC;YACtE,IAAI,mBACF,kBAAkB,IAAI,CAAC,gBAAgB,EACxC,GAAG;YACJ,IAAI,UAAU,IAAI,CAAC,OAAO;YAC1B,IAAI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C7C,IAAI,iBAAiB,MACnB,gBAAgB;QAElB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA2HF","sources":["packages/@react-aria/test-utils/src/select.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {act, waitFor, within} from '@testing-library/react';\nimport {BaseTesterOpts, UserOpts} from './user';\n\nexport interface SelectOptions extends UserOpts, BaseTesterOpts {\n // TODO: I think the type grabbed from the testing library dist for UserEvent is breaking the build, will need to figure out a better place to grab from\n user?: any\n}\nexport class SelectTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _trigger: HTMLElement;\n\n constructor(opts: SelectOptions) {\n let {root, user, interactionType} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n // Handle case where the wrapper element is provided rather than the Select's button (aka RAC)\n let triggerButton = within(root).queryByRole('button');\n if (triggerButton == null) {\n triggerButton = root;\n }\n this._trigger = triggerButton;\n }\n\n setInteractionType = (type: UserOpts['interactionType']) => {\n this._interactionType = type;\n };\n\n open = async (opts: {interactionType?: UserOpts['interactionType']} = {}) => {\n let {\n interactionType = this._interactionType\n } = opts;\n let trigger = this.trigger;\n let isDisabled = trigger.hasAttribute('disabled');\n\n if (interactionType === 'mouse') {\n await this.user.click(this._trigger);\n } else if (interactionType === 'keyboard') {\n act(() => trigger.focus());\n await this.user.keyboard('[Enter]');\n } else if (interactionType === 'touch') {\n await this.user.pointer({target: this._trigger, keys: '[TouchA]'});\n }\n\n await waitFor(() => {\n if (!isDisabled && trigger.getAttribute('aria-controls') == null) {\n throw new Error('No aria-controls found on select element trigger.');\n } else {\n return true;\n }\n });\n let listBoxId = trigger.getAttribute('aria-controls');\n await waitFor(() => {\n if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {\n throw new Error(`ListBox with id of ${listBoxId} not found in document.`);\n } else {\n return true;\n }\n });\n };\n\n selectOption = async (opts: {optionText: string, interactionType?: UserOpts['interactionType']}) => {\n let {\n optionText,\n interactionType = this._interactionType\n } = opts || {};\n let trigger = this.trigger;\n if (!trigger.getAttribute('aria-controls')) {\n await this.open();\n }\n let listbox = this.listbox;\n if (listbox) {\n let option = within(listbox).getByText(optionText);\n if (interactionType === 'keyboard') {\n if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) {\n act(() => listbox.focus());\n }\n\n // TODO: this simulates typeahead, do we want to add a helper util for that? Not sure if users would really need that for\n // their test\n await this.user.keyboard(optionText);\n await this.user.keyboard('[Enter]');\n } else {\n // TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)\n if (interactionType === 'mouse') {\n await this.user.click(option);\n } else {\n await this.user.pointer({target: option, keys: '[TouchA]'});\n }\n }\n\n if (option.getAttribute('href') == null) {\n await waitFor(() => {\n if (document.activeElement !== this._trigger) {\n throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);\n } else {\n return true;\n }\n });\n\n if (document.contains(listbox)) {\n throw new Error('Expected select element listbox to not be in the document after selecting an option');\n }\n }\n }\n };\n\n close = async () => {\n let listbox = this.listbox;\n if (listbox) {\n act(() => listbox.focus());\n await this.user.keyboard('[Escape]');\n }\n\n await waitFor(() => {\n if (document.activeElement !== this._trigger) {\n throw new Error(`Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`);\n } else {\n return true;\n }\n });\n\n if (listbox && document.contains(listbox)) {\n throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');\n }\n };\n\n get trigger() {\n return this._trigger;\n }\n\n get listbox() {\n let listBoxId = this.trigger.getAttribute('aria-controls');\n return listBoxId ? document.getElementById(listBoxId) : undefined;\n }\n\n get options() {\n let listbox = this.listbox;\n return listbox ? within(listbox).queryAllByRole('option') : [];\n }\n\n get sections() {\n let listbox = this.listbox;\n return listbox ? within(listbox).queryAllByRole('group') : [];\n }\n}\n"],"names":[],"version":3,"file":"select.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAmBM,MAAM;IAgBX;;GAEC,GACD,mBAAmB,IAAiC,EAAE;QACpD,IAAI,CAAC,gBAAgB,GAAG;IAC1B;IAEA;;GAEC,GACD,MAAM,KAAK,OAAuB,CAAC,CAAC,EAAE;QACpC,IAAI,mBACF,kBAAkB,IAAI,CAAC,gBAAgB,EACxC,GAAG;QACJ,IAAI,UAAU,IAAI,CAAC,OAAO;QAC1B,IAAI,aAAa,QAAQ,YAAY,CAAC;QAEtC,IAAI,oBAAoB,SACtB,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ;aAC9B,IAAI,oBAAoB,YAAY;YACzC,CAAA,GAAA,8BAAE,EAAE,IAAM,QAAQ,KAAK;YACvB,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;QAC3B,OAAO,IAAI,oBAAoB,SAC7B,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;YAAC,QAAQ,IAAI,CAAC,QAAQ;YAAE,MAAM;QAAU;QAGlE,MAAM,CAAA,GAAA,kCAAM,EAAE;YACZ,IAAI,CAAC,cAAc,QAAQ,YAAY,CAAC,oBAAoB,MAC1D,MAAM,IAAI,MAAM;iBAEhB,OAAO;QAEX;QACA,IAAI,YAAY,QAAQ,YAAY,CAAC;QACrC,MAAM,CAAA,GAAA,kCAAM,EAAE;YACZ,IAAI,CAAC,cAAe,CAAA,CAAC,aAAa,SAAS,cAAc,CAAC,cAAc,IAAG,GACzE,MAAM,IAAI,MAAM,CAAC,mBAAmB,EAAE,UAAU,uBAAuB,CAAC;iBAExE,OAA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AAM,EAAE;oBACZ,IAAI,SAAS,aAAa,KAAK,IAAI,CAAC,QAAQ,EAC1C,MAAM,IAAI,MAAM,CAAC,yGAAyG,EAAE,SAAS,aAAa,EAAE;yBAEpJ,OAAO;gBAEX;gBAEA,IAAI,SAAS,QAAQ,CAAC,UACpB,MAAM,IAAI,MAAM;YAEpB;QACF;IACF;IAEA;;GAEC,GACD,QAAQ,OAAgC,CAAC,CAAC,EAAiB;QACzD,IAAI,WAAC,UAAU,IAAI,CAAC,OAAO,EAAC,GAAG;QAC/B,IAAI,UAAU,EAAE;QAChB,IAAI,SACF,UAAU,CAAA,GAAA,iCAAK,EAAE,SAAS,cAAc,CAAC;QAG3C,OAAO;IACT;IAEA;;GAEC,GACD,IAAI,UAAuB;QACzB,OAAO,IAAI,CAAC,QAAQ;IACtB;IAEA;;GAEC,GACD,IAAI,UAA8B;QAChC,IAAI,YAAY,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC;QAC1C,OAAO,YAAY,SAAS,cAAc,CAAC,aAAa;IAC1D;IAEA;;GAEC,GACD,IAAI,WAA0B;QAC5B,IAAI,UAAU,IAAI,CAAC,OAAO;QAC1B,OAAO,UAAU,CAAA,GAAA,iCAAK,EAAE,SAAS,cAAc,CAAC,WAAW,EAAE;IAC/D;IAvNA,YAAY,IAAsB,CAAE;QAClC,IAAI,QAAC,IAAI,QAAE,IAAI,mBAAE,eAAe,EAAC,GAAG;QACpC,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,gBAAgB,GAAG,mBAAmB;QAC3C,8FAA8F;QAC9F,IAAI,gBAAgB,CAAA,GAAA,iCAAK,EAAE,MAAM,WAAW,CAAC;QAC7C,IAAI,iBAAiB,MACnB,gBAAgB;QAElB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA8MF","sources":["packages/@react-aria/test-utils/src/select.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {act, waitFor, within} from '@testing-library/react';\nimport {SelectTesterOpts, UserOpts} from './types';\n\ninterface SelectOpenOpts {\n /**\n * What interaction type to use when opening the select. Defaults to the interaction type set on the tester.\n */\n interactionType?: UserOpts['interactionType']\n}\n\ninterface SelectTriggerOptionOpts extends SelectOpenOpts {\n /**\n * The index, text, or node of the option to select. Option nodes can be sourced via `options()`.\n */\n option: number | string | HTMLElement\n}\n\nexport class SelectTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _trigger: HTMLElement;\n\n constructor(opts: SelectTesterOpts) {\n let {root, user, interactionType} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n // Handle case where the wrapper element is provided rather than the Select's button (aka RAC)\n let triggerButton = within(root).queryByRole('button');\n if (triggerButton == null) {\n triggerButton = root;\n }\n this._trigger = triggerButton;\n }\n /**\n * Set the interaction type used by the select tester.\n */\n setInteractionType(type: UserOpts['interactionType']) {\n this._interactionType = type;\n }\n\n /**\n * Opens the select. Defaults to using the interaction type set on the select tester.\n */\n async open(opts: SelectOpenOpts = {}) {\n let {\n interactionType = this._interactionType\n } = opts;\n let trigger = this.trigger;\n let isDisabled = trigger.hasAttribute('disabled');\n\n if (interactionType === 'mouse') {\n await this.user.click(this._trigger);\n } else if (interactionType === 'keyboard') {\n act(() => trigger.focus());\n await this.user.keyboard('[Enter]');\n } else if (interactionType === 'touch') {\n await this.user.pointer({target: this._trigger, keys: '[TouchA]'});\n }\n\n await waitFor(() => {\n if (!isDisabled && trigger.getAttribute('aria-controls') == null) {\n throw new Error('No aria-controls found on select element trigger.');\n } else {\n return true;\n }\n });\n let listBoxId = trigger.getAttribute('aria-controls');\n await waitFor(() => {\n if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) {\n throw new Error(`ListBox with id of ${listBoxId} not found in document.`);\n } else {\n return true;\n }\n });\n }\n\n /**\n * Closes the select.\n */\n async close() {\n let listbox = this.listbox;\n if (listbox) {\n act(() => listbox.focus());\n await this.user.keyboard('[Escape]');\n }\n\n await waitFor(() => {\n if (document.activeElement !== this._trigger) {\n throw new Error(`Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`);\n } else {\n return true;\n }\n });\n\n if (listbox && document.contains(listbox)) {\n throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');\n }\n }\n\n /**\n * Returns a option matching the specified index or text content.\n */\n findOption(opts: {optionIndexOrText: number | string}): HTMLElement {\n let {\n optionIndexOrText\n } = opts;\n\n let option;\n let options = this.options();\n let listbox = this.listbox;\n\n if (typeof optionIndexOrText === 'number') {\n option = options[optionIndexOrText];\n } else if (typeof optionIndexOrText === 'string' && listbox != null) {\n option = (within(listbox!).getByText(optionIndexOrText).closest('[role=option]'))! as HTMLElement;\n }\n\n return option;\n }\n\n private async keyboardNavigateToOption(opts: {option: HTMLElement}) {\n let {option} = opts;\n let options = this.options();\n let targetIndex = options.indexOf(option);\n if (targetIndex === -1) {\n throw new Error('Option provided is not in the listbox');\n }\n if (document.activeElement === this.listbox) {\n await this.user.keyboard('[ArrowDown]');\n }\n let currIndex = options.indexOf(document.activeElement as HTMLElement);\n if (currIndex === -1) {\n throw new Error('ActiveElement is not in the listbox');\n }\n let direction = targetIndex > currIndex ? 'down' : 'up';\n\n for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {\n await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);\n }\n };\n\n /**\n * Selects the desired select option. Defaults to using the interaction type set on the select tester. If necessary, will open the select dropdown beforehand.\n * The desired option can be targeted via the option's node, the option's text, or the option's index.\n */\n async selectOption(opts: SelectTriggerOptionOpts) {\n let {\n option,\n interactionType = this._interactionType\n } = opts || {};\n let trigger = this.trigger;\n if (!trigger.getAttribute('aria-controls')) {\n await this.open();\n }\n let listbox = this.listbox;\n if (!listbox) {\n throw new Error('Select\\'s listbox not found.');\n }\n\n if (listbox) {\n if (typeof option === 'string' || typeof option === 'number') {\n option = this.findOption({optionIndexOrText: option});\n }\n\n if (!option) {\n throw new Error('Target option not found in the listbox.');\n }\n\n if (interactionType === 'keyboard') {\n if (option?.getAttribute('aria-disabled') === 'true') {\n return;\n }\n\n if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) {\n act(() => listbox.focus());\n }\n await this.keyboardNavigateToOption({option});\n await this.user.keyboard('[Enter]');\n } else {\n // TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)\n if (interactionType === 'mouse') {\n await this.user.click(option);\n } else {\n await this.user.pointer({target: option, keys: '[TouchA]'});\n }\n }\n\n if (option?.getAttribute('href') == null) {\n await waitFor(() => {\n if (document.activeElement !== this._trigger) {\n throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);\n } else {\n return true;\n }\n });\n\n if (document.contains(listbox)) {\n throw new Error('Expected select element listbox to not be in the document after selecting an option');\n }\n }\n }\n }\n\n /**\n * Returns the select's options if present. Can be filtered to a subsection of the listbox if provided via `element`.\n */\n options(opts: {element?: HTMLElement} = {}): HTMLElement[] {\n let {element = this.listbox} = opts;\n let options = [];\n if (element) {\n options = within(element).queryAllByRole('option');\n }\n\n return options;\n }\n\n /**\n * Returns the select's trigger.\n */\n get trigger(): HTMLElement {\n return this._trigger;\n }\n\n /**\n * Returns the select's listbox if present.\n */\n get listbox(): HTMLElement | null {\n let listBoxId = this.trigger.getAttribute('aria-controls');\n return listBoxId ? document.getElementById(listBoxId) : null;\n }\n\n /**\n * Returns the select's sections if present.\n */\n get sections(): HTMLElement[] {\n let listbox = this.listbox;\n return listbox ? within(listbox).queryAllByRole('group') : [];\n }\n}\n"],"names":[],"version":3,"file":"select.main.js.map"}
|
package/dist/select.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {act as $7v0pZ$act, waitFor as $7v0pZ$waitFor, within as $7v0pZ$within} from "@testing-library/react";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2024 Adobe. All rights reserved.
|
|
@@ -12,87 +12,133 @@ import {within as $7v0pZ$within, act as $7v0pZ$act, waitFor as $7v0pZ$waitFor} f
|
|
|
12
12
|
* governing permissions and limitations under the License.
|
|
13
13
|
*/
|
|
14
14
|
class $4350df3e19e40e36$export$d1859707465446a9 {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Set the interaction type used by the select tester.
|
|
17
|
+
*/ setInteractionType(type) {
|
|
18
|
+
this._interactionType = type;
|
|
17
19
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Opens the select. Defaults to using the interaction type set on the select tester.
|
|
22
|
+
*/ async open(opts = {}) {
|
|
23
|
+
let { interactionType: interactionType = this._interactionType } = opts;
|
|
24
|
+
let trigger = this.trigger;
|
|
25
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
26
|
+
if (interactionType === 'mouse') await this.user.click(this._trigger);
|
|
27
|
+
else if (interactionType === 'keyboard') {
|
|
28
|
+
(0, $7v0pZ$act)(()=>trigger.focus());
|
|
29
|
+
await this.user.keyboard('[Enter]');
|
|
30
|
+
} else if (interactionType === 'touch') await this.user.pointer({
|
|
31
|
+
target: this._trigger,
|
|
32
|
+
keys: '[TouchA]'
|
|
33
|
+
});
|
|
34
|
+
await (0, $7v0pZ$waitFor)(()=>{
|
|
35
|
+
if (!isDisabled && trigger.getAttribute('aria-controls') == null) throw new Error('No aria-controls found on select element trigger.');
|
|
36
|
+
else return true;
|
|
37
|
+
});
|
|
38
|
+
let listBoxId = trigger.getAttribute('aria-controls');
|
|
39
|
+
await (0, $7v0pZ$waitFor)(()=>{
|
|
40
|
+
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
41
|
+
else return true;
|
|
42
|
+
});
|
|
21
43
|
}
|
|
22
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Closes the select.
|
|
46
|
+
*/ async close() {
|
|
23
47
|
let listbox = this.listbox;
|
|
24
|
-
|
|
48
|
+
if (listbox) {
|
|
49
|
+
(0, $7v0pZ$act)(()=>listbox.focus());
|
|
50
|
+
await this.user.keyboard('[Escape]');
|
|
51
|
+
}
|
|
52
|
+
await (0, $7v0pZ$waitFor)(()=>{
|
|
53
|
+
if (document.activeElement !== this._trigger) throw new Error(`Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`);
|
|
54
|
+
else return true;
|
|
55
|
+
});
|
|
56
|
+
if (listbox && document.contains(listbox)) throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');
|
|
25
57
|
}
|
|
26
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Returns a option matching the specified index or text content.
|
|
60
|
+
*/ findOption(opts) {
|
|
61
|
+
let { optionIndexOrText: optionIndexOrText } = opts;
|
|
62
|
+
let option;
|
|
63
|
+
let options = this.options();
|
|
27
64
|
let listbox = this.listbox;
|
|
28
|
-
|
|
65
|
+
if (typeof optionIndexOrText === 'number') option = options[optionIndexOrText];
|
|
66
|
+
else if (typeof optionIndexOrText === 'string' && listbox != null) option = (0, $7v0pZ$within)(listbox).getByText(optionIndexOrText).closest('[role=option]');
|
|
67
|
+
return option;
|
|
29
68
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
69
|
+
async keyboardNavigateToOption(opts) {
|
|
70
|
+
let { option: option } = opts;
|
|
71
|
+
let options = this.options();
|
|
72
|
+
let targetIndex = options.indexOf(option);
|
|
73
|
+
if (targetIndex === -1) throw new Error('Option provided is not in the listbox');
|
|
74
|
+
if (document.activeElement === this.listbox) await this.user.keyboard('[ArrowDown]');
|
|
75
|
+
let currIndex = options.indexOf(document.activeElement);
|
|
76
|
+
if (currIndex === -1) throw new Error('ActiveElement is not in the listbox');
|
|
77
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
78
|
+
for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Selects the desired select option. Defaults to using the interaction type set on the select tester. If necessary, will open the select dropdown beforehand.
|
|
82
|
+
* The desired option can be targeted via the option's node, the option's text, or the option's index.
|
|
83
|
+
*/ async selectOption(opts) {
|
|
84
|
+
let { option: option, interactionType: interactionType = this._interactionType } = opts || {};
|
|
85
|
+
let trigger = this.trigger;
|
|
86
|
+
if (!trigger.getAttribute('aria-controls')) await this.open();
|
|
87
|
+
let listbox = this.listbox;
|
|
88
|
+
if (!listbox) throw new Error('Select\'s listbox not found.');
|
|
89
|
+
if (listbox) {
|
|
90
|
+
if (typeof option === 'string' || typeof option === 'number') option = this.findOption({
|
|
91
|
+
optionIndexOrText: option
|
|
92
|
+
});
|
|
93
|
+
if (!option) throw new Error('Target option not found in the listbox.');
|
|
94
|
+
if (interactionType === 'keyboard') {
|
|
95
|
+
if ((option === null || option === void 0 ? void 0 : option.getAttribute('aria-disabled')) === 'true') return;
|
|
96
|
+
if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) (0, $7v0pZ$act)(()=>listbox.focus());
|
|
97
|
+
await this.keyboardNavigateToOption({
|
|
98
|
+
option: option
|
|
99
|
+
});
|
|
41
100
|
await this.user.keyboard('[Enter]');
|
|
42
|
-
} else if (
|
|
43
|
-
|
|
101
|
+
} else // TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)
|
|
102
|
+
if (interactionType === 'mouse') await this.user.click(option);
|
|
103
|
+
else await this.user.pointer({
|
|
104
|
+
target: option,
|
|
44
105
|
keys: '[TouchA]'
|
|
45
106
|
});
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let listBoxId = trigger.getAttribute('aria-controls');
|
|
51
|
-
await (0, $7v0pZ$waitFor)(()=>{
|
|
52
|
-
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
53
|
-
else return true;
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
this.selectOption = async (opts)=>{
|
|
57
|
-
let { optionText: optionText, interactionType: interactionType = this._interactionType } = opts || {};
|
|
58
|
-
let trigger = this.trigger;
|
|
59
|
-
if (!trigger.getAttribute('aria-controls')) await this.open();
|
|
60
|
-
let listbox = this.listbox;
|
|
61
|
-
if (listbox) {
|
|
62
|
-
let option = (0, $7v0pZ$within)(listbox).getByText(optionText);
|
|
63
|
-
if (interactionType === 'keyboard') {
|
|
64
|
-
if (document.activeElement !== listbox || !listbox.contains(document.activeElement)) (0, $7v0pZ$act)(()=>listbox.focus());
|
|
65
|
-
// TODO: this simulates typeahead, do we want to add a helper util for that? Not sure if users would really need that for
|
|
66
|
-
// their test
|
|
67
|
-
await this.user.keyboard(optionText);
|
|
68
|
-
await this.user.keyboard('[Enter]');
|
|
69
|
-
} else // TODO: what if the user needs to scroll the list to find the option? What if there are multiple matches for text (hopefully the picker options are pretty unique)
|
|
70
|
-
if (interactionType === 'mouse') await this.user.click(option);
|
|
71
|
-
else await this.user.pointer({
|
|
72
|
-
target: option,
|
|
73
|
-
keys: '[TouchA]'
|
|
107
|
+
if ((option === null || option === void 0 ? void 0 : option.getAttribute('href')) == null) {
|
|
108
|
+
await (0, $7v0pZ$waitFor)(()=>{
|
|
109
|
+
if (document.activeElement !== this._trigger) throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);
|
|
110
|
+
else return true;
|
|
74
111
|
});
|
|
75
|
-
if (
|
|
76
|
-
await (0, $7v0pZ$waitFor)(()=>{
|
|
77
|
-
if (document.activeElement !== this._trigger) throw new Error(`Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`);
|
|
78
|
-
else return true;
|
|
79
|
-
});
|
|
80
|
-
if (document.contains(listbox)) throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
this.close = async ()=>{
|
|
85
|
-
let listbox = this.listbox;
|
|
86
|
-
if (listbox) {
|
|
87
|
-
(0, $7v0pZ$act)(()=>listbox.focus());
|
|
88
|
-
await this.user.keyboard('[Escape]');
|
|
112
|
+
if (document.contains(listbox)) throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
89
113
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Returns the select's options if present. Can be filtered to a subsection of the listbox if provided via `element`.
|
|
118
|
+
*/ options(opts = {}) {
|
|
119
|
+
let { element: element = this.listbox } = opts;
|
|
120
|
+
let options = [];
|
|
121
|
+
if (element) options = (0, $7v0pZ$within)(element).queryAllByRole('option');
|
|
122
|
+
return options;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns the select's trigger.
|
|
126
|
+
*/ get trigger() {
|
|
127
|
+
return this._trigger;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns the select's listbox if present.
|
|
131
|
+
*/ get listbox() {
|
|
132
|
+
let listBoxId = this.trigger.getAttribute('aria-controls');
|
|
133
|
+
return listBoxId ? document.getElementById(listBoxId) : null;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns the select's sections if present.
|
|
137
|
+
*/ get sections() {
|
|
138
|
+
let listbox = this.listbox;
|
|
139
|
+
return listbox ? (0, $7v0pZ$within)(listbox).queryAllByRole('group') : [];
|
|
140
|
+
}
|
|
141
|
+
constructor(opts){
|
|
96
142
|
let { root: root, user: user, interactionType: interactionType } = opts;
|
|
97
143
|
this.user = user;
|
|
98
144
|
this._interactionType = interactionType || 'mouse';
|