Skip to content

Commit

Permalink
✅ test: Can convert basic arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 9, 2021
1 parent aeb355d commit 4bb0c09
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/utility.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Y from "yjs";
import { arrayToYArray, } from "./utility";

describe("arrayToYArray", () =>
{
let ydoc: Y.Doc = new Y.Doc();
let ymap: Y.Map<any> = new Y.Map();

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

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

it.each([
[
[]
],
[
[ 1 ]
],
[
[ 1, 2, 3, 4 ]
]
])("Creates a YArray from %s.", (array: number[]) =>
{
ymap.set("array", arrayToYArray(array));
expect(ymap.get("array").toJSON()).toEqual(array);
});
});
11 changes: 11 additions & 0 deletions src/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Y from "yjs";

export const arrayToYArray = (array: any[]): Y.Array<any> =>
{
const yarray = new Y.Array();

array.forEach((value) =>
yarray.push([ value ]));

return yarray;
};

0 comments on commit 4bb0c09

Please sign in to comment.