Skip to content
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

[tree view] Cleanup onKeyDown handler #11481

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 30 additions & 0 deletions packages/x-tree-view/src/TreeItem/TreeItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,36 @@ describe('<TreeItem />', () => {

expect(getByTestId('one')).not.to.have.attribute('aria-selected');
});

it('should select a node when Enter is pressed and the node is not selected', () => {
const { getByRole, getByTestId } = render(
<SimpleTreeView>
<TreeItem nodeId="one" label="one" data-testid="one" />
</SimpleTreeView>,
);

act(() => {
getByRole('tree').focus();
});
fireEvent.keyDown(getByRole('tree'), { key: 'Enter' });

expect(getByTestId('one')).to.have.attribute('aria-selected');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: have you considered using getByRole('treeitem') instead? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the DX used on the other tests, but I did not think about the alternative tbh

});

it('should not un-select a node when Enter is pressed and the node is selected', () => {
const { getByRole, getByTestId } = render(
<SimpleTreeView defaultSelected="one">
<TreeItem nodeId="one" label="one" data-testid="one" />
</SimpleTreeView>,
);

act(() => {
getByRole('tree').focus();
});
fireEvent.keyDown(getByRole('tree'), { key: 'Enter' });

expect(getByTestId('one')).to.have.attribute('aria-selected');
});
});

describe('mouse', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type MuiCancellableEvent = {
defaultMuiPrevented?: boolean;
};

export type MuiCancellableEventHandler<Event> = (event: Event & MuiCancellableEvent) => void;
Loading