Skip to content

Commit

Permalink
✅ test: Verified nested YArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 9, 2021
1 parent 4bb0c09 commit a6013ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/utility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ describe("arrayToYArray", () =>
ymap.set("array", arrayToYArray(array));
expect(ymap.get("array").toJSON()).toEqual(array);
});

it.each([
[
[ [] ], 0
],
[
[ 1, [ 2, 3 ] ], 1
]
])(
"Creates nested YArrays from %s.",
(array: any[], nestedArrayIndex: number) =>
{
ymap.set("array", arrayToYArray(array));

expect(ymap.get("array").toJSON()).toEqual(array);
expect((ymap.get("array").get(nestedArrayIndex) as Y.Array<any>).toJSON())
.toEqual(array[nestedArrayIndex]);
}
);
});
8 changes: 7 additions & 1 deletion src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ export const arrayToYArray = (array: any[]): Y.Array<any> =>
const yarray = new Y.Array();

array.forEach((value) =>
yarray.push([ value ]));
{
if (value instanceof Array)
yarray.push([ arrayToYArray(value) ]);

else
yarray.push([ value ]);
});

return yarray;
};

0 comments on commit a6013ff

Please sign in to comment.