@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.
Files changed (64) hide show
  1. package/dist/combobox.main.js +116 -82
  2. package/dist/combobox.main.js.map +1 -1
  3. package/dist/combobox.mjs +117 -83
  4. package/dist/combobox.module.js +117 -83
  5. package/dist/combobox.module.js.map +1 -1
  6. package/dist/events.main.js.map +1 -1
  7. package/dist/events.module.js.map +1 -1
  8. package/dist/gridlist.main.js +102 -59
  9. package/dist/gridlist.main.js.map +1 -1
  10. package/dist/gridlist.mjs +103 -60
  11. package/dist/gridlist.module.js +103 -60
  12. package/dist/gridlist.module.js.map +1 -1
  13. package/dist/listbox.main.js +135 -0
  14. package/dist/listbox.main.js.map +1 -0
  15. package/dist/listbox.mjs +130 -0
  16. package/dist/listbox.module.js +130 -0
  17. package/dist/listbox.module.js.map +1 -0
  18. package/dist/main.js.map +1 -1
  19. package/dist/menu.main.js +195 -161
  20. package/dist/menu.main.js.map +1 -1
  21. package/dist/menu.mjs +196 -162
  22. package/dist/menu.module.js +196 -162
  23. package/dist/menu.module.js.map +1 -1
  24. package/dist/module.js.map +1 -1
  25. package/dist/select.main.js +116 -70
  26. package/dist/select.main.js.map +1 -1
  27. package/dist/select.mjs +117 -71
  28. package/dist/select.module.js +117 -71
  29. package/dist/select.module.js.map +1 -1
  30. package/dist/table.main.js +170 -139
  31. package/dist/table.main.js.map +1 -1
  32. package/dist/table.mjs +171 -140
  33. package/dist/table.module.js +171 -140
  34. package/dist/table.module.js.map +1 -1
  35. package/dist/tabs.main.js +133 -0
  36. package/dist/tabs.main.js.map +1 -0
  37. package/dist/tabs.mjs +128 -0
  38. package/dist/tabs.module.js +128 -0
  39. package/dist/tabs.module.js.map +1 -0
  40. package/dist/tree.main.js +165 -0
  41. package/dist/tree.main.js.map +1 -0
  42. package/dist/tree.mjs +160 -0
  43. package/dist/tree.module.js +160 -0
  44. package/dist/tree.module.js.map +1 -0
  45. package/dist/types.d.ts +607 -145
  46. package/dist/types.d.ts.map +1 -1
  47. package/dist/user.main.js +15 -4
  48. package/dist/user.main.js.map +1 -1
  49. package/dist/user.mjs +15 -4
  50. package/dist/user.module.js +15 -4
  51. package/dist/user.module.js.map +1 -1
  52. package/package.json +2 -3
  53. package/src/combobox.ts +105 -36
  54. package/src/events.ts +1 -1
  55. package/src/gridlist.ts +146 -59
  56. package/src/index.ts +1 -1
  57. package/src/listbox.ts +226 -0
  58. package/src/menu.ts +152 -62
  59. package/src/select.ts +137 -44
  60. package/src/table.ts +129 -64
  61. package/src/tabs.ts +198 -0
  62. package/src/tree.ts +248 -0
  63. package/src/types.ts +133 -0
  64. package/src/user.ts +62 -37
