Skip to content
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
6 changes: 3 additions & 3 deletions packages/dev/test-utils/src/renderOverride.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ function customRender(ui: Parameters<typeof render>[0], options?: Parameters<typ
let reactTestingLibrary = require('@testing-library/react');
// export renderHook and actHook from testing-library/react-hooks library if they don't exist in @testing-library/react
// (i.e. renderHook is only in v13+ of testing library)
export let renderHook = reactTestingLibrary.renderHook as typeof originalRenderHook;
export let renderHook: typeof originalRenderHook = (render, options) => reactTestingLibrary.renderHook(render, {wrapper: StrictModeWrapper, ...options});
export let actHook = reactTestingLibrary.act as typeof originalAct;
if (!renderHook) {
if (!reactTestingLibrary.renderHook) {
let rhtl = require('@testing-library/react-hooks');
renderHook = rhtl.renderHook;
renderHook = (render, options) => rhtl.renderHook(render, {wrapper: StrictModeWrapper, ...options});
actHook = rhtl.act;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-stately/src/data/useTreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
},
insert(parentKey: Key | null, index: number, ...values: T[]) {
setItems(({items, nodeMap: originalMap}) => {
let {items: newNodes, nodeMap: newMap} = buildTree(values, originalMap, parentKey);
let {items: newNodes, nodeMap: newMap} = buildTree(values, new Map(originalMap), parentKey);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should the update function below also do this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure why, but the update function works fine with StrictMode.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've been unable to find a case where it doesn't work. If we did need to make the change though, we'd need to create the new map outside of the updateTree function so if there's multiple items to update, we don't create a new map for each one.


// If parentKey is null, insert into the root.
if (parentKey == null) {
Expand Down
41 changes: 41 additions & 0 deletions packages/react-stately/test/data/useTreeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ describe('useTreeData', function () {
expect(result.current.items[0].children[0].children[1].value).toEqual({name: 'Devon'});
expect(result.current.items[0].children[1]).toBe(initialResult.items[0].children[1]);
expect(result.current.items[0].children[2]).toBe(initialResult.items[0].children[2]);

({result} = renderHook(() => useTreeData({initialItems: [], getChildren, getKey})));

expect(result.current.items).toHaveLength(0);

act(() => {
result.current.insert(null, 0, {name: 'John'});
});

expect(result.current.items).toHaveLength(1);
expect(result.current.items[0].value).toEqual({name: 'John'});

act(() => {
result.current.insert('John', 0, {name: 'Devon'});
});

expect(result.current.items).toHaveLength(1);
expect(result.current.items[0].value).toEqual({name: 'John'});
expect(result.current.items[0].children).toHaveLength(1);
expect(result.current.items[0].children[0].value).toEqual({name: 'Devon'});
});

it('should insert multiple items into a child node', function () {
Expand All @@ -90,6 +110,27 @@ describe('useTreeData', function () {
expect(result.current.items[0].children[0].children[2].value).toEqual({name: 'Danni'});
expect(result.current.items[0].children[1]).toBe(initialResult.items[0].children[1]);
expect(result.current.items[0].children[2]).toBe(initialResult.items[0].children[2]);

({result} = renderHook(() => useTreeData({initialItems: [], getChildren, getKey})));

expect(result.current.items).toHaveLength(0);

act(() => {
result.current.insert(null, 0, {name: 'John'});
});

expect(result.current.items).toHaveLength(1);
expect(result.current.items[0].value).toEqual({name: 'John'});

act(() => {
result.current.insert('John', 0, {name: 'Devon'}, {name: 'Danni'});
});

expect(result.current.items).toHaveLength(1);
expect(result.current.items[0].value).toEqual({name: 'John'});
expect(result.current.items[0].children).toHaveLength(2);
expect(result.current.items[0].children[0].value).toEqual({name: 'Devon'});
expect(result.current.items[0].children[1].value).toEqual({name: 'Danni'});
});

it('should insert an item into the root', function () {
Expand Down
Loading