Skip to content

Commit

Permalink
✅ test: Maps nested in maps are patched
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 9, 2021
1 parent f5646a1 commit 4c68537
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/patching.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Y from "yjs";
import { objectToYMap, } from "./mapping";
import { arrayToYArray, objectToYMap, } from "./mapping";
import { getChangeList, patchSharedType, } from "./patching";

describe("getChangeList", () =>
Expand Down Expand Up @@ -94,31 +94,41 @@ describe("patchSharedType", () =>
ydoc.destroy();
});

it("Applies additions to the given shared type.", () =>
it("Applies additions to maps.", () =>
{
ymap.set("state", objectToYMap({ }));
patchSharedType(ymap.get("state"), { "foo": 1, });

expect(ymap.get("state").get("foo")).toBe(1);
});

it("Applies updates to the given shared type.", () =>
it("Applies updates to maps.", () =>
{
ymap.set("state", objectToYMap({ "foo": 1, }));
patchSharedType(ymap.get("state"), { "foo": 2, });

expect(ymap.get("state").get("foo")).toBe(2);
});

it("Applies deletes to the given shared type.", () =>
it("Applies deletes to maps.", () =>
{
ymap.set("state", objectToYMap({ "foo": 1, }));
patchSharedType(ymap.get("state"), { });

expect(Array.from(ymap.get("state").keys())).toEqual([]);
});

it("Applies nested additions to the given shared type.", () =>
it("Applies additions to maps nested in maps.", () =>
{
ymap.set("state", objectToYMap({ "foo": { }, }));
patchSharedType(ymap.get("state"), { "foo": { "bar": 2, }, });

expect(ymap.get("state")
.get("foo")
.get("bar")).toBe(2);
});

it("Applies updates to maps nested in maps.", () =>
{
ymap.set("state", objectToYMap({ "foo": { "bar": 1, }, }));
patchSharedType(ymap.get("state"), { "foo": { "bar": 2, }, });
Expand All @@ -127,4 +137,22 @@ describe("patchSharedType", () =>
.get("foo")
.get("bar")).toBe(2);
});

it("Applies deletions to maps nested in maps.", () =>
{
ymap.set("state", objectToYMap({ "foo": { "bar": 1, }, }));
patchSharedType(ymap.get("state"), { "foo": { }, });

expect(Array.from(ymap.get("state")
.get("foo")
.keys())).toEqual([]);
});

it("Applies additions to arrays.", () =>
{
ymap.set("array", arrayToYArray([ ]));
patchSharedType(ymap.get("array"), [ 1 ]);

expect(ymap.get("array").get(0)).toBe(1);
});
});
11 changes: 11 additions & 0 deletions src/patching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ export const patchSharedType = (
if (sharedType instanceof Y.Map)
sharedType.set(property as string, value);

else if (sharedType instanceof Y.Array)
{
const index = property as number;

const left = sharedType.slice(0, index);
const right = sharedType.slice(index+1);

sharedType.delete(0, sharedType.length);
sharedType.insert(0, [ ...left, value, ...right ]);
}

break;

case "delete":
Expand Down

0 comments on commit 4c68537

Please sign in to comment.