jazz-tools 0.13.10 → 0.13.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -119,6 +119,71 @@ describe("Simple CoMap operations", async () => {
119
119
  ).toThrow();
120
120
  });
121
121
 
122
+ test("toJSON should not fail when there is a key in the raw value not represented in the schema", () => {
123
+ class TestMap extends CoMap {
124
+ color = co.string;
125
+ height = co.number;
126
+ }
127
+
128
+ const map = TestMap.create({ color: "red", height: 10 }, { owner: me });
129
+
130
+ map._raw.set("extra", "extra");
131
+
132
+ expect(map.toJSON()).toEqual({
133
+ _type: "CoMap",
134
+ id: map.id,
135
+ color: "red",
136
+ height: 10,
137
+ });
138
+ });
139
+
140
+ test("toJSON should handle references", () => {
141
+ class TestMap extends CoMap {
142
+ color = co.string;
143
+ height = co.number;
144
+ nested = co.optional.ref(TestMap);
145
+ }
146
+
147
+ const map = TestMap.create({ color: "red", height: 10 }, { owner: me });
148
+
149
+ map.nested = TestMap.create({ color: "blue", height: 20 }, { owner: me });
150
+
151
+ expect(map.toJSON()).toEqual({
152
+ _type: "CoMap",
153
+ id: map.id,
154
+ color: "red",
155
+ height: 10,
156
+ nested: {
157
+ _type: "CoMap",
158
+ id: map.nested?.id,
159
+ color: "blue",
160
+ height: 20,
161
+ },
162
+ });
163
+ });
164
+
165
+ test("toJSON should handle circular references", () => {
166
+ class TestMap extends CoMap {
167
+ color = co.string;
168
+ height = co.number;
169
+ nested = co.optional.ref(TestMap);
170
+ }
171
+
172
+ const map = TestMap.create({ color: "red", height: 10 }, { owner: me });
173
+
174
+ map.nested = map;
175
+
176
+ expect(map.toJSON()).toEqual({
177
+ _type: "CoMap",
178
+ id: map.id,
179
+ color: "red",
180
+ height: 10,
181
+ nested: {
182
+ _circular: map.id,
183
+ },
184
+ });
185
+ });
186
+
122
187
  test("testing toJSON on a CoMap with a Date field", () => {
123
188
  const map = TestMap.create(
124
189
  {
@@ -36,10 +36,10 @@ describe("CoPlainText", () => {
36
36
  test("insertion", () => {
37
37
  const text = CoPlainText.create("hello world", { owner: me });
38
38
 
39
- text.insertAfter(5, " cruel");
39
+ text.insertAfter(4, " cruel");
40
40
  expect(text + "").toEqual("hello cruel world");
41
41
 
42
- text.insertAfter(0, "Hello, ");
42
+ text.insertBefore(0, "Hello, ");
43
43
  expect(text + "").toEqual("Hello, hello cruel world");
44
44
  });
45
45
 
@@ -49,6 +49,24 @@ describe("CoPlainText", () => {
49
49
  text.deleteRange({ from: 3, to: 8 });
50
50
  expect(text + "").toEqual("helrld");
51
51
  });
52
+
53
+ test("applyDiff", () => {
54
+ const text = CoPlainText.create("hello world", { owner: me });
55
+ text.applyDiff("hello cruel world");
56
+ expect(text.toString()).toEqual("hello cruel world");
57
+ });
58
+ });
59
+
60
+ describe("Properties", () => {
61
+ test("length", () => {
62
+ const text = CoPlainText.create("hello world", { owner: me });
63
+ expect(text.length).toBe(11);
64
+ });
65
+
66
+ test("toString", () => {
67
+ const text = CoPlainText.create("hello world", { owner: me });
68
+ expect(text.toString()).toBe("hello world");
69
+ });
52
70
  });
53
71
 
54
72
  describe("Position operations", () => {
@@ -155,7 +173,7 @@ describe("CoPlainText", () => {
155
173
  expect(update1.toString()).toBe("hello world");
156
174
 
157
175
  // When we make a change, we should get an update
158
- text.insertAfter(5, " beautiful");
176
+ text.insertAfter(4, " beautiful");
159
177
  const update2 = (await queue.next()).value;
160
178
  expect(update2.toString()).toBe("hello beautiful world");
161
179