zulip/web/tests/server_events.test.js

140 lines
3.3 KiB
JavaScript

"use strict";
const {strict: assert} = require("assert");
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const blueslip = require("./lib/zblueslip");
const {page_params} = require("./lib/zpage_params");
const noop = () => {};
set_global("document", {
to_$() {
return {
trigger() {},
};
},
});
set_global("addEventListener", noop);
const channel = mock_esm("../src/channel");
const message_lists = mock_esm("../src/message_lists");
mock_esm("../src/reload_state", {
is_in_progress() {
return false;
},
});
message_lists.home = {
select_id: noop,
selected_id() {
return 1;
},
};
page_params.test_suite = false;
// we also directly write to pointer
set_global("pointer", {});
mock_esm("../src/ui_report", {
hide_error() {
return false;
},
});
mock_esm("../src/stream_events", {
update_property() {
throw new Error("subs update error");
},
});
const message_events = mock_esm("../src/message_events", {
insert_new_messages() {
throw new Error("insert error");
},
update_messages() {
throw new Error("update error");
},
});
const server_events = zrequire("server_events");
server_events.home_view_loaded();
run_test("message_event", ({override}) => {
const event = {
type: "message",
message: {
content: "hello",
},
flags: [],
};
let inserted;
override(message_events, "insert_new_messages", (messages) => {
assert.equal(messages[0].content, event.message.content);
inserted = true;
});
server_events._get_events_success([event]);
assert.ok(inserted);
});
// Start blueslip tests here
const setup = () => {
server_events.home_view_loaded();
};
run_test("event_dispatch_error", () => {
setup();
const data = {events: [{type: "stream", op: "update", id: 1, other: "thing"}]};
channel.get = (options) => {
options.success(data);
};
blueslip.expect("error", "Failed to process an event");
server_events.restart_get_events();
const logs = blueslip.get_test_logs("error");
assert.equal(logs.length, 1);
assert.equal(logs[0].more_info.event.type, "stream");
assert.equal(logs[0].more_info.event.op, "update");
assert.equal(logs[0].more_info.event.id, 1);
assert.equal(logs[0].more_info.other, undefined);
});
run_test("event_new_message_error", () => {
setup();
const data = {events: [{type: "message", id: 1, other: "thing", message: {}}]};
channel.get = (options) => {
options.success(data);
};
blueslip.expect("error", "Failed to insert new messages");
server_events.restart_get_events();
const logs = blueslip.get_test_logs("error");
assert.equal(logs.length, 1);
assert.equal(logs[0].more_info, undefined);
});
run_test("event_edit_message_error", () => {
setup();
const data = {events: [{type: "update_message", id: 1, other: "thing"}]};
channel.get = (options) => {
options.success(data);
};
blueslip.expect("error", "Failed to update messages");
server_events.restart_get_events();
const logs = blueslip.get_test_logs("error");
assert.equal(logs.length, 1);
assert.equal(logs[0].more_info, undefined);
});