Skip to content

Commit 813e2f4

Browse files
committed
added unit test for setting correct paths for array changes
1 parent ba0cc05 commit 813e2f4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

packages/runner/test/data-updating.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,44 @@ describe("data-updating", () => {
305305
expect(changes[0].value).toBe(2);
306306
});
307307

308+
it("should generate correct paths when setting array length to 0", () => {
309+
const testCell = runtime.getCell<{ items: number[] }>(
310+
space,
311+
"normalizeAndDiff array length to zero",
312+
undefined,
313+
tx,
314+
);
315+
// Create array with 100 items
316+
const largeArray = Array.from({ length: 100 }, (_, i) => i);
317+
testCell.set({ items: largeArray });
318+
319+
// Now set length to 0 through the length property
320+
const lengthLink = testCell.key("items").key("length")
321+
.getAsNormalizedFullLink();
322+
const changes = normalizeAndDiff(runtime, tx, lengthLink, 0);
323+
324+
// Should have 101 changes total
325+
expect(changes.length).toBe(101);
326+
327+
// Find the length change
328+
const lengthChange = changes.find((c) =>
329+
c.location.path[c.location.path.length - 1] === "length"
330+
);
331+
expect(lengthChange).toBeDefined();
332+
expect(lengthChange!.value).toBe(0);
333+
334+
// Verify all elements are marked undefined with correct paths
335+
const elementChanges = changes.filter((c) =>
336+
c.location.path[c.location.path.length - 1] !== "length"
337+
);
338+
expect(elementChanges.length).toBe(100);
339+
340+
elementChanges.forEach((change, i) => {
341+
expect(change.location.path).toEqual(["items", i.toString()]);
342+
expect(change.value).toBe(undefined);
343+
});
344+
});
345+
308346
it("should handle array element changes", () => {
309347
const testCell = runtime.getCell<{ items: number[] }>(
310348
space,

0 commit comments

Comments
 (0)