Skip to content

Commit 7614175

Browse files
authored
tree(fix): Validate source indices correctly on moves (#21552)
## Description This PR fixes the validation of moves when there is a specified source array. Previously, the validation was using the destination array for validation unconditionally instead of the source when it was passed.
1 parent 7c2ac9b commit 7614175

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

packages/dds/tree/src/simple-tree/arrayNode.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -773,17 +773,20 @@ abstract class CustomArrayNodeBase<const T extends ImplicitAllowedTypes>
773773
fieldEditor.remove(removeStart, removeEnd - removeStart);
774774
}
775775
public moveToStart(sourceIndex: number, source?: TreeArrayNode): void {
776-
const field = getSequenceField(this);
776+
const sourceArray = source ?? this;
777+
const field = getSequenceField(sourceArray);
777778
validateIndex(sourceIndex, field, "moveToStart");
778779
this.moveRangeToIndex(0, sourceIndex, sourceIndex + 1, source);
779780
}
780781
public moveToEnd(sourceIndex: number, source?: TreeArrayNode): void {
781-
const field = getSequenceField(this);
782+
const sourceArray = source ?? this;
783+
const field = getSequenceField(sourceArray);
782784
validateIndex(sourceIndex, field, "moveToEnd");
783785
this.moveRangeToIndex(this.length, sourceIndex, sourceIndex + 1, source);
784786
}
785787
public moveToIndex(index: number, sourceIndex: number, source?: TreeArrayNode): void {
786-
const field = getSequenceField(this);
788+
const sourceArray = source ?? this;
789+
const field = getSequenceField(sourceArray);
787790
validateIndex(index, field, "moveToIndex", true);
788791
validateIndex(sourceIndex, field, "moveToIndex");
789792
this.moveRangeToIndex(index, sourceIndex, sourceIndex + 1, source);

packages/dds/tree/src/test/simple-tree/arrayNode.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ describe("ArrayNode", () => {
110110
});
111111

112112
describe("moveToStart", () => {
113+
it("move element to start of empty array", () => {
114+
const schema = schemaFactory.object("parent", {
115+
array1: schemaFactory.array(schemaFactory.number),
116+
array2: schemaFactory.array(schemaFactory.number),
117+
});
118+
const { array1, array2 } = hydrate(schema, { array1: [], array2: [1, 2, 3] });
119+
array1.moveToStart(1, array2);
120+
assert.deepEqual([...array1], [2]);
121+
assert.deepEqual([...array2], [1, 3]);
122+
});
123+
113124
it("move within field", () => {
114125
const array = hydrate(schemaType, [1, 2, 3]);
115126
array.moveToStart(1);
@@ -144,6 +155,17 @@ describe("ArrayNode", () => {
144155
});
145156

146157
describe("moveToEnd", () => {
158+
it("move element to end of empty array", () => {
159+
const schema = schemaFactory.object("parent", {
160+
array1: schemaFactory.array(schemaFactory.number),
161+
array2: schemaFactory.array(schemaFactory.number),
162+
});
163+
const { array1, array2 } = hydrate(schema, { array1: [], array2: [1, 2, 3] });
164+
array1.moveToEnd(1, array2);
165+
assert.deepEqual([...array1], [2]);
166+
assert.deepEqual([...array2], [1, 3]);
167+
});
168+
147169
it("move within field", () => {
148170
const array = hydrate(schemaType, [1, 2, 3]);
149171
array.moveToEnd(1);
@@ -178,6 +200,17 @@ describe("ArrayNode", () => {
178200
});
179201

180202
describe("moveToIndex", () => {
203+
it("move element to start of empty array", () => {
204+
const schema = schemaFactory.object("parent", {
205+
array1: schemaFactory.array(schemaFactory.number),
206+
array2: schemaFactory.array(schemaFactory.number),
207+
});
208+
const { array1, array2 } = hydrate(schema, { array1: [], array2: [1, 2, 3] });
209+
array1.moveToIndex(0, 1, array2);
210+
assert.deepEqual([...array1], [2]);
211+
assert.deepEqual([...array2], [1, 3]);
212+
});
213+
181214
it("move within field", () => {
182215
const array = hydrate(schemaType, [1, 2, 3]);
183216
array.moveToIndex(0, 1);

0 commit comments

Comments
 (0)