CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/test/doc.test.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { StringDocument } from "../doc";
7
8
describe("create a String document and call methods on it", () => {
9
let doc: StringDocument;
10
11
it("creates the doc", () => {
12
doc = new StringDocument("cocalc");
13
expect(`${doc}`).toBe("[object Object]");
14
});
15
16
it("convert to string", () => {
17
expect(doc.to_str()).toBe("cocalc");
18
});
19
20
it("checks equality", () => {
21
expect(doc.is_equal()).toBe(false);
22
expect(doc.is_equal(undefined)).toBe(false);
23
expect(doc.is_equal(doc)).toBe(true);
24
expect(doc.is_equal(new StringDocument("cocalc"))).toBe(true);
25
expect(doc.is_equal(new StringDocument("sagemathcloud"))).toBe(false);
26
});
27
28
it("make and apply a patch", () => {
29
const patch = doc.make_patch(new StringDocument("CoCalc"));
30
expect(doc.apply_patch(patch).to_str()).toBe("CoCalc");
31
});
32
33
it("set the document (thus making a new one, since immutable!)", () => {
34
const d2 = doc.set("CoCalc");
35
expect(doc.to_str()).toBe("cocalc");
36
expect(d2.to_str()).toBe("CoCalc");
37
38
// also test type checking
39
expect(() => {
40
doc.set(0);
41
}).toThrow("must be a string");
42
});
43
44
it("tests that the db-like API all throws errors", () => {
45
expect(() => doc.get()).toThrow("don't have meaning");
46
expect(() => doc.get_one()).toThrow("don't have meaning");
47
expect(() => doc.delete()).toThrow("doesn't have meaning");
48
expect(doc.changes(doc)).toBe(undefined);
49
});
50
51
it("count gives length", () => {
52
expect(doc.count()).toBe(doc.to_str().length);
53
});
54
});
55
56