Skip to content

Commit

Permalink
✅ test: Patches are applied to nested types
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 9, 2021
1 parent 71590d5 commit f5646a1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/patching.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,22 @@ describe("getChangeList", () =>

describe("patchSharedType", () =>
{
it("Applies additions to the given shared type.", () =>
let ydoc: Y.Doc = new Y.Doc();
let ymap: Y.Map<any> = new Y.Map();

beforeEach(() =>
{
const ydoc = new Y.Doc();
const ymap = ydoc.getMap("tmp");
ydoc = new Y.Doc();
ymap = ydoc.getMap("tmp");
});

afterEach(() =>
{
ydoc.destroy();
});

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

Expand All @@ -93,9 +104,6 @@ describe("patchSharedType", () =>

it("Applies updates to the given shared type.", () =>
{
const ydoc = new Y.Doc();
const ymap = ydoc.getMap("tmp");

ymap.set("state", objectToYMap({ "foo": 1, }));
patchSharedType(ymap.get("state"), { "foo": 2, });

Expand All @@ -104,12 +112,19 @@ describe("patchSharedType", () =>

it("Applies deletes to the given shared type.", () =>
{
const ydoc = new Y.Doc();
const ymap = ydoc.getMap("tmp");

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.", () =>
{
ymap.set("state", objectToYMap({ "foo": { "bar": 1, }, }));
patchSharedType(ymap.get("state"), { "foo": { "bar": 2, }, });

expect(ymap.get("state")
.get("foo")
.get("bar")).toBe(2);
});
});
10 changes: 10 additions & 0 deletions src/patching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export const patchSharedType = (

break;

case "pending":
if (sharedType instanceof Y.Map)
{
patchSharedType(
sharedType.get(property as string),
update[property as string]
);
}
break;

default:
break;
}
Expand Down

0 comments on commit f5646a1

Please sign in to comment.