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

[core] Use describeTreeView for selection tests #12647

Merged
merged 3 commits into from
Apr 3, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 0 additions & 90 deletions packages/x-tree-view/src/SimpleTreeView/SimpleTreeView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ describe('<SimpleTreeView />', () => {
}));

describe('warnings', () => {
it('should warn when switching from controlled to uncontrolled of the selectedItems prop', () => {
const { setProps } = render(
<SimpleTreeView selectedItems={null}>
<TreeItem itemId="1" label="one" />
</SimpleTreeView>,
);

expect(() => {
setProps({ selectedItems: undefined });
}).toErrorDev(
'MUI X: A component is changing the controlled selectedItems state of TreeView to be uncontrolled.',
);
});

it('should not crash when shift clicking a clean tree', () => {
render(
<SimpleTreeView multiSelect>
Expand Down Expand Up @@ -145,70 +131,6 @@ describe('<SimpleTreeView />', () => {
expect(getByTestId('one')).to.have.attribute('aria-selected');
});

it('should be able to be controlled with the selectedItems prop and singleSelect', () => {
function MyComponent() {
const [selectedState, setSelectedState] = React.useState(null);
const onSelectedItemsChange = (event, items) => {
setSelectedState(items);
};
return (
<SimpleTreeView selectedItems={selectedState} onSelectedItemsChange={onSelectedItemsChange}>
<TreeItem itemId="1" label="one" data-testid="one" />
<TreeItem itemId="2" label="two" data-testid="two" />
</SimpleTreeView>
);
}

const { getByTestId, getByText } = render(<MyComponent />);

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

fireEvent.click(getByText('one'));

expect(getByTestId('one')).to.have.attribute('aria-selected', 'true');
expect(getByTestId('two')).not.to.have.attribute('aria-selected');

fireEvent.click(getByText('two'));

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

it('should be able to be controlled with the selectedItems prop and multiSelect', () => {
function MyComponent() {
const [selectedState, setSelectedState] = React.useState([]);
const onSelectedItemsChange = (event, items) => {
setSelectedState(items);
};
return (
<SimpleTreeView
selectedItems={selectedState}
onSelectedItemsChange={onSelectedItemsChange}
multiSelect
>
<TreeItem itemId="1" label="one" data-testid="one" />
<TreeItem itemId="2" label="two" data-testid="two" />
</SimpleTreeView>
);
}

const { getByTestId, getByText } = render(<MyComponent />);

expect(getByTestId('one')).to.have.attribute('aria-selected', 'false');
expect(getByTestId('two')).to.have.attribute('aria-selected', 'false');

fireEvent.click(getByText('one'));

expect(getByTestId('one')).to.have.attribute('aria-selected', 'true');
expect(getByTestId('two')).to.have.attribute('aria-selected', 'false');

fireEvent.click(getByText('two'), { ctrlKey: true });

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

it('should not error when component state changes', () => {
function MyComponent() {
const [, setState] = React.useState(1);
Expand Down Expand Up @@ -438,17 +360,5 @@ describe('<SimpleTreeView />', () => {

expect(getByRole('tree')).not.to.equal(null);
});

it('(TreeView) should have the attribute `aria-multiselectable=false if using single select`', () => {
const { getByRole } = render(<SimpleTreeView />);

expect(getByRole('tree')).to.have.attribute('aria-multiselectable', 'false');
});

it('(TreeView) should have the attribute `aria-multiselectable=true if using multi select`', () => {
const { getByRole } = render(<SimpleTreeView multiSelect />);

expect(getByRole('tree')).to.have.attribute('aria-multiselectable', 'true');
});
});
});