Skip to content

chore: Update test util doc examples so they don't error out #7612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/@react-aria/test-utils/src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class MenuTester {
let menu = this.menu;
if (menu) {
if (typeof submenuTrigger === 'string') {
submenuTrigger = within(menu).getByText(submenuTrigger);
submenuTrigger = (within(menu!).getByText(submenuTrigger).closest('[role=menuitem]'))! as HTMLElement;
}

let submenuTriggerTester = new MenuTester({user: this.user, interactionType: this._interactionType, root: submenuTrigger, isSubmenu: true});
Expand All @@ -292,7 +292,8 @@ export class MenuTester {
private async keyboardNavigateToOption(opts: {option: HTMLElement}) {
let {option} = opts;
let options = this.options();
let targetIndex = options.indexOf(option);
let targetIndex = options.findIndex(opt => (opt === option) || opt.contains(option));

if (targetIndex === -1) {
throw new Error('Option provided is not in the menu');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-aria/test-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface UserOpts {

export interface BaseTesterOpts extends UserOpts {
/** @private */
user: any,
user?: any,
/** The base element for the given tester (e.g. the table, menu trigger button, etc). */
root: HTMLElement
}
Expand Down
7 changes: 2 additions & 5 deletions packages/@react-spectrum/list/docs/ListView.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1211,25 +1211,22 @@ it('ListView can select a row via keyboard', async function () {
// Render your test component/app and initialize the gridlist tester
let {getByTestId} = render(
<Provider theme={defaultTheme}>
<ListView data-testid="test-gridlist">
<ListView data-testid="test-gridlist" selectionMode="single">
...
</ListView>
</Provider>
);
let gridlistTester = testUtilUser.createTester('GridList', {root: getByTestId('test-gridlist'), interactionType: 'keyboard'});
let gridListTester = testUtilUser.createTester('GridList', {root: getByTestId('test-gridlist'), interactionType: 'keyboard'});

let row = gridListTester.rows[0];
expect(row).not.toHaveClass('selected');
expect(within(row).getByRole('checkbox')).not.toBeChecked();
expect(gridListTester.selectedRows).toHaveLength(0);

await gridListTester.toggleRowSelection({row: 0});
expect(row).toHaveClass('selected');
expect(within(row).getByRole('checkbox')).toBeChecked();
expect(gridListTester.selectedRows).toHaveLength(1);

await gridListTester.toggleRowSelection({row: 0});
expect(row).not.toHaveClass('selected');
expect(within(row).getByRole('checkbox')).not.toBeChecked();
expect(gridListTester.selectedRows).toHaveLength(0);
});
Expand Down
3 changes: 1 addition & 2 deletions packages/@react-spectrum/listbox/docs/ListBox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ it('ListBox can select an option via keyboard', async function () {
// Render your test component/app and initialize the listbox tester
let {getByTestId} = render(
<Provider theme={defaultTheme}>
<ListBox data-testid="test-listbox">
<ListBox selectionMode="single" data-testid="test-listbox">
...
</ListBox>
</Provider>
Expand All @@ -438,7 +438,6 @@ it('ListBox can select an option via keyboard', async function () {

await listboxTester.toggleOptionSelection({option: 4});
expect(listboxTester.options()[4]).toHaveAttribute('aria-selected', 'true');
expect(onSelectionChange).toBeCalledTimes(1);
});
```

Expand Down
9 changes: 5 additions & 4 deletions packages/@react-spectrum/menu/docs/MenuTrigger.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ it('Menu can open its submenu via keyboard', async function () {
// Render your test component/app and initialize the menu tester
let {getByTestId} = render(
<Provider theme={defaultTheme}>
<MenuTrigger data-testid="test-menutrigger">
<MenuTrigger>
<Button data-testid="test-menutrigger">Menu trigger</Button>
...
</MenuTrigger>
</Provider>
Expand All @@ -291,9 +292,9 @@ it('Menu can open its submenu via keyboard', async function () {
let submenuTester = await menuTester.openSubmenu({submenuTrigger: 'Share…'});
expect(submenuTester.menu).toBeInTheDocument();

await submenuTester.selectOption({option: submenuUtil.options()[0]});
expect(submenuTester.menu).toBeInTheDocument();
expect(menuTester.menu).toBeInTheDocument();
await submenuTester.selectOption({option: submenuTester.options()[0]});
expect(submenuTester.menu).not.toBeInTheDocument();
expect(menuTester.menu).not.toBeInTheDocument();
});
```

Expand Down
3 changes: 1 addition & 2 deletions packages/@react-spectrum/picker/docs/Picker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,7 @@ it('Picker can select an option via keyboard', async function () {
);
let selectTester = testUtilUser.createTester('Select', {root: getByTestId('test-select'), interactionType: 'keyboard'});
let trigger = selectTester.trigger;
expect(trigger).toHaveTextContent('Select an item');
expect(trigger).not.toHaveAttribute('data-pressed');
expect(trigger).toHaveTextContent('Select…');

await selectTester.selectOption({option: 'Cat'});
expect(trigger).toHaveTextContent('Cat');
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/table/docs/TableView.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,7 @@ it('TableView can toggle row selection', async function () {
// Render your test component/app and initialize the table tester
let {getByTestId} = render(
<Provider theme={defaultTheme}>
<TableView data-testid="test-table">
<TableView data-testid="test-table" selectionMode="multiple">
...
</TableView>
</Provider>
Expand Down
1 change: 0 additions & 1 deletion packages/@react-spectrum/tabs/docs/Tabs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ it('Tabs can change selection via keyboard', async function () {
expect(tabsTester.selectedTab).toBe(tabs[0]);

await tabsTester.triggerTab({tab: 1});
expect(onSelectionChange).toBeCalledTimes(1);
expect(tabsTester.selectedTab).toBe(tabs[1]);
});
```
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/tree/docs/TreeView.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ it('TreeView can select a row via keyboard', async function () {
// Render your test component/app and initialize the Tree tester
let {getByTestId} = render(
<Provider theme={defaultTheme}>
<TreeView data-testid="test-tree">
<TreeView data-testid="test-tree" selectionMode="multiple">
...
</TreeView>
</Provider>
Expand Down
7 changes: 2 additions & 5 deletions packages/react-aria-components/docs/GridList.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1885,24 +1885,21 @@ let testUtilUser = new User({interactionType: 'mouse'});
it('GridList can select a row via keyboard', async function () {
// Render your test component/app and initialize the gridlist tester
let {getByTestId} = render(
<GridList data-testid="test-gridlist">
<GridList data-testid="test-gridlist" selectionMode="single">
...
</GridList>
);
let gridlistTester = testUtilUser.createTester('GridList', {root: getByTestId('test-gridlist'), interactionType: 'keyboard'});
let gridListTester = testUtilUser.createTester('GridList', {root: getByTestId('test-gridlist'), interactionType: 'keyboard'});

let row = gridListTester.rows[0];
expect(row).not.toHaveClass('selected');
expect(within(row).getByRole('checkbox')).not.toBeChecked();
expect(gridListTester.selectedRows).toHaveLength(0);

await gridListTester.toggleRowSelection({row: 0});
expect(row).toHaveClass('selected');
expect(within(row).getByRole('checkbox')).toBeChecked();
expect(gridListTester.selectedRows).toHaveLength(1);

await gridListTester.toggleRowSelection({row: 0});
expect(row).not.toHaveClass('selected');
expect(within(row).getByRole('checkbox')).not.toBeChecked();
expect(gridListTester.selectedRows).toHaveLength(0);
});
Expand Down
3 changes: 1 addition & 2 deletions packages/react-aria-components/docs/ListBox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2109,15 +2109,14 @@ let testUtilUser = new User({interactionType: 'mouse'});
it('ListBox can select an option via keyboard', async function () {
// Render your test component/app and initialize the listbox tester
let {getByTestId} = render(
<ListBox data-testid="test-listbox">
<ListBox selectionMode="single" data-testid="test-listbox">
...
</ListBox>
);
let listboxTester = testUtilUser.createTester('ListBox', {root: getByTestId('test-listbox'), interactionType: 'keyboard'});

await listboxTester.toggleOptionSelection({option: 4});
expect(listboxTester.options()[4]).toHaveAttribute('aria-selected', 'true');
expect(onSelectionChange).toBeCalledTimes(1);
});
```

Expand Down
9 changes: 5 additions & 4 deletions packages/react-aria-components/docs/Menu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,8 @@ let testUtilUser = new User({interactionType: 'mouse'});
it('Menu can open its submenu via keyboard', async function () {
// Render your test component/app and initialize the menu tester
let {getByTestId} = render(
<MenuTrigger data-testid="test-menutrigger">
<MenuTrigger>
<Button data-testid="test-menutrigger">Menu trigger</Button>
...
</MenuTrigger>
);
Expand All @@ -1173,9 +1174,9 @@ it('Menu can open its submenu via keyboard', async function () {
let submenuTester = await menuTester.openSubmenu({submenuTrigger: 'Share…'});
expect(submenuTester.menu).toBeInTheDocument();

await submenuTester.selectOption({option: submenuUtil.options()[0]});
expect(submenuTester.menu).toBeInTheDocument();
expect(menuTester.menu).toBeInTheDocument();
await submenuTester.selectOption({option: submenuTester.options()[0]});
expect(submenuTester.menu).not.toBeInTheDocument();
expect(menuTester.menu).not.toBeInTheDocument();
});
```

Expand Down
2 changes: 1 addition & 1 deletion packages/react-aria-components/docs/Table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,7 @@ let testUtilUser = new User({interactionType: 'mouse', advanceTimer: jest.advanc
it('Table can toggle row selection', async function () {
// Render your test component/app and initialize the table tester
let {getByTestId} = render(
<Table data-testid="test-table">
<Table data-testid="test-table" selectionMode="multiple">
...
</Table>
);
Expand Down
1 change: 0 additions & 1 deletion packages/react-aria-components/docs/Tabs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,6 @@ it('Tabs can change selection via keyboard', async function () {
expect(tabsTester.selectedTab).toBe(tabs[0]);

await tabsTester.triggerTab({tab: 1});
expect(onSelectionChange).toBeCalledTimes(1);
expect(tabsTester.selectedTab).toBe(tabs[1]);
});
```
Expand Down
2 changes: 1 addition & 1 deletion packages/react-aria-components/docs/Tree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ let testUtilUser = new User({interactionType: 'mouse'});
it('Tree can select a row via keyboard', async function () {
// Render your test component/app and initialize the Tree tester
let {getByTestId} = render(
<Tree data-testid="test-tree">
<Tree data-testid="test-tree" selectionMode="multiple">
...
</Tree>
);
Expand Down
Loading