Skip to content

Commit c42ace5

Browse files
committed
update test utils to make sure grid/tree/etc is focused before doing keyboard selection
1 parent 2df4b10 commit c42ace5

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

packages/@react-aria/test-utils/src/gridlist.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export class GridListTester {
6464
if (targetIndex === -1) {
6565
throw new Error('Option provided is not in the gridlist');
6666
}
67+
68+
if (document.activeElement !== this._gridlist || !this._gridlist.contains(document.activeElement)) {
69+
act(() => this._gridlist.focus());
70+
}
71+
6772
if (document.activeElement === this._gridlist) {
6873
await this.user.keyboard('[ArrowDown]');
6974
} else if (this._gridlist.contains(document.activeElement) && document.activeElement!.getAttribute('role') !== 'row') {
@@ -161,10 +166,6 @@ export class GridListTester {
161166
return;
162167
}
163168

164-
if (document.activeElement !== this._gridlist || !this._gridlist.contains(document.activeElement)) {
165-
act(() => this._gridlist.focus());
166-
}
167-
168169
await this.keyboardNavigateToRow({row});
169170
await this.user.keyboard('[Enter]');
170171
} else {

packages/@react-aria/test-utils/src/listbox.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export class ListBoxTester {
9393
throw new Error('Option provided is not in the listbox');
9494
}
9595

96+
if (document.activeElement !== this._listbox || !this._listbox.contains(document.activeElement)) {
97+
act(() => this._listbox.focus());
98+
}
99+
96100
if (document.activeElement === this._listbox) {
97101
await this.user.keyboard('[ArrowDown]');
98102
}
@@ -135,10 +139,6 @@ export class ListBoxTester {
135139
return;
136140
}
137141

138-
if (document.activeElement !== this._listbox || !this._listbox.contains(document.activeElement)) {
139-
act(() => this._listbox.focus());
140-
}
141-
142142
await this.keyboardNavigateToOption({option});
143143
await this.user.keyboard(`[${keyboardActivation}]`);
144144
} else {
@@ -179,10 +179,6 @@ export class ListBoxTester {
179179
return;
180180
}
181181

182-
if (document.activeElement !== this._listbox || !this._listbox.contains(document.activeElement)) {
183-
act(() => this._listbox.focus());
184-
}
185-
186182
await this.keyboardNavigateToOption({option});
187183
await this.user.keyboard('[Enter]');
188184
} else {

packages/@react-aria/test-utils/src/tabs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export class TabsTester {
8484
throw new Error('Tab provided is not in the tablist');
8585
}
8686

87+
if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) {
88+
act(() => this._tablist.focus());
89+
}
90+
8791
if (!this._tablist.contains(document.activeElement)) {
8892
let selectedTab = this.selectedTab;
8993
if (selectedTab != null) {
@@ -137,10 +141,6 @@ export class TabsTester {
137141
}
138142

139143
if (interactionType === 'keyboard') {
140-
if (document.activeElement !== this._tablist || !this._tablist.contains(document.activeElement)) {
141-
act(() => this._tablist.focus());
142-
}
143-
144144
let tabsOrientation = this._tablist.getAttribute('aria-orientation') || 'horizontal';
145145
await this.keyboardNavigateToTab({tab, orientation: tabsOrientation as Orientation});
146146
if (manualActivation) {

packages/@react-aria/test-utils/src/tree.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export class TreeTester {
7171
if (targetIndex === -1) {
7272
throw new Error('Option provided is not in the tree');
7373
}
74+
75+
if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) {
76+
act(() => this._tree.focus());
77+
}
78+
7479
if (document.activeElement === this.tree) {
7580
await this.user.keyboard('[ArrowDown]');
7681
} else if (this._tree.contains(document.activeElement) && document.activeElement!.getAttribute('role') !== 'row') {
@@ -79,7 +84,6 @@ export class TreeTester {
7984
} while (document.activeElement!.getAttribute('role') !== 'row');
8085
}
8186
let currIndex = rows.indexOf(document.activeElement as HTMLElement);
82-
console.log('document', document.activeElement?.outerHTML)
8387
if (currIndex === -1) {
8488
throw new Error('ActiveElement is not in the tree');
8589
}
@@ -90,6 +94,8 @@ export class TreeTester {
9094
}
9195
};
9296

97+
// TODO: we'll need to support selectionBehavior="replace", where clicks/keyboard will need to go through different flows
98+
// for both single selection and multiple selection due to the nature of the selection replacement on focus
9399
/**
94100
* Toggles the selection for the specified tree row. Defaults to using the interaction type set on the tree tester.
95101
*/
@@ -120,11 +126,6 @@ export class TreeTester {
120126
// this would be better than the check to do nothing in events.ts
121127
// also, it'd be good to be able to trigger selection on the row instead of having to go to the checkbox directly
122128
if (interactionType === 'keyboard' && !checkboxSelection) {
123-
// TODO: probable should just call this in keyboardNavigateToRow everytime, adjust this for all the utils
124-
if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) {
125-
act(() => this._tree.focus());
126-
}
127-
128129
await this.keyboardNavigateToRow({row});
129130
await this.user.keyboard('{Space}');
130131
return;
@@ -212,10 +213,6 @@ export class TreeTester {
212213
return;
213214
}
214215

215-
if (document.activeElement !== this._tree || !this._tree.contains(document.activeElement)) {
216-
act(() => this._tree.focus());
217-
}
218-
219216
await this.keyboardNavigateToRow({row});
220217
await this.user.keyboard('[Enter]');
221218
} else {

packages/@react-spectrum/tree/test/TreeView.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@ describe('Tree', () => {
524524
expect(rows[0]).toHaveAttribute('aria-selected', 'true');
525525
});
526526

527+
// TODO: replace the test above this one with a similar set up as the below
527528
it('should perform selection for highlight mode with single selection', async () => {
528529
let {getByRole} = render(<StaticTree treeProps={{selectionMode: 'single', selectionStyle: 'highlight'}} />);
529530
let treeTester = testUtilUser.createTester('Tree', {user, root: getByRole('treegrid')});
530-
expect(treeTester.tree).toHaveAttribute('aria-multiselectable', 'true');
531531
let rows = treeTester.rows;
532532

533533
for (let row of treeTester.rows) {

0 commit comments

Comments
 (0)