Fix useTreeData subtree move validation - #10560
Conversation
d305b81 to
6bc74bf
Compare
6bc74bf to
0153b79
Compare
| expect(e.toString()).toContain('Cannot move an item to be a child of itself.'); | ||
| } | ||
|
|
||
| // In React 17, act() does not synchronously re-throw errors from state updaters. |
There was a problem hiding this comment.
I see, change it to this:
let reactMajor = parseInt(React.version, 10);
let expectMoveError = (result, action) => {
if (reactMajor >= 18) {
expect(() => act(action)).toThrow('Cannot move an item to be a child of itself.');
} else {
act(action);
expect(result.error?.message).toContain('Cannot move an item to be a child of itself.');
}
};
and put the change I made to source back or explain why it doesn't work
There was a problem hiding this comment.
Thanks for the cleaner test helper! I've updated the tests to use expectMoveError.
My previous commit reverting your while (parent != null) logic was just an accidental git staging mistake while debugging locally.
There was a problem hiding this comment.
When I saw Ci fail I suspected your change to the while loop in useTreeData.ts might have caused it.
To test this theory, I ran git checkout aef9f68 packages/react-stately/src/data/useTreeData.ts to see if it fixed the test failures locally. eventually figured out the actual issue was a yarn cache problem locally However, I forgot to un-stage my temporary checkout of useTreeData.ts. So when I committed the test fixes
Thank you for correcting me.
e61d1e1 to
9cc97f0
Compare
Description
Closes #10539
Summary
Prevent
useTreeDatafrom silently dropping nodes when an item is moved into its own subtree.This adds validation to the imperative
move,moveBefore, andmoveAfterAPIs so that moves targeting a node's own descendants throw:Cannot move an item to be a child of itself.The validation also covers root-level nodes, which were previously missed by
moveBeforeandmoveAfter.Changes
move().moveBefore()andmoveAfter().Pull Request Checklist:
Test Instructions:
Run the
useTreeDatatest suite:The regression tests verify that
move,moveBefore, andmoveAfterthrow when attempting to move an item into its own subtree, including root-level ancestor cases.Your Project:
None