@react-aria/test-utils 1.0.0-nightly.5042 → 1.0.0-rc.0
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/README.md +70 -0
- package/dist/import.mjs +6 -4
- package/dist/main.js +15 -23
- package/dist/main.js.map +1 -1
- package/dist/module.js +6 -4
- package/dist/module.js.map +1 -1
- package/dist/private/act.cjs +33 -0
- package/dist/private/act.cjs.map +1 -0
- package/dist/private/act.js +28 -0
- package/dist/private/act.js.map +1 -0
- package/dist/private/checkboxgroup.cjs +107 -0
- package/dist/private/checkboxgroup.cjs.map +1 -0
- package/dist/private/checkboxgroup.js +102 -0
- package/dist/private/checkboxgroup.js.map +1 -0
- package/dist/private/combobox.cjs +199 -0
- package/dist/private/combobox.cjs.map +1 -0
- package/dist/private/combobox.js +194 -0
- package/dist/private/combobox.js.map +1 -0
- package/dist/private/dialog.cjs +110 -0
- package/dist/private/dialog.cjs.map +1 -0
- package/dist/private/dialog.js +105 -0
- package/dist/private/dialog.js.map +1 -0
- package/dist/private/gridlist.cjs +173 -0
- package/dist/private/gridlist.cjs.map +1 -0
- package/dist/private/gridlist.js +168 -0
- package/dist/private/gridlist.js.map +1 -0
- package/dist/private/listbox.cjs +163 -0
- package/dist/private/listbox.cjs.map +1 -0
- package/dist/private/listbox.js +158 -0
- package/dist/private/listbox.js.map +1 -0
- package/dist/private/menu.cjs +265 -0
- package/dist/private/menu.cjs.map +1 -0
- package/dist/private/menu.js +260 -0
- package/dist/private/menu.js.map +1 -0
- package/dist/private/radiogroup.cjs +122 -0
- package/dist/private/radiogroup.cjs.map +1 -0
- package/dist/private/radiogroup.js +117 -0
- package/dist/private/radiogroup.js.map +1 -0
- package/dist/private/select.cjs +169 -0
- package/dist/private/select.cjs.map +1 -0
- package/dist/private/select.js +164 -0
- package/dist/private/select.js.map +1 -0
- package/dist/private/table.cjs +346 -0
- package/dist/private/table.cjs.map +1 -0
- package/dist/private/table.js +341 -0
- package/dist/private/table.js.map +1 -0
- package/dist/private/tabs.cjs +131 -0
- package/dist/private/tabs.cjs.map +1 -0
- package/dist/private/tabs.js +126 -0
- package/dist/private/tabs.js.map +1 -0
- package/dist/private/testSetup.cjs +87 -0
- package/dist/private/testSetup.cjs.map +1 -0
- package/dist/private/testSetup.js +81 -0
- package/dist/private/testSetup.js.map +1 -0
- package/dist/private/tree.cjs +181 -0
- package/dist/private/tree.cjs.map +1 -0
- package/dist/private/tree.js +176 -0
- package/dist/private/tree.js.map +1 -0
- package/dist/private/user.cjs +85 -0
- package/dist/private/user.cjs.map +1 -0
- package/dist/private/user.js +76 -0
- package/dist/private/user.js.map +1 -0
- package/dist/{userEventMaps.main.js → private/userEventMaps.cjs} +3 -3
- package/dist/private/userEventMaps.cjs.map +1 -0
- package/dist/{userEventMaps.mjs → private/userEventMaps.js} +3 -3
- package/dist/private/userEventMaps.js.map +1 -0
- package/dist/private/utils.cjs +136 -0
- package/dist/private/utils.cjs.map +1 -0
- package/dist/private/utils.js +127 -0
- package/dist/private/utils.js.map +1 -0
- package/dist/types/src/act.d.ts +4 -0
- package/dist/types/src/checkboxgroup.d.ts +47 -0
- package/dist/types/src/combobox.d.ts +87 -0
- package/dist/types/src/dialog.d.ts +37 -0
- package/dist/types/src/events.d.ts +25 -0
- package/dist/types/src/gridlist.d.ts +56 -0
- package/dist/types/src/index.d.ts +16 -0
- package/dist/types/src/listbox.d.ts +91 -0
- package/dist/types/src/menu.d.ts +112 -0
- package/dist/types/src/radiogroup.d.ts +47 -0
- package/dist/types/src/select.d.ts +74 -0
- package/dist/types/src/table.d.ts +120 -0
- package/dist/types/src/tabs.d.ts +59 -0
- package/dist/types/src/testSetup.d.ts +6 -0
- package/dist/types/src/tree.d.ts +62 -0
- package/dist/types/src/types.d.ts +143 -0
- package/dist/types/src/user.d.ts +49 -0
- package/dist/types/src/userEventMaps.d.ts +2 -0
- package/dist/types/src/utils.d.ts +29 -0
- package/package.json +26 -18
- package/src/act.ts +35 -0
- package/src/checkboxgroup.ts +165 -0
- package/src/combobox.ts +307 -0
- package/src/dialog.ts +155 -0
- package/src/gridlist.ts +278 -0
- package/src/index.ts +17 -3
- package/src/listbox.ts +300 -0
- package/src/menu.ts +479 -0
- package/src/radiogroup.ts +179 -0
- package/src/select.ts +273 -0
- package/src/table.ts +589 -0
- package/src/tabs.ts +204 -0
- package/src/testSetup.ts +41 -36
- package/src/tree.ts +290 -0
- package/src/types.ts +171 -0
- package/src/user.ts +153 -0
- package/src/userEventMaps.ts +1 -1
- package/src/utils.ts +155 -0
- package/dist/events.main.js +0 -37
- package/dist/events.main.js.map +0 -1
- package/dist/events.mjs +0 -31
- package/dist/events.module.js +0 -31
- package/dist/events.module.js.map +0 -1
- package/dist/testSetup.main.js +0 -82
- package/dist/testSetup.main.js.map +0 -1
- package/dist/testSetup.mjs +0 -76
- package/dist/testSetup.module.js +0 -76
- package/dist/testSetup.module.js.map +0 -1
- package/dist/types.d.ts +0 -16
- package/dist/types.d.ts.map +0 -1
- package/dist/userEventMaps.main.js.map +0 -1
- package/dist/userEventMaps.module.js +0 -38
- package/dist/userEventMaps.module.js.map +0 -1
- package/src/events.ts +0 -28
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
var $a84d573878cc1e5e$exports = require("./act.cjs");
|
|
2
|
+
var $022fcf8e360befed$exports = require("./utils.cjs");
|
|
3
|
+
var $gbHUB$testinglibrarydom = require("@testing-library/dom");
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function $parcel$export(e, n, v, s) {
|
|
7
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
$parcel$export(module.exports, "RadioGroupTester", function () { return $4308942495e5af40$export$d66326e63d27e116; });
|
|
11
|
+
/*
|
|
12
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class $4308942495e5af40$export$d66326e63d27e116 {
|
|
25
|
+
constructor(opts){
|
|
26
|
+
let { root: root, user: user, interactionType: interactionType, direction: direction } = opts;
|
|
27
|
+
this.user = user;
|
|
28
|
+
this._interactionType = interactionType || 'mouse';
|
|
29
|
+
this._direction = direction || 'ltr';
|
|
30
|
+
this._radiogroup = root;
|
|
31
|
+
let radiogroup = (0, $gbHUB$testinglibrarydom.within)(root).queryAllByRole('radiogroup');
|
|
32
|
+
if (radiogroup.length > 0) this._radiogroup = radiogroup[0];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Set the interaction type used by the radio tester.
|
|
36
|
+
*/ setInteractionType(type) {
|
|
37
|
+
this._interactionType = type;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns a radio matching the specified index or text content.
|
|
41
|
+
*/ findRadio(opts) {
|
|
42
|
+
let { indexOrText: indexOrText } = opts;
|
|
43
|
+
let radio;
|
|
44
|
+
if (typeof indexOrText === 'number') radio = this.getRadios()[indexOrText];
|
|
45
|
+
else if (typeof indexOrText === 'string') {
|
|
46
|
+
let label = (0, $gbHUB$testinglibrarydom.within)(this.getRadioGroup()).getByText(indexOrText);
|
|
47
|
+
// Label may wrap the radio, or the actual label may be a sibling span, or the radio div could have the label within it
|
|
48
|
+
if (label) {
|
|
49
|
+
radio = (0, $gbHUB$testinglibrarydom.within)(label).queryByRole('radio');
|
|
50
|
+
if (!radio) {
|
|
51
|
+
let labelWrapper = label.closest('label');
|
|
52
|
+
if (labelWrapper) radio = (0, $gbHUB$testinglibrarydom.within)(labelWrapper).queryByRole('radio');
|
|
53
|
+
else radio = label.closest('[role=radio]');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return radio;
|
|
58
|
+
}
|
|
59
|
+
async keyboardNavigateToRadio(opts) {
|
|
60
|
+
let { radio: radio, orientation: orientation = 'vertical' } = opts;
|
|
61
|
+
let radios = this.getRadios();
|
|
62
|
+
radios = radios.filter((radio)=>!(radio.hasAttribute('disabled') || radio.getAttribute('aria-disabled') === 'true'));
|
|
63
|
+
if (radios.length === 0) throw new Error('Radio group doesnt have any non-disabled radios. Please double check your radio group.');
|
|
64
|
+
let targetIndex = radios.indexOf(radio);
|
|
65
|
+
if (targetIndex === -1) throw new Error('Radio provided is not in the radio group.');
|
|
66
|
+
if (!this.getRadioGroup().contains(document.activeElement)) {
|
|
67
|
+
let selectedRadio = this.getSelectedRadio();
|
|
68
|
+
if (selectedRadio != null) (0, $a84d573878cc1e5e$exports.act)(()=>selectedRadio.focus());
|
|
69
|
+
else (0, $a84d573878cc1e5e$exports.act)(()=>radios[0]?.focus());
|
|
70
|
+
}
|
|
71
|
+
let currIndex = radios.indexOf(document.activeElement);
|
|
72
|
+
if (currIndex === -1) throw new Error('Active element is not in the radio group.');
|
|
73
|
+
let arrowUp = 'ArrowUp';
|
|
74
|
+
let arrowDown = 'ArrowDown';
|
|
75
|
+
if (orientation === 'horizontal') {
|
|
76
|
+
if (this._direction === 'ltr') {
|
|
77
|
+
arrowUp = 'ArrowLeft';
|
|
78
|
+
arrowDown = 'ArrowRight';
|
|
79
|
+
} else {
|
|
80
|
+
arrowUp = 'ArrowRight';
|
|
81
|
+
arrowDown = 'ArrowLeft';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
let movementDirection = targetIndex > currIndex ? 'down' : 'up';
|
|
85
|
+
for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Triggers the specified radio. Defaults to using the interaction type set on the radio tester.
|
|
89
|
+
*/ async triggerRadio(opts) {
|
|
90
|
+
let { radio: radio, interactionType: interactionType = this._interactionType } = opts;
|
|
91
|
+
if (typeof radio === 'string' || typeof radio === 'number') radio = this.findRadio({
|
|
92
|
+
indexOrText: radio
|
|
93
|
+
});
|
|
94
|
+
if (!radio) throw new Error(`Target radio "${(0, $022fcf8e360befed$exports.formatTargetNode)(opts.radio)}" not found in the radio group.`);
|
|
95
|
+
else if (radio.hasAttribute('disabled')) throw new Error(`Target radio "${(0, $022fcf8e360befed$exports.formatTargetNode)(opts.radio)}" is disabled.`);
|
|
96
|
+
if (interactionType === 'keyboard') {
|
|
97
|
+
let radioOrientation = this._radiogroup.getAttribute('aria-orientation') || 'horizontal';
|
|
98
|
+
await this.keyboardNavigateToRadio({
|
|
99
|
+
radio: radio,
|
|
100
|
+
orientation: radioOrientation
|
|
101
|
+
});
|
|
102
|
+
} else await (0, $022fcf8e360befed$exports.pressElement)(this.user, radio, interactionType);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns the radiogroup.
|
|
106
|
+
*/ getRadioGroup() {
|
|
107
|
+
return this._radiogroup;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns the radios.
|
|
111
|
+
*/ getRadios() {
|
|
112
|
+
return (0, $gbHUB$testinglibrarydom.within)(this.getRadioGroup()).queryAllByRole('radio');
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Returns the currently selected radio in the radiogroup if any.
|
|
116
|
+
*/ getSelectedRadio() {
|
|
117
|
+
return this.getRadios().find((radio)=>radio.checked) || null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=radiogroup.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";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A,GAAA,0CAAe,EAAE,KAAK,KAAK,EAAE,+BAA+B,CAAC;aAE3E,IAAI,MAAM,YAAY,CAAC,aAC5B,MAAM,IAAI,MAAM,CAAC,cAAc,EAAE,CAAA,GAAA,0CAAe,EAAE,KAAK,KAAK,EAAE,cAAc,CAAC;QAG/E,IAAI,oBAAoB,YAAY;YAClC,IAAI,mBAAmB,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,uBAAuB;YAC5E,MAAM,IAAI,CAAC,uBAAuB,CAAC;uBAAC;gBAAO,aAAa;YAA+B;QACzF,OACE,MAAM,CAAA,GAAA,sCAAW,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO;IAEzC;IAEA;;GAEC,GACD,gBAA6B;QAC3B,OAAO,IAAI,CAAC,WAAW;IACzB;IAEA;;GAEC,GACD,YAA2B;QACzB,OAAO,CAAA,GAAA,+BAAK,EAAE,IAAI,CAAC,aAAa,IAAI,cAAc,CAAC;IACrD;IAEA;;GAEC,GACD,mBAAuC;QACrC,OAAO,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,CAAA,QAAS,AAAC,MAA2B,OAAO,KAAK;IAChF;AACF","sources":["packages/@react-aria/test-utils/src/radiogroup.ts"],"sourcesContent":["/*\n * Copyright 2025 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} from './act';\nimport {Direction, Orientation, RadioGroupTesterOpts, UserOpts} from './types';\nimport {formatTargetNode, pressElement} from './utils';\nimport {within} from '@testing-library/dom';\n\ninterface TriggerRadioOptions {\n /**\n * What interaction type to use when triggering a radio. Defaults to the interaction type set on\n * the tester.\n */\n interactionType?: UserOpts['interactionType'];\n /**\n * The index, text, or node of the radio to toggle selection for.\n */\n radio: number | string | HTMLElement;\n}\n\nexport class RadioGroupTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _radiogroup: HTMLElement;\n private _direction: Direction;\n\n constructor(opts: RadioGroupTesterOpts) {\n let {root, user, interactionType, direction} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n this._direction = direction || 'ltr';\n\n this._radiogroup = root;\n let radiogroup = within(root).queryAllByRole('radiogroup');\n if (radiogroup.length > 0) {\n this._radiogroup = radiogroup[0];\n }\n }\n\n /**\n * Set the interaction type used by the radio tester.\n */\n setInteractionType(type: UserOpts['interactionType']): void {\n this._interactionType = type;\n }\n\n /**\n * Returns a radio matching the specified index or text content.\n */\n findRadio(opts: {indexOrText: number | string}): HTMLElement {\n let {indexOrText} = opts;\n\n let radio;\n if (typeof indexOrText === 'number') {\n radio = this.getRadios()[indexOrText];\n } else if (typeof indexOrText === 'string') {\n let label = within(this.getRadioGroup()).getByText(indexOrText);\n // Label may wrap the radio, or the actual label may be a sibling span, or the radio div could have the label within it\n if (label) {\n radio = within(label).queryByRole('radio');\n if (!radio) {\n let labelWrapper = label.closest('label');\n if (labelWrapper) {\n radio = within(labelWrapper).queryByRole('radio');\n } else {\n radio = label.closest('[role=radio]');\n }\n }\n }\n }\n\n return radio;\n }\n\n private async keyboardNavigateToRadio(opts: {radio: HTMLElement; orientation?: Orientation}) {\n let {radio, orientation = 'vertical'} = opts;\n let radios = this.getRadios();\n radios = radios.filter(\n radio => !(radio.hasAttribute('disabled') || radio.getAttribute('aria-disabled') === 'true')\n );\n if (radios.length === 0) {\n throw new Error(\n 'Radio group doesnt have any non-disabled radios. Please double check your radio group.'\n );\n }\n\n let targetIndex = radios.indexOf(radio);\n if (targetIndex === -1) {\n throw new Error('Radio provided is not in the radio group.');\n }\n\n if (!this.getRadioGroup().contains(document.activeElement)) {\n let selectedRadio = this.getSelectedRadio();\n if (selectedRadio != null) {\n act(() => selectedRadio.focus());\n } else {\n act(() => radios[0]?.focus());\n }\n }\n\n let currIndex = radios.indexOf(document.activeElement as HTMLElement);\n if (currIndex === -1) {\n throw new Error('Active element is not in the radio group.');\n }\n\n let arrowUp = 'ArrowUp';\n let arrowDown = 'ArrowDown';\n if (orientation === 'horizontal') {\n if (this._direction === 'ltr') {\n arrowUp = 'ArrowLeft';\n arrowDown = 'ArrowRight';\n } else {\n arrowUp = 'ArrowRight';\n arrowDown = 'ArrowLeft';\n }\n }\n\n let movementDirection = targetIndex > currIndex ? 'down' : 'up';\n for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {\n await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);\n }\n }\n\n /**\n * Triggers the specified radio. Defaults to using the interaction type set on the radio tester.\n */\n async triggerRadio(opts: TriggerRadioOptions): Promise<void> {\n let {radio, interactionType = this._interactionType} = opts;\n\n if (typeof radio === 'string' || typeof radio === 'number') {\n radio = this.findRadio({indexOrText: radio});\n }\n\n if (!radio) {\n throw new Error(\n `Target radio \"${formatTargetNode(opts.radio)}\" not found in the radio group.`\n );\n } else if (radio.hasAttribute('disabled')) {\n throw new Error(`Target radio \"${formatTargetNode(opts.radio)}\" is disabled.`);\n }\n\n if (interactionType === 'keyboard') {\n let radioOrientation = this._radiogroup.getAttribute('aria-orientation') || 'horizontal';\n await this.keyboardNavigateToRadio({radio, orientation: radioOrientation as Orientation});\n } else {\n await pressElement(this.user, radio, interactionType);\n }\n }\n\n /**\n * Returns the radiogroup.\n */\n getRadioGroup(): HTMLElement {\n return this._radiogroup;\n }\n\n /**\n * Returns the radios.\n */\n getRadios(): HTMLElement[] {\n return within(this.getRadioGroup()).queryAllByRole('radio');\n }\n\n /**\n * Returns the currently selected radio in the radiogroup if any.\n */\n getSelectedRadio(): HTMLElement | null {\n return this.getRadios().find(radio => (radio as HTMLInputElement).checked) || null;\n }\n}\n"],"names":[],"version":3,"file":"radiogroup.cjs.map"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import {act as $1350703ef3ac1acc$export$3ba232387fd5d6dd} from "./act.js";
|
|
2
|
+
import {formatTargetNode as $b4e037a2907521c6$export$bc3bc4a9206bf789, pressElement as $b4e037a2907521c6$export$6ffa3eb717517feb} from "./utils.js";
|
|
3
|
+
import {within as $aysq4$within} from "@testing-library/dom";
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
7
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
9
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
12
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
13
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
14
|
+
* governing permissions and limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class $14dc6ef63d191867$export$d66326e63d27e116 {
|
|
19
|
+
constructor(opts){
|
|
20
|
+
let { root: root, user: user, interactionType: interactionType, direction: direction } = opts;
|
|
21
|
+
this.user = user;
|
|
22
|
+
this._interactionType = interactionType || 'mouse';
|
|
23
|
+
this._direction = direction || 'ltr';
|
|
24
|
+
this._radiogroup = root;
|
|
25
|
+
let radiogroup = (0, $aysq4$within)(root).queryAllByRole('radiogroup');
|
|
26
|
+
if (radiogroup.length > 0) this._radiogroup = radiogroup[0];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Set the interaction type used by the radio tester.
|
|
30
|
+
*/ setInteractionType(type) {
|
|
31
|
+
this._interactionType = type;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns a radio matching the specified index or text content.
|
|
35
|
+
*/ findRadio(opts) {
|
|
36
|
+
let { indexOrText: indexOrText } = opts;
|
|
37
|
+
let radio;
|
|
38
|
+
if (typeof indexOrText === 'number') radio = this.getRadios()[indexOrText];
|
|
39
|
+
else if (typeof indexOrText === 'string') {
|
|
40
|
+
let label = (0, $aysq4$within)(this.getRadioGroup()).getByText(indexOrText);
|
|
41
|
+
// Label may wrap the radio, or the actual label may be a sibling span, or the radio div could have the label within it
|
|
42
|
+
if (label) {
|
|
43
|
+
radio = (0, $aysq4$within)(label).queryByRole('radio');
|
|
44
|
+
if (!radio) {
|
|
45
|
+
let labelWrapper = label.closest('label');
|
|
46
|
+
if (labelWrapper) radio = (0, $aysq4$within)(labelWrapper).queryByRole('radio');
|
|
47
|
+
else radio = label.closest('[role=radio]');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return radio;
|
|
52
|
+
}
|
|
53
|
+
async keyboardNavigateToRadio(opts) {
|
|
54
|
+
let { radio: radio, orientation: orientation = 'vertical' } = opts;
|
|
55
|
+
let radios = this.getRadios();
|
|
56
|
+
radios = radios.filter((radio)=>!(radio.hasAttribute('disabled') || radio.getAttribute('aria-disabled') === 'true'));
|
|
57
|
+
if (radios.length === 0) throw new Error('Radio group doesnt have any non-disabled radios. Please double check your radio group.');
|
|
58
|
+
let targetIndex = radios.indexOf(radio);
|
|
59
|
+
if (targetIndex === -1) throw new Error('Radio provided is not in the radio group.');
|
|
60
|
+
if (!this.getRadioGroup().contains(document.activeElement)) {
|
|
61
|
+
let selectedRadio = this.getSelectedRadio();
|
|
62
|
+
if (selectedRadio != null) (0, $1350703ef3ac1acc$export$3ba232387fd5d6dd)(()=>selectedRadio.focus());
|
|
63
|
+
else (0, $1350703ef3ac1acc$export$3ba232387fd5d6dd)(()=>radios[0]?.focus());
|
|
64
|
+
}
|
|
65
|
+
let currIndex = radios.indexOf(document.activeElement);
|
|
66
|
+
if (currIndex === -1) throw new Error('Active element is not in the radio group.');
|
|
67
|
+
let arrowUp = 'ArrowUp';
|
|
68
|
+
let arrowDown = 'ArrowDown';
|
|
69
|
+
if (orientation === 'horizontal') {
|
|
70
|
+
if (this._direction === 'ltr') {
|
|
71
|
+
arrowUp = 'ArrowLeft';
|
|
72
|
+
arrowDown = 'ArrowRight';
|
|
73
|
+
} else {
|
|
74
|
+
arrowUp = 'ArrowRight';
|
|
75
|
+
arrowDown = 'ArrowLeft';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
let movementDirection = targetIndex > currIndex ? 'down' : 'up';
|
|
79
|
+
for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Triggers the specified radio. Defaults to using the interaction type set on the radio tester.
|
|
83
|
+
*/ async triggerRadio(opts) {
|
|
84
|
+
let { radio: radio, interactionType: interactionType = this._interactionType } = opts;
|
|
85
|
+
if (typeof radio === 'string' || typeof radio === 'number') radio = this.findRadio({
|
|
86
|
+
indexOrText: radio
|
|
87
|
+
});
|
|
88
|
+
if (!radio) throw new Error(`Target radio "${(0, $b4e037a2907521c6$export$bc3bc4a9206bf789)(opts.radio)}" not found in the radio group.`);
|
|
89
|
+
else if (radio.hasAttribute('disabled')) throw new Error(`Target radio "${(0, $b4e037a2907521c6$export$bc3bc4a9206bf789)(opts.radio)}" is disabled.`);
|
|
90
|
+
if (interactionType === 'keyboard') {
|
|
91
|
+
let radioOrientation = this._radiogroup.getAttribute('aria-orientation') || 'horizontal';
|
|
92
|
+
await this.keyboardNavigateToRadio({
|
|
93
|
+
radio: radio,
|
|
94
|
+
orientation: radioOrientation
|
|
95
|
+
});
|
|
96
|
+
} else await (0, $b4e037a2907521c6$export$6ffa3eb717517feb)(this.user, radio, interactionType);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns the radiogroup.
|
|
100
|
+
*/ getRadioGroup() {
|
|
101
|
+
return this._radiogroup;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Returns the radios.
|
|
105
|
+
*/ getRadios() {
|
|
106
|
+
return (0, $aysq4$within)(this.getRadioGroup()).queryAllByRole('radio');
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns the currently selected radio in the radiogroup if any.
|
|
110
|
+
*/ getSelectedRadio() {
|
|
111
|
+
return this.getRadios().find((radio)=>radio.checked) || null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
export {$14dc6ef63d191867$export$d66326e63d27e116 as RadioGroupTester};
|
|
117
|
+
//# sourceMappingURL=radiogroup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;AAAA;;;;;;;;;;CAUC;;;AAmBM,MAAM;IAMX,YAAY,IAA0B,CAAE;QACtC,IAAI,QAAC,IAAI,QAAE,IAAI,mBAAE,eAAe,aAAE,SAAS,EAAC,GAAG;QAC/C,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,gBAAgB,GAAG,mBAAmB;QAC3C,IAAI,CAAC,UAAU,GAAG,aAAa;QAE/B,IAAI,CAAC,WAAW,GAAG;QACnB,IAAI,aAAa,CAAA,GAAA,aAAK,EAAE,MAAM,cAAc,CAAC;QAC7C,IAAI,WAAW,MAAM,GAAG,GACtB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC,EAAE;IAEpC;IAEA;;GAEC,GACD,mBAAmB,IAAiC,EAAQ;QAC1D,IAAI,CAAC,gBAAgB,GAAG;IAC1B;IAEA;;GAEC,GACD,UAAU,IAAoC,EAAe;QAC3D,IAAI,eAAC,WAAW,EAAC,GAAG;QAEpB,IAAI;QACJ,IAAI,OAAO,gBAAgB,UACzB,QAAQ,IAAI,CAAC,SAAS,EAAE,CAAC,YAAY;aAChC,IAAI,OAAO,gBAAgB,UAAU;YAC1C,IAAI,QAAQ,CAAA,GAAA,aAAK,EAAE,IAAI,CAAC,aAAa,IAAI,SAAS,CAAC;YACnD,uHAAuH;YACvH,IAAI,OAAO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c,YAAY,IACrD,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,sBAAsB,SAAS,YAAY,QAAQ,CAAC,CAAC;IAEtF;IAEA;;GAEC,GACD,MAAM,aAAa,IAAyB,EAAiB;QAC3D,IAAI,SAAC,KAAK,mBAAE,kBAAkB,IAAI,CAAC,gBAAgB,EAAC,GAAG;QAEvD,IAAI,OAAO,UAAU,YAAY,OAAO,UAAU,UAChD,QAAQ,IAAI,CAAC,SAAS,CAAC;YAAC,aAAa;QAAK;QAG5C,IAAI,CAAC,OACH,MAAM,IAAI,MACR,CAAC,cAAc,EAAE,CAAA,GAAA,yCAAe,EAAE,KAAK,KAAK,EAAE,+BAA+B,CAAC;aAE3E,IAAI,MAAM,YAAY,CAAC,aAC5B,MAAM,IAAI,MAAM,CAAC,cAAc,EAAE,CAAA,GAAA,yCAAe,EAAE,KAAK,KAAK,EAAE,cAAc,CAAC;QAG/E,IAAI,oBAAoB,YAAY;YAClC,IAAI,mBAAmB,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,uBAAuB;YAC5E,MAAM,IAAI,CAAC,uBAAuB,CAAC;uBAAC;gBAAO,aAAa;YAA+B;QACzF,OACE,MAAM,CAAA,GAAA,yCAAW,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO;IAEzC;IAEA;;GAEC,GACD,gBAA6B;QAC3B,OAAO,IAAI,CAAC,WAAW;IACzB;IAEA;;GAEC,GACD,YAA2B;QACzB,OAAO,CAAA,GAAA,aAAK,EAAE,IAAI,CAAC,aAAa,IAAI,cAAc,CAAC;IACrD;IAEA;;GAEC,GACD,mBAAuC;QACrC,OAAO,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,CAAA,QAAS,AAAC,MAA2B,OAAO,KAAK;IAChF;AACF","sources":["packages/@react-aria/test-utils/src/radiogroup.ts"],"sourcesContent":["/*\n * Copyright 2025 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} from './act';\nimport {Direction, Orientation, RadioGroupTesterOpts, UserOpts} from './types';\nimport {formatTargetNode, pressElement} from './utils';\nimport {within} from '@testing-library/dom';\n\ninterface TriggerRadioOptions {\n /**\n * What interaction type to use when triggering a radio. Defaults to the interaction type set on\n * the tester.\n */\n interactionType?: UserOpts['interactionType'];\n /**\n * The index, text, or node of the radio to toggle selection for.\n */\n radio: number | string | HTMLElement;\n}\n\nexport class RadioGroupTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _radiogroup: HTMLElement;\n private _direction: Direction;\n\n constructor(opts: RadioGroupTesterOpts) {\n let {root, user, interactionType, direction} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n this._direction = direction || 'ltr';\n\n this._radiogroup = root;\n let radiogroup = within(root).queryAllByRole('radiogroup');\n if (radiogroup.length > 0) {\n this._radiogroup = radiogroup[0];\n }\n }\n\n /**\n * Set the interaction type used by the radio tester.\n */\n setInteractionType(type: UserOpts['interactionType']): void {\n this._interactionType = type;\n }\n\n /**\n * Returns a radio matching the specified index or text content.\n */\n findRadio(opts: {indexOrText: number | string}): HTMLElement {\n let {indexOrText} = opts;\n\n let radio;\n if (typeof indexOrText === 'number') {\n radio = this.getRadios()[indexOrText];\n } else if (typeof indexOrText === 'string') {\n let label = within(this.getRadioGroup()).getByText(indexOrText);\n // Label may wrap the radio, or the actual label may be a sibling span, or the radio div could have the label within it\n if (label) {\n radio = within(label).queryByRole('radio');\n if (!radio) {\n let labelWrapper = label.closest('label');\n if (labelWrapper) {\n radio = within(labelWrapper).queryByRole('radio');\n } else {\n radio = label.closest('[role=radio]');\n }\n }\n }\n }\n\n return radio;\n }\n\n private async keyboardNavigateToRadio(opts: {radio: HTMLElement; orientation?: Orientation}) {\n let {radio, orientation = 'vertical'} = opts;\n let radios = this.getRadios();\n radios = radios.filter(\n radio => !(radio.hasAttribute('disabled') || radio.getAttribute('aria-disabled') === 'true')\n );\n if (radios.length === 0) {\n throw new Error(\n 'Radio group doesnt have any non-disabled radios. Please double check your radio group.'\n );\n }\n\n let targetIndex = radios.indexOf(radio);\n if (targetIndex === -1) {\n throw new Error('Radio provided is not in the radio group.');\n }\n\n if (!this.getRadioGroup().contains(document.activeElement)) {\n let selectedRadio = this.getSelectedRadio();\n if (selectedRadio != null) {\n act(() => selectedRadio.focus());\n } else {\n act(() => radios[0]?.focus());\n }\n }\n\n let currIndex = radios.indexOf(document.activeElement as HTMLElement);\n if (currIndex === -1) {\n throw new Error('Active element is not in the radio group.');\n }\n\n let arrowUp = 'ArrowUp';\n let arrowDown = 'ArrowDown';\n if (orientation === 'horizontal') {\n if (this._direction === 'ltr') {\n arrowUp = 'ArrowLeft';\n arrowDown = 'ArrowRight';\n } else {\n arrowUp = 'ArrowRight';\n arrowDown = 'ArrowLeft';\n }\n }\n\n let movementDirection = targetIndex > currIndex ? 'down' : 'up';\n for (let i = 0; i < Math.abs(targetIndex - currIndex); i++) {\n await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);\n }\n }\n\n /**\n * Triggers the specified radio. Defaults to using the interaction type set on the radio tester.\n */\n async triggerRadio(opts: TriggerRadioOptions): Promise<void> {\n let {radio, interactionType = this._interactionType} = opts;\n\n if (typeof radio === 'string' || typeof radio === 'number') {\n radio = this.findRadio({indexOrText: radio});\n }\n\n if (!radio) {\n throw new Error(\n `Target radio \"${formatTargetNode(opts.radio)}\" not found in the radio group.`\n );\n } else if (radio.hasAttribute('disabled')) {\n throw new Error(`Target radio \"${formatTargetNode(opts.radio)}\" is disabled.`);\n }\n\n if (interactionType === 'keyboard') {\n let radioOrientation = this._radiogroup.getAttribute('aria-orientation') || 'horizontal';\n await this.keyboardNavigateToRadio({radio, orientation: radioOrientation as Orientation});\n } else {\n await pressElement(this.user, radio, interactionType);\n }\n }\n\n /**\n * Returns the radiogroup.\n */\n getRadioGroup(): HTMLElement {\n return this._radiogroup;\n }\n\n /**\n * Returns the radios.\n */\n getRadios(): HTMLElement[] {\n return within(this.getRadioGroup()).queryAllByRole('radio');\n }\n\n /**\n * Returns the currently selected radio in the radiogroup if any.\n */\n getSelectedRadio(): HTMLElement | null {\n return this.getRadios().find(radio => (radio as HTMLInputElement).checked) || null;\n }\n}\n"],"names":[],"version":3,"file":"radiogroup.js.map"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
var $a84d573878cc1e5e$exports = require("./act.cjs");
|
|
2
|
+
var $022fcf8e360befed$exports = require("./utils.cjs");
|
|
3
|
+
var $gMthZ$testinglibrarydom = require("@testing-library/dom");
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function $parcel$export(e, n, v, s) {
|
|
7
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
$parcel$export(module.exports, "SelectTester", function () { return $842eb702165f4e83$export$d1859707465446a9; });
|
|
11
|
+
/*
|
|
12
|
+
* Copyright 2024 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class $842eb702165f4e83$export$d1859707465446a9 {
|
|
25
|
+
constructor(opts){
|
|
26
|
+
let { root: root, user: user, interactionType: interactionType } = opts;
|
|
27
|
+
this.user = user;
|
|
28
|
+
this._interactionType = interactionType || 'mouse';
|
|
29
|
+
// Handle case where the wrapper element is provided rather than the Select's button (aka RAC)
|
|
30
|
+
let buttons = (0, $gMthZ$testinglibrarydom.within)(root).queryAllByRole('button');
|
|
31
|
+
let triggerButton;
|
|
32
|
+
if (buttons.length === 0) triggerButton = root;
|
|
33
|
+
else if (buttons.length === 1) triggerButton = buttons[0];
|
|
34
|
+
else triggerButton = buttons.find((button)=>button.hasAttribute('aria-haspopup'));
|
|
35
|
+
this._trigger = triggerButton ?? root;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Set the interaction type used by the select tester.
|
|
39
|
+
*/ setInteractionType(type) {
|
|
40
|
+
this._interactionType = type;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Opens the select. Defaults to using the interaction type set on the select tester.
|
|
44
|
+
*/ async open(opts = {}) {
|
|
45
|
+
let { interactionType: interactionType = this._interactionType } = opts;
|
|
46
|
+
let trigger = this.getTrigger();
|
|
47
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
48
|
+
if (interactionType === 'mouse') await this.user.click(this._trigger);
|
|
49
|
+
else if (interactionType === 'keyboard') {
|
|
50
|
+
(0, $a84d573878cc1e5e$exports.act)(()=>trigger.focus());
|
|
51
|
+
await this.user.keyboard('[Enter]');
|
|
52
|
+
} else if (interactionType === 'touch') await this.user.pointer({
|
|
53
|
+
target: this._trigger,
|
|
54
|
+
keys: '[TouchA]'
|
|
55
|
+
});
|
|
56
|
+
await (0, $gMthZ$testinglibrarydom.waitFor)(()=>{
|
|
57
|
+
if (!isDisabled && trigger.getAttribute('aria-controls') == null) throw new Error('No aria-controls found on select element trigger.');
|
|
58
|
+
else return true;
|
|
59
|
+
});
|
|
60
|
+
let listBoxId = trigger.getAttribute('aria-controls');
|
|
61
|
+
await (0, $gMthZ$testinglibrarydom.waitFor)(()=>{
|
|
62
|
+
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
63
|
+
else return true;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Closes the select.
|
|
68
|
+
*/ async close() {
|
|
69
|
+
let listbox = this.getListbox();
|
|
70
|
+
if (listbox) {
|
|
71
|
+
(0, $a84d573878cc1e5e$exports.act)(()=>listbox.focus());
|
|
72
|
+
await this.user.keyboard('[Escape]');
|
|
73
|
+
}
|
|
74
|
+
await (0, $gMthZ$testinglibrarydom.waitFor)(()=>{
|
|
75
|
+
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}`);
|
|
76
|
+
else return true;
|
|
77
|
+
});
|
|
78
|
+
if (listbox && document.contains(listbox)) throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns a option matching the specified index or text content.
|
|
82
|
+
*/ findOption(opts) {
|
|
83
|
+
let { indexOrText: indexOrText } = opts;
|
|
84
|
+
let option;
|
|
85
|
+
let options = this.getOptions();
|
|
86
|
+
let listbox = this.getListbox();
|
|
87
|
+
if (typeof indexOrText === 'number') option = options[indexOrText];
|
|
88
|
+
else if (typeof indexOrText === 'string' && listbox != null) option = (0, $gMthZ$testinglibrarydom.within)(listbox).getByText(indexOrText).closest('[role=option]');
|
|
89
|
+
return option;
|
|
90
|
+
}
|
|
91
|
+
async keyboardNavigateToOption(opts) {
|
|
92
|
+
let { option: option } = opts;
|
|
93
|
+
let options = this.getOptions();
|
|
94
|
+
let targetIndex = options.indexOf(option);
|
|
95
|
+
if (targetIndex === -1) throw new Error('Option provided is not in the listbox');
|
|
96
|
+
if (document.activeElement === this.getListbox()) await this.user.keyboard('[ArrowDown]');
|
|
97
|
+
let currIndex = options.indexOf(document.activeElement);
|
|
98
|
+
if (currIndex === -1) throw new Error('ActiveElement is not in the listbox');
|
|
99
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
100
|
+
for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Toggles the selection of the desired select option if possible. Defaults to using the
|
|
104
|
+
* interaction type set on the select tester. If necessary, will open the select dropdown
|
|
105
|
+
* beforehand. The desired option can be targeted via the option's node, the option's text, or the
|
|
106
|
+
* option's index.
|
|
107
|
+
*/ async toggleOptionSelection(opts) {
|
|
108
|
+
let { option: option, closesOnSelect: closesOnSelect, interactionType: interactionType = this._interactionType } = opts || {};
|
|
109
|
+
let trigger = this.getTrigger();
|
|
110
|
+
if (!trigger.getAttribute('aria-controls')) await this.open();
|
|
111
|
+
let listbox = this.getListbox();
|
|
112
|
+
if (!listbox) throw new Error("Select's listbox not found.");
|
|
113
|
+
if (typeof option === 'string' || typeof option === 'number') option = this.findOption({
|
|
114
|
+
indexOrText: option
|
|
115
|
+
});
|
|
116
|
+
if (!option) throw new Error(`Target option "${(0, $022fcf8e360befed$exports.formatTargetNode)(opts.option)}" not found in the listbox.`);
|
|
117
|
+
let isMultiSelect = listbox.getAttribute('aria-multiselectable') === 'true';
|
|
118
|
+
let isSingleSelect = !isMultiSelect;
|
|
119
|
+
closesOnSelect = closesOnSelect ?? isSingleSelect;
|
|
120
|
+
if (interactionType === 'keyboard') {
|
|
121
|
+
if (option?.getAttribute('aria-disabled') === 'true') throw new Error(`Cannot select disabled option "${(0, $022fcf8e360befed$exports.formatTargetNode)(opts.option)}".`);
|
|
122
|
+
if (document.activeElement !== listbox && !listbox.contains(document.activeElement)) (0, $a84d573878cc1e5e$exports.act)(()=>listbox.focus());
|
|
123
|
+
await this.keyboardNavigateToOption({
|
|
124
|
+
option: option
|
|
125
|
+
});
|
|
126
|
+
await this.user.keyboard('[Enter]');
|
|
127
|
+
} else if (interactionType === 'mouse') await this.user.click(option);
|
|
128
|
+
else await this.user.pointer({
|
|
129
|
+
target: option,
|
|
130
|
+
keys: '[TouchA]'
|
|
131
|
+
});
|
|
132
|
+
if (closesOnSelect && option?.getAttribute('href') == null) {
|
|
133
|
+
await (0, $gMthZ$testinglibrarydom.waitFor)(()=>{
|
|
134
|
+
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}`);
|
|
135
|
+
else return true;
|
|
136
|
+
});
|
|
137
|
+
if (document.contains(listbox)) throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Returns the select's options if present. Can be filtered to a subsection of the listbox if
|
|
142
|
+
* provided via `element`.
|
|
143
|
+
*/ getOptions(opts = {}) {
|
|
144
|
+
let { element: element = this.getListbox() } = opts;
|
|
145
|
+
let options = [];
|
|
146
|
+
if (element) options = (0, $gMthZ$testinglibrarydom.within)(element).queryAllByRole('option');
|
|
147
|
+
return options;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns the select's trigger.
|
|
151
|
+
*/ getTrigger() {
|
|
152
|
+
return this._trigger;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Returns the select's listbox if present.
|
|
156
|
+
*/ getListbox() {
|
|
157
|
+
let listBoxId = this.getTrigger().getAttribute('aria-controls');
|
|
158
|
+
return listBoxId ? document.getElementById(listBoxId) : null;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns the select's sections if present.
|
|
162
|
+
*/ getSections() {
|
|
163
|
+
let listbox = this.getListbox();
|
|
164
|
+
return listbox ? (0, $gMthZ$testinglibrarydom.within)(listbox).queryAllByRole('group') : [];
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
//# sourceMappingURL=select.cjs.map
|
|
@@ -0,0 +1 @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E;IAC/D;AACF","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} from './act';\nimport {formatTargetNode} from './utils';\nimport {SelectTesterOpts, UserOpts} from './types';\nimport {waitFor, within} from '@testing-library/dom';\n\ninterface SelectOpenOpts {\n /**\n * What interaction type to use when opening the select. Defaults to the interaction type set on\n * 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\n * `getOptions()`.\n */\n option: number | string | HTMLElement;\n /**\n * Whether or not the select closes on selection. Depends on select implementation and\n * configuration.\n *\n * @default true\n */\n closesOnSelect?: boolean;\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 buttons = within(root).queryAllByRole('button');\n let triggerButton;\n if (buttons.length === 0) {\n triggerButton = root;\n } else if (buttons.length === 1) {\n triggerButton = buttons[0];\n } else {\n triggerButton = buttons.find(button => button.hasAttribute('aria-haspopup'));\n }\n\n this._trigger = triggerButton ?? root;\n }\n /**\n * Set the interaction type used by the select tester.\n */\n setInteractionType(type: UserOpts['interactionType']): void {\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 = {}): Promise<void> {\n let {interactionType = this._interactionType} = opts;\n let trigger = this.getTrigger();\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(): Promise<void> {\n let listbox = this.getListbox();\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(\n `Expected the document.activeElement after closing the select dropdown to be the select component trigger but got ${document.activeElement}`\n );\n } else {\n return true;\n }\n });\n\n if (listbox && document.contains(listbox)) {\n throw new Error(\n 'Expected the select element listbox to not be in the document after closing the dropdown.'\n );\n }\n }\n\n /**\n * Returns a option matching the specified index or text content.\n */\n findOption(opts: {indexOrText: number | string}): HTMLElement {\n let {indexOrText} = opts;\n\n let option;\n let options = this.getOptions();\n let listbox = this.getListbox();\n\n if (typeof indexOrText === 'number') {\n option = options[indexOrText];\n } else if (typeof indexOrText === 'string' && listbox != null) {\n option = within(listbox!).getByText(indexOrText).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.getOptions();\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.getListbox()) {\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 * Toggles the selection of the desired select option if possible. Defaults to using the\n * interaction type set on the select tester. If necessary, will open the select dropdown\n * beforehand. The desired option can be targeted via the option's node, the option's text, or the\n * option's index.\n */\n async toggleOptionSelection(opts: SelectTriggerOptionOpts): Promise<void> {\n let {option, closesOnSelect, interactionType = this._interactionType} = opts || {};\n let trigger = this.getTrigger();\n if (!trigger.getAttribute('aria-controls')) {\n await this.open();\n }\n let listbox = this.getListbox();\n if (!listbox) {\n throw new Error(\"Select's listbox not found.\");\n }\n\n if (typeof option === 'string' || typeof option === 'number') {\n option = this.findOption({indexOrText: option});\n }\n\n if (!option) {\n throw new Error(`Target option \"${formatTargetNode(opts.option)}\" not found in the listbox.`);\n }\n\n let isMultiSelect = listbox.getAttribute('aria-multiselectable') === 'true';\n let isSingleSelect = !isMultiSelect;\n closesOnSelect = closesOnSelect ?? isSingleSelect;\n\n if (interactionType === 'keyboard') {\n if (option?.getAttribute('aria-disabled') === 'true') {\n throw new Error(`Cannot select disabled option \"${formatTargetNode(opts.option)}\".`);\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 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 (closesOnSelect && option?.getAttribute('href') == null) {\n await waitFor(() => {\n if (document.activeElement !== this._trigger) {\n throw new Error(\n `Expected the document.activeElement after selecting an option to be the select component trigger but got ${document.activeElement}`\n );\n } else {\n return true;\n }\n });\n\n if (document.contains(listbox)) {\n throw new Error(\n '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\n * provided via `element`.\n */\n getOptions(opts: {element?: HTMLElement} = {}): HTMLElement[] {\n let {element = this.getListbox()} = 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 getTrigger(): HTMLElement {\n return this._trigger;\n }\n\n /**\n * Returns the select's listbox if present.\n */\n getListbox(): HTMLElement | null {\n let listBoxId = this.getTrigger().getAttribute('aria-controls');\n return listBoxId ? document.getElementById(listBoxId) : null;\n }\n\n /**\n * Returns the select's sections if present.\n */\n getSections(): HTMLElement[] {\n let listbox = this.getListbox();\n return listbox ? within(listbox).queryAllByRole('group') : [];\n }\n}\n"],"names":[],"version":3,"file":"select.cjs.map"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {act as $1350703ef3ac1acc$export$3ba232387fd5d6dd} from "./act.js";
|
|
2
|
+
import {formatTargetNode as $b4e037a2907521c6$export$bc3bc4a9206bf789} from "./utils.js";
|
|
3
|
+
import {within as $d0NQz$within, waitFor as $d0NQz$waitFor} from "@testing-library/dom";
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Copyright 2024 Adobe. All rights reserved.
|
|
7
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
9
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
12
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
13
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
14
|
+
* governing permissions and limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class $6acb352063615cf0$export$d1859707465446a9 {
|
|
19
|
+
constructor(opts){
|
|
20
|
+
let { root: root, user: user, interactionType: interactionType } = opts;
|
|
21
|
+
this.user = user;
|
|
22
|
+
this._interactionType = interactionType || 'mouse';
|
|
23
|
+
// Handle case where the wrapper element is provided rather than the Select's button (aka RAC)
|
|
24
|
+
let buttons = (0, $d0NQz$within)(root).queryAllByRole('button');
|
|
25
|
+
let triggerButton;
|
|
26
|
+
if (buttons.length === 0) triggerButton = root;
|
|
27
|
+
else if (buttons.length === 1) triggerButton = buttons[0];
|
|
28
|
+
else triggerButton = buttons.find((button)=>button.hasAttribute('aria-haspopup'));
|
|
29
|
+
this._trigger = triggerButton ?? root;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Set the interaction type used by the select tester.
|
|
33
|
+
*/ setInteractionType(type) {
|
|
34
|
+
this._interactionType = type;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Opens the select. Defaults to using the interaction type set on the select tester.
|
|
38
|
+
*/ async open(opts = {}) {
|
|
39
|
+
let { interactionType: interactionType = this._interactionType } = opts;
|
|
40
|
+
let trigger = this.getTrigger();
|
|
41
|
+
let isDisabled = trigger.hasAttribute('disabled');
|
|
42
|
+
if (interactionType === 'mouse') await this.user.click(this._trigger);
|
|
43
|
+
else if (interactionType === 'keyboard') {
|
|
44
|
+
(0, $1350703ef3ac1acc$export$3ba232387fd5d6dd)(()=>trigger.focus());
|
|
45
|
+
await this.user.keyboard('[Enter]');
|
|
46
|
+
} else if (interactionType === 'touch') await this.user.pointer({
|
|
47
|
+
target: this._trigger,
|
|
48
|
+
keys: '[TouchA]'
|
|
49
|
+
});
|
|
50
|
+
await (0, $d0NQz$waitFor)(()=>{
|
|
51
|
+
if (!isDisabled && trigger.getAttribute('aria-controls') == null) throw new Error('No aria-controls found on select element trigger.');
|
|
52
|
+
else return true;
|
|
53
|
+
});
|
|
54
|
+
let listBoxId = trigger.getAttribute('aria-controls');
|
|
55
|
+
await (0, $d0NQz$waitFor)(()=>{
|
|
56
|
+
if (!isDisabled && (!listBoxId || document.getElementById(listBoxId) == null)) throw new Error(`ListBox with id of ${listBoxId} not found in document.`);
|
|
57
|
+
else return true;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Closes the select.
|
|
62
|
+
*/ async close() {
|
|
63
|
+
let listbox = this.getListbox();
|
|
64
|
+
if (listbox) {
|
|
65
|
+
(0, $1350703ef3ac1acc$export$3ba232387fd5d6dd)(()=>listbox.focus());
|
|
66
|
+
await this.user.keyboard('[Escape]');
|
|
67
|
+
}
|
|
68
|
+
await (0, $d0NQz$waitFor)(()=>{
|
|
69
|
+
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}`);
|
|
70
|
+
else return true;
|
|
71
|
+
});
|
|
72
|
+
if (listbox && document.contains(listbox)) throw new Error('Expected the select element listbox to not be in the document after closing the dropdown.');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns a option matching the specified index or text content.
|
|
76
|
+
*/ findOption(opts) {
|
|
77
|
+
let { indexOrText: indexOrText } = opts;
|
|
78
|
+
let option;
|
|
79
|
+
let options = this.getOptions();
|
|
80
|
+
let listbox = this.getListbox();
|
|
81
|
+
if (typeof indexOrText === 'number') option = options[indexOrText];
|
|
82
|
+
else if (typeof indexOrText === 'string' && listbox != null) option = (0, $d0NQz$within)(listbox).getByText(indexOrText).closest('[role=option]');
|
|
83
|
+
return option;
|
|
84
|
+
}
|
|
85
|
+
async keyboardNavigateToOption(opts) {
|
|
86
|
+
let { option: option } = opts;
|
|
87
|
+
let options = this.getOptions();
|
|
88
|
+
let targetIndex = options.indexOf(option);
|
|
89
|
+
if (targetIndex === -1) throw new Error('Option provided is not in the listbox');
|
|
90
|
+
if (document.activeElement === this.getListbox()) await this.user.keyboard('[ArrowDown]');
|
|
91
|
+
let currIndex = options.indexOf(document.activeElement);
|
|
92
|
+
if (currIndex === -1) throw new Error('ActiveElement is not in the listbox');
|
|
93
|
+
let direction = targetIndex > currIndex ? 'down' : 'up';
|
|
94
|
+
for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Toggles the selection of the desired select option if possible. Defaults to using the
|
|
98
|
+
* interaction type set on the select tester. If necessary, will open the select dropdown
|
|
99
|
+
* beforehand. The desired option can be targeted via the option's node, the option's text, or the
|
|
100
|
+
* option's index.
|
|
101
|
+
*/ async toggleOptionSelection(opts) {
|
|
102
|
+
let { option: option, closesOnSelect: closesOnSelect, interactionType: interactionType = this._interactionType } = opts || {};
|
|
103
|
+
let trigger = this.getTrigger();
|
|
104
|
+
if (!trigger.getAttribute('aria-controls')) await this.open();
|
|
105
|
+
let listbox = this.getListbox();
|
|
106
|
+
if (!listbox) throw new Error("Select's listbox not found.");
|
|
107
|
+
if (typeof option === 'string' || typeof option === 'number') option = this.findOption({
|
|
108
|
+
indexOrText: option
|
|
109
|
+
});
|
|
110
|
+
if (!option) throw new Error(`Target option "${(0, $b4e037a2907521c6$export$bc3bc4a9206bf789)(opts.option)}" not found in the listbox.`);
|
|
111
|
+
let isMultiSelect = listbox.getAttribute('aria-multiselectable') === 'true';
|
|
112
|
+
let isSingleSelect = !isMultiSelect;
|
|
113
|
+
closesOnSelect = closesOnSelect ?? isSingleSelect;
|
|
114
|
+
if (interactionType === 'keyboard') {
|
|
115
|
+
if (option?.getAttribute('aria-disabled') === 'true') throw new Error(`Cannot select disabled option "${(0, $b4e037a2907521c6$export$bc3bc4a9206bf789)(opts.option)}".`);
|
|
116
|
+
if (document.activeElement !== listbox && !listbox.contains(document.activeElement)) (0, $1350703ef3ac1acc$export$3ba232387fd5d6dd)(()=>listbox.focus());
|
|
117
|
+
await this.keyboardNavigateToOption({
|
|
118
|
+
option: option
|
|
119
|
+
});
|
|
120
|
+
await this.user.keyboard('[Enter]');
|
|
121
|
+
} else if (interactionType === 'mouse') await this.user.click(option);
|
|
122
|
+
else await this.user.pointer({
|
|
123
|
+
target: option,
|
|
124
|
+
keys: '[TouchA]'
|
|
125
|
+
});
|
|
126
|
+
if (closesOnSelect && option?.getAttribute('href') == null) {
|
|
127
|
+
await (0, $d0NQz$waitFor)(()=>{
|
|
128
|
+
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}`);
|
|
129
|
+
else return true;
|
|
130
|
+
});
|
|
131
|
+
if (document.contains(listbox)) throw new Error('Expected select element listbox to not be in the document after selecting an option');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns the select's options if present. Can be filtered to a subsection of the listbox if
|
|
136
|
+
* provided via `element`.
|
|
137
|
+
*/ getOptions(opts = {}) {
|
|
138
|
+
let { element: element = this.getListbox() } = opts;
|
|
139
|
+
let options = [];
|
|
140
|
+
if (element) options = (0, $d0NQz$within)(element).queryAllByRole('option');
|
|
141
|
+
return options;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Returns the select's trigger.
|
|
145
|
+
*/ getTrigger() {
|
|
146
|
+
return this._trigger;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Returns the select's listbox if present.
|
|
150
|
+
*/ getListbox() {
|
|
151
|
+
let listBoxId = this.getTrigger().getAttribute('aria-controls');
|
|
152
|
+
return listBoxId ? document.getElementById(listBoxId) : null;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Returns the select's sections if present.
|
|
156
|
+
*/ getSections() {
|
|
157
|
+
let listbox = this.getListbox();
|
|
158
|
+
return listbox ? (0, $d0NQz$within)(listbox).queryAllByRole('group') : [];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
export {$6acb352063615cf0$export$d1859707465446a9 as SelectTester};
|
|
164
|
+
//# sourceMappingURL=select.js.map
|