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

[TreeView] Throw an error when two items have the same id #11715

Merged
merged 2 commits into from
Jan 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ export const useTreeViewJSXNodes: TreeViewPlugin<UseTreeViewJSXNodesSignature> =
setState,
}) => {
const insertJSXNode = useEventCallback((node: TreeViewNode) => {
setState((prevState) => ({ ...prevState, nodeMap: { ...prevState.nodeMap, [node.id]: node } }));
setState((prevState) => {
if (prevState.nodeMap[node.id] != null) {
throw new Error(
[
'MUI X: The Tree View component requires all items to have a unique `id` property.',
'Alternatively, you can use the `getItemId` prop to specify a custom id for each item.',
`Tow items were provided with the same id in the \`items\` prop: "${node.id}"`,
].join('\n'),
);
}

return { ...prevState, nodeMap: { ...prevState.nodeMap, [node.id]: node } };
});
});

const removeJSXNode = useEventCallback((nodeId: string) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer, ErrorBoundary } from '@mui-internal/test-utils';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView';
import { TreeItem } from '@mui/x-tree-view/TreeItem';

describe('useTreeViewNodes', () => {
const { render } = createRenderer();

it('should throw an error when two items have the same ID (items prop approach)', function test() {
// TODO is this fixed?
Copy link
Member Author

Choose a reason for hiding this comment

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

The grid could not run similar tests with karma, I tried a little but did not want to invest to much time on it

if (!/jsdom/.test(window.navigator.userAgent)) {
// can't catch render errors in the browser for unknown reason
// tried try-catch + error boundary + window onError preventDefault
this.skip();
}

expect(() =>
render(
<ErrorBoundary>
<RichTreeView
items={[
{ id: '1', label: '1' },
{ id: '1', label: 'B' },
]}
/>
</ErrorBoundary>,
),
).toErrorDev([
'MUI X: The Tree View component requires all items to have a unique `id` property.',
'MUI X: The Tree View component requires all items to have a unique `id` property.',
'The above error occurred in the <ForwardRef(RichTreeView)> component:',
]);
});

it('should throw an error when two items have the same ID (JSX approach)', function test() {
// TODO is this fixed?
if (!/jsdom/.test(window.navigator.userAgent)) {
// can't catch render errors in the browser for unknown reason
// tried try-catch + error boundary + window onError preventDefault
this.skip();
}

expect(() =>
render(
<ErrorBoundary>
<SimpleTreeView>
<TreeItem nodeId="1" label="A" />
<TreeItem nodeId="1" label="B" />
</SimpleTreeView>
</ErrorBoundary>,
),
).toErrorDev([
'MUI X: The Tree View component requires all items to have a unique `id` property.',
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
'MUI X: The Tree View component requires all items to have a unique `id` property.',
'The above error occurred in the <ForwardRef(SimpleTreeView)> component:',
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const updateState = ({
parentId: string | null,
): TreeViewNodeIdAndChildren => {
const id: string = getItemId ? getItemId(item) : (item as any).id;

if (id == null) {
throw new Error(
[
Expand All @@ -40,6 +41,16 @@ const updateState = ({
);
}

if (nodeMap[id] != null) {
throw new Error(
[
'MUI X: The Tree View component requires all items to have a unique `id` property.',
'Alternatively, you can use the `getItemId` prop to specify a custom id for each item.',
`Tow items were provided with the same id in the \`items\` prop: "${id}"`,
].join('\n'),
);
}

const label = getItemLabel ? getItemLabel(item) : (item as { label: string }).label;
if (label == null) {
throw new Error(
Expand Down