Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/sync/table/test/changefeed.test.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Changefeed } from "../changefeed";67describe("first test of the public API of the Changefeed class", () => {8const options = [];9const query = {10system_notifications: { id: null, time: null, text: null, priority: null },11};12const table = "system_notifications";1314const got: any = {};15const init_val = [16{ id: "0", time: new Date(0), text: "foo", priority: "low" },17];18function do_query(opts) {19got.do_query = opts;20opts.cb(undefined, { query: { [table]: init_val } });21}22function query_cancel(opts) {23got.query_cancel = opts;24opts.cb();25}2627let changefeed: Changefeed;28const opts = {29do_query,30query_cancel,31options,32query,33table,34};35it("creates the changefeed", () => {36changefeed = new Changefeed(opts);37expect(changefeed.get_state()).toBe("disconnected");38});3940it("initializes the changefeed", async () => {41const init = await changefeed.connect();42expect(changefeed.get_state()).toBe("connected");43expect(init).toBe(init_val);44});4546it("causes an update", (done) => {47changefeed.on("update", (x) => {48expect(x).toEqual({ action: "insert", new_val: { text: "bar" } });49done();50});51got.do_query.cb(undefined, { action: "insert", new_val: { text: "bar" } });52});5354it("ends the changefeed via query_cancel event", (done) => {55changefeed.on("close", () => {56expect(changefeed.get_state()).toBe("closed");57done();58});59got.do_query.cb(undefined, { event: "query_cancel" });60});6162it("creates changefeed again", async () => {63changefeed = new Changefeed(opts);64await changefeed.connect();65expect(changefeed.get_state()).toBe("connected");66});6768it("ends the changefeed via an error", (done) => {69changefeed.on("close", () => {70expect(changefeed.get_state()).toBe("closed");71done();72});73got.do_query.cb("fatal");74});7576it("creates changefeed again", async () => {77changefeed = new Changefeed(opts);78await changefeed.connect();79expect(changefeed.get_state()).toBe("connected");80});8182it("ends the changefeed via an error", (done) => {83changefeed.on("close", () => {84expect(changefeed.get_state()).toBe("closed");85done();86});87got.do_query.cb("fatal");88});8990it("creates changefeed again", async () => {91changefeed = new Changefeed(opts);92await changefeed.connect();93expect(changefeed.get_state()).toBe("connected");94});9596it("ends the changefeed by explicitly calling close", (done) => {97changefeed.on("close", () => {98expect(changefeed.get_state()).toBe("closed");99done();100});101changefeed.close();102});103});104105106