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/table/test/changefeed.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 { Changefeed } from "../changefeed";
7
8
describe("first test of the public API of the Changefeed class", () => {
9
const options = [];
10
const query = {
11
system_notifications: { id: null, time: null, text: null, priority: null },
12
};
13
const table = "system_notifications";
14
15
const got: any = {};
16
const init_val = [
17
{ id: "0", time: new Date(0), text: "foo", priority: "low" },
18
];
19
function do_query(opts) {
20
got.do_query = opts;
21
opts.cb(undefined, { query: { [table]: init_val } });
22
}
23
function query_cancel(opts) {
24
got.query_cancel = opts;
25
opts.cb();
26
}
27
28
let changefeed: Changefeed;
29
const opts = {
30
do_query,
31
query_cancel,
32
options,
33
query,
34
table,
35
};
36
it("creates the changefeed", () => {
37
changefeed = new Changefeed(opts);
38
expect(changefeed.get_state()).toBe("disconnected");
39
});
40
41
it("initializes the changefeed", async () => {
42
const init = await changefeed.connect();
43
expect(changefeed.get_state()).toBe("connected");
44
expect(init).toBe(init_val);
45
});
46
47
it("causes an update", (done) => {
48
changefeed.on("update", (x) => {
49
expect(x).toEqual({ action: "insert", new_val: { text: "bar" } });
50
done();
51
});
52
got.do_query.cb(undefined, { action: "insert", new_val: { text: "bar" } });
53
});
54
55
it("ends the changefeed via query_cancel event", (done) => {
56
changefeed.on("close", () => {
57
expect(changefeed.get_state()).toBe("closed");
58
done();
59
});
60
got.do_query.cb(undefined, { event: "query_cancel" });
61
});
62
63
it("creates changefeed again", async () => {
64
changefeed = new Changefeed(opts);
65
await changefeed.connect();
66
expect(changefeed.get_state()).toBe("connected");
67
});
68
69
it("ends the changefeed via an error", (done) => {
70
changefeed.on("close", () => {
71
expect(changefeed.get_state()).toBe("closed");
72
done();
73
});
74
got.do_query.cb("fatal");
75
});
76
77
it("creates changefeed again", async () => {
78
changefeed = new Changefeed(opts);
79
await changefeed.connect();
80
expect(changefeed.get_state()).toBe("connected");
81
});
82
83
it("ends the changefeed via an error", (done) => {
84
changefeed.on("close", () => {
85
expect(changefeed.get_state()).toBe("closed");
86
done();
87
});
88
got.do_query.cb("fatal");
89
});
90
91
it("creates changefeed again", async () => {
92
changefeed = new Changefeed(opts);
93
await changefeed.connect();
94
expect(changefeed.get_state()).toBe("connected");
95
});
96
97
it("ends the changefeed by explicitly calling close", (done) => {
98
changefeed.on("close", () => {
99
expect(changefeed.get_state()).toBe("closed");
100
done();
101
});
102
changefeed.close();
103
});
104
});
105
106