Y_test.res.mjs
1// Generated by ReScript, PLEASE EDIT WITH CARE
2
3import * as Y from "../src/ffi/Y.res.mjs";
4import * as Yjs from "yjs";
5import * as Vitest from "vitest";
6import * as Core__JSON from "@rescript/core/lib/es6/src/Core__JSON.res.mjs";
7import * as Core__Option from "@rescript/core/lib/es6/src/Core__Option.res.mjs";
8import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
9
10function insertText(ytext, index, text) {
11 (((yt, idx, str) => { yt.insert(idx, str); })(ytext, index, text));
12}
13
14function pushArray(yarray, values) {
15 (((ya, vals) => { ya.push(vals); })(yarray, values));
16}
17
18function setMap(ymap, key, value) {
19 (((ym, k, v) => { ym.set(k, v); })(ymap, key, value));
20}
21
22Vitest.describe("Y.Doc", () => {
23 Vitest.test("make creates a doc instance", async t => {
24 let doc = new Yjs.Doc();
25 t.expect(doc).not.toBeNull();
26 });
27 Vitest.test("onAfterTransaction fires after a transaction", async t => {
28 let doc = new Yjs.Doc();
29 let events = {
30 contents: []
31 };
32 Y.Doc.onAfterTransaction(doc, event => {
33 events.contents = events.contents.concat([event]);
34 });
35 let ytext = doc.getText("trigger");
36 insertText(ytext, 0, "test");
37 t.expect(events.contents.length).toBeGreaterThan(0);
38 });
39 Vitest.test("onAfterTransaction receives doc and transaction", async t => {
40 let doc = new Yjs.Doc();
41 let receivedDoc = {
42 contents: undefined
43 };
44 Y.Doc.onAfterTransaction(doc, event => {
45 receivedDoc.contents = Primitive_option.some(event.doc);
46 });
47 doc.getText("trigger");
48 t.expect(receivedDoc.contents).not.toBeNull();
49 });
50});
51
52Vitest.describe("Y.Text", () => {
53 Vitest.test("get returns a Y.Text instance", async t => {
54 let doc = new Yjs.Doc();
55 let ytext = doc.getText("mytext");
56 t.expect(ytext).not.toBeNull();
57 });
58 Vitest.test("toString returns string content", async t => {
59 let doc = new Yjs.Doc();
60 let ytext = doc.getText("mytext");
61 insertText(ytext, 0, "hello world");
62 let result = ytext.toString();
63 t.expect(result).toEqual("hello world");
64 });
65 Vitest.test("multiple text types in one doc", async t => {
66 let doc = new Yjs.Doc();
67 let title = doc.getText("title");
68 let body = doc.getText("body");
69 insertText(title, 0, "Trip Plan");
70 insertText(body, 0, "Details here...");
71 t.expect(title.toString()).toEqual("Trip Plan");
72 t.expect(body.toString()).toEqual("Details here...");
73 });
74});
75
76Vitest.describe("Y.Array", () => {
77 Vitest.test("get returns a Y.Array instance", async t => {
78 let doc = new Yjs.Doc();
79 let yarray = doc.getArray("myarray");
80 t.expect(yarray).not.toBeNull();
81 });
82 Vitest.test("toJSON returns array of JSON values", async t => {
83 let doc = new Yjs.Doc();
84 let yarray = doc.getArray("myarray");
85 pushArray(yarray, [
86 1,
87 2,
88 3
89 ]);
90 let result = yarray.toJSON();
91 t.expect(result.length).toEqual(3);
92 t.expect(Core__Option.flatMap(result[0], Core__JSON.Decode.float)).toEqual(1.0);
93 t.expect(Core__Option.flatMap(result[1], Core__JSON.Decode.float)).toEqual(2.0);
94 t.expect(Core__Option.flatMap(result[2], Core__JSON.Decode.float)).toEqual(3.0);
95 });
96 Vitest.test("toJSON with objects can be unmarshalled to records", async t => {
97 let doc = new Yjs.Doc();
98 let yarray = doc.getArray("routes");
99 let pushRouteFn = (function(ya) {
100 ya.push([{from: 'A', to: 'B', distance: 120}, {from: 'B', to: 'C', distance: 85}]);
101 });
102 pushRouteFn(yarray);
103 let routes = yarray.toJSON().map(json => {
104 let dict = Core__JSON.Decode.object(json);
105 if (dict === undefined) {
106 return {
107 from: "",
108 to: "",
109 distance: 0
110 };
111 }
112 let from = Core__Option.getOr(Core__Option.flatMap(dict["from"], Core__JSON.Decode.string), "");
113 let to = Core__Option.getOr(Core__Option.flatMap(dict["to"], Core__JSON.Decode.string), "");
114 let distance = Core__Option.getOr(Core__Option.map(Core__Option.flatMap(dict["distance"], Core__JSON.Decode.float), prim => prim | 0), 0);
115 return {
116 from: from,
117 to: to,
118 distance: distance
119 };
120 });
121 t.expect(routes.length).toEqual(2);
122 t.expect(routes[0]).toEqual({
123 from: "A",
124 to: "B",
125 distance: 120
126 });
127 t.expect(routes[1]).toEqual({
128 from: "B",
129 to: "C",
130 distance: 85
131 });
132 });
133});
134
135Vitest.describe("Y.Map", () => {
136 Vitest.test("get returns a Y.Map instance", async t => {
137 let doc = new Yjs.Doc();
138 let ymap = doc.getMap("mymap");
139 t.expect(ymap).not.toBeNull();
140 });
141 Vitest.test("toJSON returns JSON object", async t => {
142 let doc = new Yjs.Doc();
143 let ymap = doc.getMap("mymap");
144 setMap(ymap, "key", "value");
145 let result = ymap.toJSON();
146 let dict = Core__JSON.Decode.object(result);
147 if (dict !== undefined) {
148 let v = dict["key"];
149 if (v !== undefined) {
150 let match = Core__JSON.Decode.string(v);
151 if (match !== undefined) {
152 if (match === "value") {
153 t.expect(true).toEqual(true);
154 return;
155 }
156 t.expect("decode string failed").toEqual("");
157 return;
158 }
159 t.expect("decode string failed").toEqual("");
160 return;
161 }
162 t.expect("key missing").toEqual("");
163 return;
164 }
165 t.expect("not object").toEqual("");
166 });
167 Vitest.test("toJSON can be unmarshalled to a record", async t => {
168 let doc = new Yjs.Doc();
169 let ymap = doc.getMap("tripInfo");
170 let setTripInfoFn = (function(ym) {
171 ym.set('origin', 'Kathmandu');
172 ym.set('destination', 'Pokhara');
173 ym.set('distanceKm', 200);
174 });
175 setTripInfoFn(ymap);
176 let dict = Core__JSON.Decode.object(ymap.toJSON());
177 let info = dict !== undefined ? ({
178 origin: Core__Option.getOr(Core__Option.flatMap(dict["origin"], Core__JSON.Decode.string), ""),
179 destination: Core__Option.getOr(Core__Option.flatMap(dict["destination"], Core__JSON.Decode.string), ""),
180 distanceKm: Core__Option.getOr(Core__Option.map(Core__Option.flatMap(dict["distanceKm"], Core__JSON.Decode.float), prim => prim | 0), 0)
181 }) : ({
182 origin: "",
183 destination: "",
184 distanceKm: 0
185 });
186 t.expect(info.origin).toEqual("Kathmandu");
187 t.expect(info.destination).toEqual("Pokhara");
188 t.expect(info.distanceKm).toEqual(200);
189 });
190});
191
192Vitest.describe("Y.Doc with multiple shared types", () => {
193 Vitest.test("doc can hold Text, Array, and Map simultaneously", async t => {
194 let doc = new Yjs.Doc();
195 let ytext = doc.getText("notes");
196 let yarray = doc.getArray("waypoints");
197 let ymap = doc.getMap("metadata");
198 insertText(ytext, 0, "Day 1: Kathmandu to Pokhara");
199 pushArray(yarray, [
200 "Kathmandu",
201 "Pokhara"
202 ]);
203 setMap(ymap, "author", "Rider One");
204 t.expect(ytext.toString()).toEqual("Day 1: Kathmandu to Pokhara");
205 t.expect(yarray.toJSON().length).toEqual(2);
206 let dict = Core__JSON.Decode.object(ymap.toJSON());
207 if (dict !== undefined) {
208 let match = Core__Option.flatMap(dict["author"], Core__JSON.Decode.string);
209 if (match !== undefined) {
210 if (match === "Rider One") {
211 t.expect(true).toEqual(true);
212 return;
213 }
214 t.expect("author decode failed").toEqual("");
215 return;
216 }
217 t.expect("author decode failed").toEqual("");
218 return;
219 }
220 t.expect("not object").toEqual("");
221 });
222 Vitest.test("onAfterTransaction fires for any shared type change", async t => {
223 let doc = new Yjs.Doc();
224 let changeCount = {
225 contents: 0
226 };
227 Y.Doc.onAfterTransaction(doc, _event => {
228 changeCount.contents = changeCount.contents + 1 | 0;
229 });
230 doc.getText("text1");
231 doc.getArray("array1");
232 doc.getMap("map1");
233 t.expect(changeCount.contents).toBeGreaterThan(0);
234 });
235});
236
237export {
238 insertText,
239 pushArray,
240 setMap,
241}
242/* Not a pure module */