package/dist/tabs.mjs ADDED
@@ -0,0 +1,128 @@
1
+ import {pressElement as $5d1eca18f92ad0e6$export$6ffa3eb717517feb} from "./events.mjs";
2
+ import {within as $im2y9$within, act as $im2y9$act} from "@testing-library/react";
3
+
4
+ /*
5
+ * Copyright 2024 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ class $e615c34a00448a49$export$f1539bff3fc7d485 {
17
+ /**
18
+ * Set the interaction type used by the tabs tester.
19
+ */ setInteractionType(type) {
20
+ this._interactionType = type;
21
+ }
22
+ // TODO: This is pretty similar across most the utils, refactor to make it generic?
23
+ /**
24
+ * Returns a tab matching the specified index or text content.
25
+ */ findTab(opts) {
26
+ let { tabIndexOrText: tabIndexOrText } = opts;
27
+ let tab;
28
+ let tabs = this.tabs;
29
+ if (typeof tabIndexOrText === 'number') tab = tabs[tabIndexOrText];
30
+ else if (typeof tabIndexOrText === 'string') tab = (0, $im2y9$within)(this._tablist).getByText(tabIndexOrText).closest('[role=tab]');
31
+ return tab;
32
+ }
33
+ // TODO: also quite similar across more utils albeit with orientation, refactor to make generic
34
+ async keyboardNavigateToTab(opts) {
35
+ let { tab: tab, orientation: orientation = 'vertical' } = opts;
36
+ let tabs = this.tabs;
37
+ let targetIndex = tabs.indexOf(tab);
38
+ if (targetIndex === -1) throw new Error('Tab provided is not in the tablist');
39
+ if (!this._tablist.contains(document.activeElement)) {
40
+ let selectedTab = this.selectedTab;
41
+ if (selectedTab != null) (0, $im2y9$act)(()=>selectedTab.focus());
42
+ else (0, $im2y9$act)(()=>{
43
+ var _tabs_find;
44
+ return (_tabs_find = tabs.find((tab)=>!(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'))) === null || _tabs_find === void 0 ? void 0 : _tabs_find.focus();
45
+ });
46
+ }
47
+ let currIndex = this.tabs.indexOf(document.activeElement);
48
+ if (currIndex === -1) throw new Error('ActiveElement is not in the tablist');
49
+ let arrowUp = 'ArrowUp';
50
+ let arrowDown = 'ArrowDown';
51
+ if (orientation === 'horizontal') {
52
+ if (this._direction === 'ltr') {
53
+ arrowUp = 'ArrowLeft';
54
+ arrowDown = 'ArrowRight';
55
+ } else {
56
+ arrowUp = 'ArrowRight';
57
+ arrowDown = 'ArrowLeft';
58
+ }
59
+ }
60
+ let movementDirection = targetIndex > currIndex ? 'down' : 'up';
61
+ for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
62
+ }
63
+ /**
64
+ * Triggers the specified tab. Defaults to using the interaction type set on the tabs tester.
65
+ */ async triggerTab(opts) {
66
+ let { tab: tab, interactionType: interactionType = this._interactionType, manualActivation: manualActivation } = opts;
67
+ if (typeof tab === 'string' || typeof tab === 'number') tab = this.findTab({
68
+ tabIndexOrText: tab
69
+ });
70
+ if (!tab) throw new Error('Target tab not found in the tablist.');
71
+ else if (tab.hasAttribute('disabled')) throw new Error('Target tab is disabled.');
72
+ if (interactionType === 'keyboard') {
73
+ if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) (0, $im2y9$act)(()=>this._tablist.focus());
74
+ let tabsOrientation = this._tablist.getAttribute('aria-orientation') || 'horizontal';
75
+ await this.keyboardNavigateToTab({
76
+ tab: tab,
77
+ orientation: tabsOrientation
78
+ });
79
+ if (manualActivation) await this.user.keyboard('[Enter]');
80
+ } else await (0, $5d1eca18f92ad0e6$export$6ffa3eb717517feb)(this.user, tab, interactionType);
81
+ }
82
+ /**
83
+ * Returns the tablist.
84
+ */ get tablist() {
85
+ return this._tablist;
86
+ }
87
+ /**
88
+ * Returns the tabpanels.
89
+ */ get tabpanels() {
90
+ let tabpanels = [];
91
+ for (let tab of this.tabs){
92
+ let controlId = tab.getAttribute('aria-controls');
93
+ let panel = controlId != null ? document.getElementById(controlId) : null;
94
+ if (panel != null) tabpanels.push(panel);
95
+ }
96
+ return tabpanels;
97
+ }
98
+ /**
99
+ * Returns the tabs in the tablist.
100
+ */ get tabs() {
101
+ return (0, $im2y9$within)(this.tablist).queryAllByRole('tab');
102
+ }
103
+ /**
104
+ * Returns the currently selected tab in the tablist if any.
105
+ */ get selectedTab() {
106
+ return this.tabs.find((tab)=>tab.getAttribute('aria-selected') === 'true') || null;
107
+ }
108
+ /**
109
+ * Returns the currently active tabpanel if any.
110
+ */ get activeTabpanel() {
111
+ var _this_selectedTab;
112
+ let activeTabpanelId = (_this_selectedTab = this.selectedTab) === null || _this_selectedTab === void 0 ? void 0 : _this_selectedTab.getAttribute('aria-controls');
113
+ return activeTabpanelId ? document.getElementById(activeTabpanelId) : null;
114
+ }
115
+ constructor(opts){
116
+ let { root: root, user: user, interactionType: interactionType, direction: direction } = opts;
117
+ this.user = user;
118
+ this._interactionType = interactionType || 'mouse';
119
+ this._direction = direction || 'ltr';
120
+ this._tablist = root;
121
+ let tablist = (0, $im2y9$within)(root).queryAllByRole('tablist');
122
+ if (tablist.length > 0) this._tablist = tablist[0];
123
+ }
124
+ }
125
+
126
+
127
+ export {$e615c34a00448a49$export$f1539bff3fc7d485 as TabsTester};
128
+ //# sourceMappingURL=tabs.module.js.map
@@ -0,0 +1,128 @@
1
+ import {pressElement as $5d1eca18f92ad0e6$export$6ffa3eb717517feb} from "./events.module.js";
2
+ import {within as $im2y9$within, act as $im2y9$act} from "@testing-library/react";
3
+
4
+ /*
5
+ * Copyright 2024 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ class $e615c34a00448a49$export$f1539bff3fc7d485 {
17
+ /**
18
+ * Set the interaction type used by the tabs tester.
19
+ */ setInteractionType(type) {
20
+ this._interactionType = type;
21
+ }
22
+ // TODO: This is pretty similar across most the utils, refactor to make it generic?
23
+ /**
24
+ * Returns a tab matching the specified index or text content.
25
+ */ findTab(opts) {
26
+ let { tabIndexOrText: tabIndexOrText } = opts;
27
+ let tab;
28
+ let tabs = this.tabs;
29
+ if (typeof tabIndexOrText === 'number') tab = tabs[tabIndexOrText];
30
+ else if (typeof tabIndexOrText === 'string') tab = (0, $im2y9$within)(this._tablist).getByText(tabIndexOrText).closest('[role=tab]');
31
+ return tab;
32
+ }
33
+ // TODO: also quite similar across more utils albeit with orientation, refactor to make generic
34
+ async keyboardNavigateToTab(opts) {
35
+ let { tab: tab, orientation: orientation = 'vertical' } = opts;
36
+ let tabs = this.tabs;
37
+ let targetIndex = tabs.indexOf(tab);
38
+ if (targetIndex === -1) throw new Error('Tab provided is not in the tablist');
39
+ if (!this._tablist.contains(document.activeElement)) {
40
+ let selectedTab = this.selectedTab;
41
+ if (selectedTab != null) (0, $im2y9$act)(()=>selectedTab.focus());
42
+ else (0, $im2y9$act)(()=>{
43
+ var _tabs_find;
44
+ return (_tabs_find = tabs.find((tab)=>!(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'))) === null || _tabs_find === void 0 ? void 0 : _tabs_find.focus();
45
+ });
46
+ }
47
+ let currIndex = this.tabs.indexOf(document.activeElement);
48
+ if (currIndex === -1) throw new Error('ActiveElement is not in the tablist');
49
+ let arrowUp = 'ArrowUp';
50
+ let arrowDown = 'ArrowDown';
51
+ if (orientation === 'horizontal') {
52
+ if (this._direction === 'ltr') {
53
+ arrowUp = 'ArrowLeft';
54
+ arrowDown = 'ArrowRight';
55
+ } else {
56
+ arrowUp = 'ArrowRight';
57
+ arrowDown = 'ArrowLeft';
58
+ }
59
+ }
60
+ let movementDirection = targetIndex > currIndex ? 'down' : 'up';
61
+ for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${movementDirection === 'down' ? arrowDown : arrowUp}]`);
62
+ }
63
+ /**
64
+ * Triggers the specified tab. Defaults to using the interaction type set on the tabs tester.
65
+ */ async triggerTab(opts) {
66
+ let { tab: tab, interactionType: interactionType = this._interactionType, manualActivation: manualActivation } = opts;
67
+ if (typeof tab === 'string' || typeof tab === 'number') tab = this.findTab({
68
+ tabIndexOrText: tab
69
+ });
70
+ if (!tab) throw new Error('Target tab not found in the tablist.');
71
+ else if (tab.hasAttribute('disabled')) throw new Error('Target tab is disabled.');
72
+ if (interactionType === 'keyboard') {
73
+ if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) (0, $im2y9$act)(()=>this._tablist.focus());
74
+ let tabsOrientation = this._tablist.getAttribute('aria-orientation') || 'horizontal';
75
+ await this.keyboardNavigateToTab({
76
+ tab: tab,
77
+ orientation: tabsOrientation
78
+ });
79
+ if (manualActivation) await this.user.keyboard('[Enter]');
80
+ } else await (0, $5d1eca18f92ad0e6$export$6ffa3eb717517feb)(this.user, tab, interactionType);
81
+ }
82
+ /**
83
+ * Returns the tablist.
84
+ */ get tablist() {
85
+ return this._tablist;
86
+ }
87
+ /**
88
+ * Returns the tabpanels.
89
+ */ get tabpanels() {
90
+ let tabpanels = [];
91
+ for (let tab of this.tabs){
92
+ let controlId = tab.getAttribute('aria-controls');
93
+ let panel = controlId != null ? document.getElementById(controlId) : null;
94
+ if (panel != null) tabpanels.push(panel);
95
+ }
96
+ return tabpanels;
97
+ }
98
+ /**
99
+ * Returns the tabs in the tablist.
100
+ */ get tabs() {
101
+ return (0, $im2y9$within)(this.tablist).queryAllByRole('tab');
102
+ }
103
+ /**
104
+ * Returns the currently selected tab in the tablist if any.
105
+ */ get selectedTab() {
106
+ return this.tabs.find((tab)=>tab.getAttribute('aria-selected') === 'true') || null;
107
+ }
108
+ /**
109
+ * Returns the currently active tabpanel if any.
110
+ */ get activeTabpanel() {
111
+ var _this_selectedTab;
112
+ let activeTabpanelId = (_this_selectedTab = this.selectedTab) === null || _this_selectedTab === void 0 ? void 0 : _this_selectedTab.getAttribute('aria-controls');
113
+ return activeTabpanelId ? document.getElementById(activeTabpanelId) : null;
114
+ }
115
+ constructor(opts){
116
+ let { root: root, user: user, interactionType: interactionType, direction: direction } = opts;
117
+ this.user = user;
118
+ this._interactionType = interactionType || 'mouse';
119
+ this._direction = direction || 'ltr';
120
+ this._tablist = root;
121
+ let tablist = (0, $im2y9$within)(root).queryAllByRole('tablist');
122
+ if (tablist.length > 0) this._tablist = tablist[0];
123
+ }
124
+ }
125
+
126
+
127
+ export {$e615c34a00448a49$export$f1539bff3fc7d485 as TabsTester};
128
+ //# sourceMappingURL=tabs.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAqBM,MAAM;IAmBX;;GAEC,GACD,mBAAmB,IAAiC,EAAE;QACpD,IAAI,CAAC,gB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QAAQ,aAAa,OAAO,SAAS,cAAc,CAAC,aAAa;YACrE,IAAI,SAAS,MACX,UAAU,IAAI,CAAC;QAEnB;QAEA,OAAO;IACT;IAEA;;GAEC,GACD,IAAI,OAAsB;QACxB,OAAO,CAAA,GAAA,aAAK,EAAE,IAAI,CAAC,OAAO,EAAE,cAAc,CAAC;IAC7C;IAEA;;GAEC,GACD,IAAI,cAAkC;QACpC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,MAAO,IAAI,YAAY,CAAC,qBAAqB,WAAW;IAChF;IAEA;;GAEC,GACD,IAAI,iBAAqC;YAChB;QAAvB,IAAI,oBAAmB,oBAAA,IAAI,CAAC,WAAW,cAAhB,wCAAA,kBAAkB,YAAY,CAAC;QACtD,OAAO,mBAAmB,SAAS,cAAc,CAAC,oBAAoB;IACxE;IA/JA,YAAY,IAAoB,CAAE;QAChC,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,QAAQ,GAAG;QAChB,IAAI,UAAU,CAAA,GAAA,aAAK,EAAE,MAAM,cAAc,CAAC;QAC1C,IAAI,QAAQ,MAAM,GAAG,GACnB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,EAAE;IAE9B;AAqJF","sources":["packages/@react-aria/test-utils/src/tabs.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, within} from '@testing-library/react';\nimport {Direction, Orientation, TabsTesterOpts, UserOpts} from './types';\nimport {pressElement} from './events';\n\ninterface TriggerTabOptions {\n /**\n * What interaction type to use when triggering a tab. Defaults to the interaction type set on the tester.\n */\n interactionType?: UserOpts['interactionType'],\n /**\n * The index, text, or node of the tab to toggle selection for.\n */\n tab: number | string | HTMLElement,\n /**\n * Whether the tab needs to be activated manually rather than on focus.\n */\n manualActivation?: boolean\n}\n\nexport class TabsTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _tablist: HTMLElement;\n private _direction: Direction;\n\n constructor(opts: TabsTesterOpts) {\n let {root, user, interactionType, direction} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n this._direction = direction || 'ltr';\n\n this._tablist = root;\n let tablist = within(root).queryAllByRole('tablist');\n if (tablist.length > 0) {\n this._tablist = tablist[0];\n }\n }\n\n /**\n * Set the interaction type used by the tabs tester.\n */\n setInteractionType(type: UserOpts['interactionType']) {\n this._interactionType = type;\n }\n\n // TODO: This is pretty similar across most the utils, refactor to make it generic?\n /**\n * Returns a tab matching the specified index or text content.\n */\n findTab(opts: {tabIndexOrText: number | string}): HTMLElement {\n let {\n tabIndexOrText\n } = opts;\n\n let tab;\n let tabs = this.tabs;\n if (typeof tabIndexOrText === 'number') {\n tab = tabs[tabIndexOrText];\n } else if (typeof tabIndexOrText === 'string') {\n tab = (within(this._tablist).getByText(tabIndexOrText).closest('[role=tab]'))! as HTMLElement;\n }\n\n return tab;\n }\n\n // TODO: also quite similar across more utils albeit with orientation, refactor to make generic\n private async keyboardNavigateToTab(opts: {tab: HTMLElement, orientation?: Orientation}) {\n let {tab, orientation = 'vertical'} = opts;\n let tabs = this.tabs;\n let targetIndex = tabs.indexOf(tab);\n if (targetIndex === -1) {\n throw new Error('Tab provided is not in the tablist');\n }\n\n if (!this._tablist.contains(document.activeElement)) {\n let selectedTab = this.selectedTab;\n if (selectedTab != null) {\n act(() => selectedTab.focus());\n } else {\n act(() => tabs.find(tab => !(tab.hasAttribute('disabled') || tab.getAttribute('aria-disabled') === 'true'))?.focus());\n }\n }\n\n let currIndex = this.tabs.indexOf(document.activeElement as HTMLElement);\n if (currIndex === -1) {\n throw new Error('ActiveElement is not in the tablist');\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 tab. Defaults to using the interaction type set on the tabs tester.\n */\n async triggerTab(opts: TriggerTabOptions) {\n let {\n tab,\n interactionType = this._interactionType,\n manualActivation\n } = opts;\n\n if (typeof tab === 'string' || typeof tab === 'number') {\n tab = this.findTab({tabIndexOrText: tab});\n }\n\n if (!tab) {\n throw new Error('Target tab not found in the tablist.');\n } else if (tab.hasAttribute('disabled')) {\n throw new Error('Target tab is disabled.');\n }\n\n if (interactionType === 'keyboard') {\n if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) {\n act(() => this._tablist.focus());\n }\n\n let tabsOrientation = this._tablist.getAttribute('aria-orientation') || 'horizontal';\n await this.keyboardNavigateToTab({tab, orientation: tabsOrientation as Orientation});\n if (manualActivation) {\n await this.user.keyboard('[Enter]');\n }\n } else {\n await pressElement(this.user, tab, interactionType);\n }\n }\n\n /**\n * Returns the tablist.\n */\n get tablist(): HTMLElement {\n return this._tablist;\n }\n\n /**\n * Returns the tabpanels.\n */\n get tabpanels(): HTMLElement[] {\n let tabpanels = [] as HTMLElement[];\n for (let tab of this.tabs) {\n let controlId = tab.getAttribute('aria-controls');\n let panel = controlId != null ? document.getElementById(controlId) : null;\n if (panel != null) {\n tabpanels.push(panel);\n }\n }\n\n return tabpanels;\n }\n\n /**\n * Returns the tabs in the tablist.\n */\n get tabs(): HTMLElement[] {\n return within(this.tablist).queryAllByRole('tab');\n }\n\n /**\n * Returns the currently selected tab in the tablist if any.\n */\n get selectedTab(): HTMLElement | null {\n return this.tabs.find(tab => tab.getAttribute('aria-selected') === 'true') || null;\n }\n\n /**\n * Returns the currently active tabpanel if any.\n */\n get activeTabpanel(): HTMLElement | null {\n let activeTabpanelId = this.selectedTab?.getAttribute('aria-controls');\n return activeTabpanelId ? document.getElementById(activeTabpanelId) : null;\n }\n}\n"],"names":[],"version":3,"file":"tabs.module.js.map"}
@@ -0,0 +1,165 @@
1
+ var $5a8bfe1663b8366d$exports = require("./events.main.js");
2
+ var $cVFA5$testinglibraryreact = require("@testing-library/react");
3
+
4
+
5
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
+ }
8
+
9
+ $parcel$export(module.exports, "TreeTester", () => $9c221bde173322ba$export$4445864b14128396);
10
+ /*
11
+ * Copyright 2024 Adobe. All rights reserved.
12
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License. You may obtain a copy
14
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software distributed under
17
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
18
+ * OF ANY KIND, either express or implied. See the License for the specific language
19
+ * governing permissions and limitations under the License.
20
+ */
21
+
22
+ class $9c221bde173322ba$export$4445864b14128396 {
23
+ /**
24
+ * Set the interaction type used by the tree tester.
25
+ */ setInteractionType(type) {
26
+ this._interactionType = type;
27
+ }
28
+ /**
29
+ * Returns a row matching the specified index or text content.
30
+ */ findRow(opts) {
31
+ let { rowIndexOrText: rowIndexOrText } = opts;
32
+ let row;
33
+ if (typeof rowIndexOrText === 'number') row = this.rows[rowIndexOrText];
34
+ else if (typeof rowIndexOrText === 'string') row = (0, $cVFA5$testinglibraryreact.within)(this.tree).getByText(rowIndexOrText).closest('[role=row]');
35
+ return row;
36
+ }
37
+ // TODO: RTL
38
+ async keyboardNavigateToRow(opts) {
39
+ let { row: row } = opts;
40
+ let rows = this.rows;
41
+ let targetIndex = rows.indexOf(row);
42
+ if (targetIndex === -1) throw new Error('Option provided is not in the tree');
43
+ if (document.activeElement === this.tree) await this.user.keyboard('[ArrowDown]');
44
+ else if (this._tree.contains(document.activeElement) && document.activeElement.getAttribute('role') !== 'row') do await this.user.keyboard('[ArrowLeft]');
45
+ while (document.activeElement.getAttribute('role') !== 'row');
46
+ let currIndex = rows.indexOf(document.activeElement);
47
+ if (currIndex === -1) throw new Error('ActiveElement is not in the tree');
48
+ let direction = targetIndex > currIndex ? 'down' : 'up';
49
+ for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
50
+ }
51
+ /**
52
+ * Toggles the selection for the specified tree row. Defaults to using the interaction type set on the tree tester.
53
+ */ async toggleRowSelection(opts) {
54
+ let { row: row, needsLongPress: needsLongPress, checkboxSelection: checkboxSelection = true, interactionType: interactionType = this._interactionType } = opts;
55
+ if (typeof row === 'string' || typeof row === 'number') row = this.findRow({
56
+ rowIndexOrText: row
57
+ });
58
+ if (!row) throw new Error('Target row not found in the tree.');
59
+ let rowCheckbox = (0, $cVFA5$testinglibraryreact.within)(row).queryByRole('checkbox');
60
+ // TODO: we early return here because the checkbox can't be keyboard navigated to if the row is disabled usually
61
+ // but we may to check for disabledBehavior (aka if the disable row gets skipped when keyboard navigating or not)
62
+ if (interactionType === 'keyboard' && ((rowCheckbox === null || rowCheckbox === void 0 ? void 0 : rowCheckbox.getAttribute('disabled')) === '' || (row === null || row === void 0 ? void 0 : row.getAttribute('aria-disabled')) === 'true')) return;
63
+ // this would be better than the check to do nothing in events.ts
64
+ // also, it'd be good to be able to trigger selection on the row instead of having to go to the checkbox directly
65
+ if (interactionType === 'keyboard' && !checkboxSelection) {
66
+ await this.keyboardNavigateToRow({
67
+ row: row
68
+ });
69
+ await this.user.keyboard('{Space}');
70
+ return;
71
+ }
72
+ if (rowCheckbox && checkboxSelection) await (0, $5a8bfe1663b8366d$exports.pressElement)(this.user, rowCheckbox, interactionType);
73
+ else {
74
+ let cell = (0, $cVFA5$testinglibraryreact.within)(row).getAllByRole('gridcell')[0];
75
+ if (needsLongPress && interactionType === 'touch') {
76
+ if (this._advanceTimer == null) throw new Error('No advanceTimers provided for long press.');
77
+ // Note that long press interactions with rows is strictly touch only for grid rows
78
+ await (0, $5a8bfe1663b8366d$exports.triggerLongPress)({
79
+ element: cell,
80
+ advanceTimer: this._advanceTimer,
81
+ pointerOpts: {
82
+ pointerType: 'touch'
83
+ }
84
+ });
85
+ } else await (0, $5a8bfe1663b8366d$exports.pressElement)(this.user, cell, interactionType);
86
+ }
87
+ }
88
+ /**
89
+ * Toggles the expansion for the specified tree row. Defaults to using the interaction type set on the tree tester.
90
+ */ async toggleRowExpansion(opts) {
91
+ let { row: row, interactionType: interactionType = this._interactionType } = opts;
92
+ if (!this.tree.contains(document.activeElement)) await (0, $cVFA5$testinglibraryreact.act)(async ()=>{
93
+ this.tree.focus();
94
+ });
95
+ if (typeof row === 'string' || typeof row === 'number') row = this.findRow({
96
+ rowIndexOrText: row
97
+ });
98
+ if (!row) throw new Error('Target row not found in the tree.');
99
+ else if (row.getAttribute('aria-expanded') == null) throw new Error('Target row is not expandable.');
100
+ if (interactionType === 'mouse' || interactionType === 'touch') {
101
+ let rowExpander = (0, $cVFA5$testinglibraryreact.within)(row).getAllByRole('button')[0]; // what happens if the button is not first? how can we differentiate?
102
+ await (0, $5a8bfe1663b8366d$exports.pressElement)(this.user, rowExpander, interactionType);
103
+ } else if (interactionType === 'keyboard') {
104
+ if ((row === null || row === void 0 ? void 0 : row.getAttribute('aria-disabled')) === 'true') return;
105
+ await this.keyboardNavigateToRow({
106
+ row: row
107
+ });
108
+ if (row.getAttribute('aria-expanded') === 'true') await this.user.keyboard('[ArrowLeft]');
109
+ else await this.user.keyboard('[ArrowRight]');
110
+ }
111
+ }
112
+ /**
113
+ * Triggers the action for the specified tree row. Defaults to using the interaction type set on the tree tester.
114
+ */ async triggerRowAction(opts) {
115
+ let { row: row, needsDoubleClick: needsDoubleClick, interactionType: interactionType = this._interactionType } = opts;
116
+ if (typeof row === 'string' || typeof row === 'number') row = this.findRow({
117
+ rowIndexOrText: row
118
+ });
119
+ if (!row) throw new Error('Target row not found in the tree.');
120
+ if (needsDoubleClick) await this.user.dblClick(row);
121
+ else if (interactionType === 'keyboard') {
122
+ if ((row === null || row === void 0 ? void 0 : row.getAttribute('aria-disabled')) === 'true') return;
123
+ if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) (0, $cVFA5$testinglibraryreact.act)(()=>this._tree.focus());
124
+ await this.keyboardNavigateToRow({
125
+ row: row
126
+ });
127
+ await this.user.keyboard('[Enter]');
128
+ } else await (0, $5a8bfe1663b8366d$exports.pressElement)(this.user, row, interactionType);
129
+ }
130
+ /**
131
+ * Returns the tree.
132
+ */ get tree() {
133
+ return this._tree;
134
+ }
135
+ /**
136
+ * Returns the tree's rows if any.
137
+ */ get rows() {
138
+ var _this;
139
+ return (0, $cVFA5$testinglibraryreact.within)((_this = this) === null || _this === void 0 ? void 0 : _this.tree).queryAllByRole('row');
140
+ }
141
+ /**
142
+ * Returns the tree's selected rows if any.
143
+ */ get selectedRows() {
144
+ return this.rows.filter((row)=>row.getAttribute('aria-selected') === 'true');
145
+ }
146
+ /**
147
+ * Returns the tree's cells if any. Can be filtered against a specific row if provided via `element`.
148
+ */ cells(opts = {}) {
149
+ let { element: element = this.tree } = opts;
150
+ return (0, $cVFA5$testinglibraryreact.within)(element).queryAllByRole('gridcell');
151
+ }
152
+ constructor(opts){
153
+ let { root: root, user: user, interactionType: interactionType, advanceTimer: advanceTimer } = opts;
154
+ this.user = user;
155
+ this._interactionType = interactionType || 'mouse';
156
+ this._advanceTimer = advanceTimer;
157
+ this._tree = root;
158
+ // TODO: should all helpers do this?
159
+ let tree = (0, $cVFA5$testinglibraryreact.within)(root).queryByRole('treegrid');
160
+ if (root.getAttribute('role') !== 'treegrid' && tree) this._tree = tree;
161
+ }
162
+ }
163
+
164
+
165
+ //# sourceMappingURL=tree.main.js.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAWM,MAAM;IAmBX;;GAEC,GACD,mBAAmB,IAAiC,EAAE;QACpD,IAAI,CAAC,gBAAgB,GAAG;IAC1B;IAEA;;GAEC,GACD,QAAQ,IAAuC,EAAe;QAC5D,IAAI,kBACF,cAAc,EACf,GAAG;QAEJ,IAAI;QACJ,IAAI,OAAO,mBAAmB,UAC5B,MAAM,IAAI,CAAC,IAAI,CAAC,eAAe;aAC1B,IAAI,OAAO,mBAAmB,UACnC,MAAO,CAAA,GAAA,iCAAK,EAAE,IAAI,CAAC,IAAI,EAAG,SAAS,CAAC,gBAAgB,OAAO,CAAC;QAG9D,OAAO;IACT;IAEA,YAAY;IACZ,MAAc,sBAAsB,IAAwB,EAAE;QAC5D,IAAI,OAAC,GAAG,EAAC,GAAG;QACZ,IAAI,OAAO,IAAI,CAAC,IAAI;QACpB,IAAI,cAAc,KAAK,OAAO,CAAC;QAC/B,IAAI,gBAAgB,IAClB,MAAM,IAAI,MAAM;QAElB,IAAI,SAAS,aAAa,KAAK,IAAI,CAAC,IAAI,EACtC,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;aACpB,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,SAAS,aAAa,KAAK,SAAS,aAAa,CAAE,YAAY,CAAC,YAAY,OACzG,GACE,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;eAClB,SAAS,aAAa,CAAE,YAAY,CAAC,YAAY,OAAO;QAEnE,IAAI,YAAY,K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,CAAA,GAAA,iCAAK,EAAE,SAAS,cAAc,CAAC;IACxC;IA3NA,YAAY,IAAoB,CAAE;QAChC,IAAI,QAAC,IAAI,QAAE,IAAI,mBAAE,eAAe,gBAAE,YAAY,EAAC,GAAG;QAClD,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,gBAAgB,GAAG,mBAAmB;QAC3C,IAAI,CAAC,aAAa,GAAG;QACrB,IAAI,CAAC,KAAK,GAAG;QACb,oCAAoC;QACpC,IAAI,OAAO,CAAA,GAAA,iCAAK,EAAE,MAAM,WAAW,CAAC;QACpC,IAAI,KAAK,YAAY,CAAC,YAAY,cAAc,MAC9C,IAAI,CAAC,KAAK,GAAG;IAEjB;AAiNF","sources":["packages/@react-aria/test-utils/src/tree.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, within} from '@testing-library/react';\nimport {BaseGridRowInteractionOpts, GridRowActionOpts, ToggleGridRowOpts, TreeTesterOpts, UserOpts} from './types';\nimport {pressElement, triggerLongPress} from './events';\n\ninterface TreeToggleExpansionOpts extends BaseGridRowInteractionOpts {}\ninterface TreeToggleRowOpts extends ToggleGridRowOpts {}\ninterface TreeRowActionOpts extends GridRowActionOpts {}\n\n// TODO: this ended up being pretty much the same as gridlist, refactor so it extends from gridlist\nexport class TreeTester {\n private user;\n private _interactionType: UserOpts['interactionType'];\n private _advanceTimer: UserOpts['advanceTimer'];\n private _tree: HTMLElement;\n\n constructor(opts: TreeTesterOpts) {\n let {root, user, interactionType, advanceTimer} = opts;\n this.user = user;\n this._interactionType = interactionType || 'mouse';\n this._advanceTimer = advanceTimer;\n this._tree = root;\n // TODO: should all helpers do this?\n let tree = within(root).queryByRole('treegrid');\n if (root.getAttribute('role') !== 'treegrid' && tree) {\n this._tree = tree;\n }\n }\n\n /**\n * Set the interaction type used by the tree tester.\n */\n setInteractionType(type: UserOpts['interactionType']) {\n this._interactionType = type;\n };\n\n /**\n * Returns a row matching the specified index or text content.\n */\n findRow(opts: {rowIndexOrText: number | string}): HTMLElement {\n let {\n rowIndexOrText\n } = opts;\n\n let row;\n if (typeof rowIndexOrText === 'number') {\n row = this.rows[rowIndexOrText];\n } else if (typeof rowIndexOrText === 'string') {\n row = (within(this.tree!).getByText(rowIndexOrText).closest('[role=row]'))! as HTMLElement;\n }\n\n return row;\n }\n\n // TODO: RTL\n private async keyboardNavigateToRow(opts: {row: HTMLElement}) {\n let {row} = opts;\n let rows = this.rows;\n let targetIndex = rows.indexOf(row);\n if (targetIndex === -1) {\n throw new Error('Option provided is not in the tree');\n }\n if (document.activeElement === this.tree) {\n await this.user.keyboard('[ArrowDown]');\n } else if (this._tree.contains(document.activeElement) && document.activeElement!.getAttribute('role') !== 'row') {\n do {\n await this.user.keyboard('[ArrowLeft]');\n } while (document.activeElement!.getAttribute('role') !== 'row');\n }\n let currIndex = rows.indexOf(document.activeElement as HTMLElement);\n if (currIndex === -1) {\n throw new Error('ActiveElement is not in the tree');\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 for the specified tree row. Defaults to using the interaction type set on the tree tester.\n */\n async toggleRowSelection(opts: TreeToggleRowOpts) {\n let {\n row,\n needsLongPress,\n checkboxSelection = true,\n interactionType = this._interactionType\n } = opts;\n\n if (typeof row === 'string' || typeof row === 'number') {\n row = this.findRow({rowIndexOrText: row});\n }\n\n if (!row) {\n throw new Error('Target row not found in the tree.');\n }\n\n let rowCheckbox = within(row).queryByRole('checkbox');\n\n // TODO: we early return here because the checkbox can't be keyboard navigated to if the row is disabled usually\n // but we may to check for disabledBehavior (aka if the disable row gets skipped when keyboard navigating or not)\n if (interactionType === 'keyboard' && (rowCheckbox?.getAttribute('disabled') === '' || row?.getAttribute('aria-disabled') === 'true')) {\n return;\n }\n\n // this would be better than the check to do nothing in events.ts\n // also, it'd be good to be able to trigger selection on the row instead of having to go to the checkbox directly\n if (interactionType === 'keyboard' && !checkboxSelection) {\n await this.keyboardNavigateToRow({row});\n await this.user.keyboard('{Space}');\n return;\n }\n if (rowCheckbox && checkboxSelection) {\n await pressElement(this.user, rowCheckbox, interactionType);\n } else {\n let cell = within(row).getAllByRole('gridcell')[0];\n if (needsLongPress && interactionType === 'touch') {\n if (this._advanceTimer == null) {\n throw new Error('No advanceTimers provided for long press.');\n }\n\n // Note that long press interactions with rows is strictly touch only for grid rows\n await triggerLongPress({element: cell, advanceTimer: this._advanceTimer, pointerOpts: {pointerType: 'touch'}});\n } else {\n await pressElement(this.user, cell, interactionType);\n }\n }\n };\n\n /**\n * Toggles the expansion for the specified tree row. Defaults to using the interaction type set on the tree tester.\n */\n async toggleRowExpansion(opts: TreeToggleExpansionOpts) {\n let {\n row,\n interactionType = this._interactionType\n } = opts;\n if (!this.tree.contains(document.activeElement)) {\n await act(async () => {\n this.tree.focus();\n });\n }\n\n if (typeof row === 'string' || typeof row === 'number') {\n row = this.findRow({rowIndexOrText: row});\n }\n\n if (!row) {\n throw new Error('Target row not found in the tree.');\n } else if (row.getAttribute('aria-expanded') == null) {\n throw new Error('Target row is not expandable.');\n }\n\n if (interactionType === 'mouse' || interactionType === 'touch') {\n let rowExpander = within(row).getAllByRole('button')[0]; // what happens if the button is not first? how can we differentiate?\n await pressElement(this.user, rowExpander, interactionType);\n } else if (interactionType === 'keyboard') {\n if (row?.getAttribute('aria-disabled') === 'true') {\n return;\n }\n\n await this.keyboardNavigateToRow({row});\n if (row.getAttribute('aria-expanded') === 'true') {\n await this.user.keyboard('[ArrowLeft]');\n } else {\n await this.user.keyboard('[ArrowRight]');\n }\n }\n };\n\n /**\n * Triggers the action for the specified tree row. Defaults to using the interaction type set on the tree tester.\n */\n async triggerRowAction(opts: TreeRowActionOpts) {\n let {\n row,\n needsDoubleClick,\n interactionType = this._interactionType\n } = opts;\n\n if (typeof row === 'string' || typeof row === 'number') {\n row = this.findRow({rowIndexOrText: row});\n }\n\n if (!row) {\n throw new Error('Target row not found in the tree.');\n }\n\n if (needsDoubleClick) {\n await this.user.dblClick(row);\n } else if (interactionType === 'keyboard') {\n if (row?.getAttribute('aria-disabled') === 'true') {\n return;\n }\n\n if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) {\n act(() => this._tree.focus());\n }\n\n await this.keyboardNavigateToRow({row});\n await this.user.keyboard('[Enter]');\n } else {\n await pressElement(this.user, row, interactionType);\n }\n };\n\n /**\n * Returns the tree.\n */\n get tree(): HTMLElement {\n return this._tree;\n }\n\n /**\n * Returns the tree's rows if any.\n */\n get rows(): HTMLElement[] {\n return within(this?.tree).queryAllByRole('row');\n }\n\n /**\n * Returns the tree's selected rows if any.\n */\n get selectedRows(): HTMLElement[] {\n return this.rows.filter(row => row.getAttribute('aria-selected') === 'true');\n }\n\n /**\n * Returns the tree's cells if any. Can be filtered against a specific row if provided via `element`.\n */\n cells(opts: {element?: HTMLElement} = {}): HTMLElement[] {\n let {element = this.tree} = opts;\n return within(element).queryAllByRole('gridcell');\n }\n}\n"],"names":[],"version":3,"file":"tree.main.js.map"}
package/dist/tree.mjs ADDED
@@ -0,0 +1,160 @@
1
+ import {pressElement as $5d1eca18f92ad0e6$export$6ffa3eb717517feb, triggerLongPress as $5d1eca18f92ad0e6$export$3a22a5a9bc0fd3b} from "./events.mjs";
2
+ import {within as $3mnbZ$within, act as $3mnbZ$act} from "@testing-library/react";
3
+
4
+ /*
5
+ * Copyright 2024 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ class $962758aa2f16f1a4$export$4445864b14128396 {
17
+ /**
18
+ * Set the interaction type used by the tree tester.
19
+ */ setInteractionType(type) {
20
+ this._interactionType = type;
21
+ }
22
+ /**
23
+ * Returns a row matching the specified index or text content.
24
+ */ findRow(opts) {
25
+ let { rowIndexOrText: rowIndexOrText } = opts;
26
+ let row;
27
+ if (typeof rowIndexOrText === 'number') row = this.rows[rowIndexOrText];
28
+ else if (typeof rowIndexOrText === 'string') row = (0, $3mnbZ$within)(this.tree).getByText(rowIndexOrText).closest('[role=row]');
29
+ return row;
30
+ }
31
+ // TODO: RTL
32
+ async keyboardNavigateToRow(opts) {
33
+ let { row: row } = opts;
34
+ let rows = this.rows;
35
+ let targetIndex = rows.indexOf(row);
36
+ if (targetIndex === -1) throw new Error('Option provided is not in the tree');
37
+ if (document.activeElement === this.tree) await this.user.keyboard('[ArrowDown]');
38
+ else if (this._tree.contains(document.activeElement) && document.activeElement.getAttribute('role') !== 'row') do await this.user.keyboard('[ArrowLeft]');
39
+ while (document.activeElement.getAttribute('role') !== 'row');
40
+ let currIndex = rows.indexOf(document.activeElement);
41
+ if (currIndex === -1) throw new Error('ActiveElement is not in the tree');
42
+ let direction = targetIndex > currIndex ? 'down' : 'up';
43
+ for(let i = 0; i < Math.abs(targetIndex - currIndex); i++)await this.user.keyboard(`[${direction === 'down' ? 'ArrowDown' : 'ArrowUp'}]`);
44
+ }
45
+ /**
46
+ * Toggles the selection for the specified tree row. Defaults to using the interaction type set on the tree tester.
47
+ */ async toggleRowSelection(opts) {
48
+ let { row: row, needsLongPress: needsLongPress, checkboxSelection: checkboxSelection = true, interactionType: interactionType = this._interactionType } = opts;
49
+ if (typeof row === 'string' || typeof row === 'number') row = this.findRow({
50
+ rowIndexOrText: row
51
+ });
52
+ if (!row) throw new Error('Target row not found in the tree.');
53
+ let rowCheckbox = (0, $3mnbZ$within)(row).queryByRole('checkbox');
54
+ // TODO: we early return here because the checkbox can't be keyboard navigated to if the row is disabled usually
55
+ // but we may to check for disabledBehavior (aka if the disable row gets skipped when keyboard navigating or not)
56
+ if (interactionType === 'keyboard' && ((rowCheckbox === null || rowCheckbox === void 0 ? void 0 : rowCheckbox.getAttribute('disabled')) === '' || (row === null || row === void 0 ? void 0 : row.getAttribute('aria-disabled')) === 'true')) return;
57
+ // this would be better than the check to do nothing in events.ts
58
+ // also, it'd be good to be able to trigger selection on the row instead of having to go to the checkbox directly
59
+ if (interactionType === 'keyboard' && !checkboxSelection) {
60
+ await this.keyboardNavigateToRow({
61
+ row: row
62
+ });
63
+ await this.user.keyboard('{Space}');
64
+ return;
65
+ }
66
+ if (rowCheckbox && checkboxSelection) await (0, $5d1eca18f92ad0e6$export$6ffa3eb717517feb)(this.user, rowCheckbox, interactionType);
67
+ else {
68
+ let cell = (0, $3mnbZ$within)(row).getAllByRole('gridcell')[0];
69
+ if (needsLongPress && interactionType === 'touch') {
70
+ if (this._advanceTimer == null) throw new Error('No advanceTimers provided for long press.');
71
+ // Note that long press interactions with rows is strictly touch only for grid rows
72
+ await (0, $5d1eca18f92ad0e6$export$3a22a5a9bc0fd3b)({
73
+ element: cell,
74
+ advanceTimer: this._advanceTimer,
75
+ pointerOpts: {
76
+ pointerType: 'touch'
77
+ }
78
+ });
79
+ } else await (0, $5d1eca18f92ad0e6$export$6ffa3eb717517feb)(this.user, cell, interactionType);
80
+ }
81
+ }
82
+ /**
83
+ * Toggles the expansion for the specified tree row. Defaults to using the interaction type set on the tree tester.
84
+ */ async toggleRowExpansion(opts) {
85
+ let { row: row, interactionType: interactionType = this._interactionType } = opts;
86
+ if (!this.tree.contains(document.activeElement)) await (0, $3mnbZ$act)(async ()=>{
87
+ this.tree.focus();
88
+ });
89
+ if (typeof row === 'string' || typeof row === 'number') row = this.findRow({
90
+ rowIndexOrText: row
91
+ });
92
+ if (!row) throw new Error('Target row not found in the tree.');
93
+ else if (row.getAttribute('aria-expanded') == null) throw new Error('Target row is not expandable.');
94
+ if (interactionType === 'mouse' || interactionType === 'touch') {
95
+ let rowExpander = (0, $3mnbZ$within)(row).getAllByRole('button')[0]; // what happens if the button is not first? how can we differentiate?
96
+ await (0, $5d1eca18f92ad0e6$export$6ffa3eb717517feb)(this.user, rowExpander, interactionType);
97
+ } else if (interactionType === 'keyboard') {
98
+ if ((row === null || row === void 0 ? void 0 : row.getAttribute('aria-disabled')) === 'true') return;
99
+ await this.keyboardNavigateToRow({
100
+ row: row
101
+ });
102
+ if (row.getAttribute('aria-expanded') === 'true') await this.user.keyboard('[ArrowLeft]');
103
+ else await this.user.keyboard('[ArrowRight]');
104
+ }
105
+ }
106
+ /**
107
+ * Triggers the action for the specified tree row. Defaults to using the interaction type set on the tree tester.
108
+ */ async triggerRowAction(opts) {
109
+ let { row: row, needsDoubleClick: needsDoubleClick, interactionType: interactionType = this._interactionType } = opts;
110
+ if (typeof row === 'string' || typeof row === 'number') row = this.findRow({
111
+ rowIndexOrText: row
112
+ });
113
+ if (!row) throw new Error('Target row not found in the tree.');
114
+ if (needsDoubleClick) await this.user.dblClick(row);
115
+ else if (interactionType === 'keyboard') {
116
+ if ((row === null || row === void 0 ? void 0 : row.getAttribute('aria-disabled')) === 'true') return;
117
+ if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) (0, $3mnbZ$act)(()=>this._tree.focus());
118
+ await this.keyboardNavigateToRow({
119
+ row: row
120
+ });
121
+ await this.user.keyboard('[Enter]');
122
+ } else await (0, $5d1eca18f92ad0e6$export$6ffa3eb717517feb)(this.user, row, interactionType);
123
+ }
124
+ /**
125
+ * Returns the tree.
126
+ */ get tree() {
127
+ return this._tree;
128
+ }
129
+ /**
130
+ * Returns the tree's rows if any.
131
+ */ get rows() {
132
+ var _this;
133
+ return (0, $3mnbZ$within)((_this = this) === null || _this === void 0 ? void 0 : _this.tree).queryAllByRole('row');
134
+ }
135
+ /**
136
+ * Returns the tree's selected rows if any.
137
+ */ get selectedRows() {
138
+ return this.rows.filter((row)=>row.getAttribute('aria-selected') === 'true');
139
+ }
140
+ /**
141
+ * Returns the tree's cells if any. Can be filtered against a specific row if provided via `element`.
142
+ */ cells(opts = {}) {
143
+ let { element: element = this.tree } = opts;
144
+ return (0, $3mnbZ$within)(element).queryAllByRole('gridcell');
145
+ }
146
+ constructor(opts){
147
+ let { root: root, user: user, interactionType: interactionType, advanceTimer: advanceTimer } = opts;
148
+ this.user = user;
149
+ this._interactionType = interactionType || 'mouse';
150
+ this._advanceTimer = advanceTimer;
151
+ this._tree = root;
152
+ // TODO: should all helpers do this?
153
+ let tree = (0, $3mnbZ$within)(root).queryByRole('treegrid');
154
+ if (root.getAttribute('role') !== 'treegrid' && tree) this._tree = tree;
155
+ }
156
+ }
157
+
158
+
159
+ export {$962758aa2f16f1a4$export$4445864b14128396 as TreeTester};
160
+ //# sourceMappingURL=tree.module.js.map