2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-06-16 15:58:34 +02:00
|
|
|
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const noop = () => {};
|
2014-01-31 06:24:15 +01:00
|
|
|
|
2021-02-21 23:03:05 +01:00
|
|
|
set_global("document", {
|
|
|
|
to_$() {
|
|
|
|
return {
|
|
|
|
trigger() {},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("addEventListener", noop);
|
2021-02-21 23:03:05 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const channel = mock_esm("../../static/js/channel");
|
2021-03-30 02:21:21 +02:00
|
|
|
const message_lists = mock_esm("../../static/js/message_lists");
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/reload_state", {
|
2021-03-06 17:37:51 +01:00
|
|
|
is_in_progress() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
});
|
2021-03-30 02:21:21 +02:00
|
|
|
message_lists.home = {
|
2016-08-05 21:42:19 +02:00
|
|
|
select_id: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
selected_id() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return 1;
|
|
|
|
},
|
2021-03-30 02:21:21 +02:00
|
|
|
};
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.test_suite = false;
|
2014-01-31 06:24:15 +01:00
|
|
|
|
2017-07-01 14:21:20 +02:00
|
|
|
// we also directly write to pointer
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("pointer", {});
|
2017-07-01 14:21:20 +02:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/ui_report", {
|
2020-07-20 22:18:43 +02:00
|
|
|
hide_error() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return false;
|
|
|
|
},
|
2017-03-23 20:37:08 +01:00
|
|
|
});
|
2016-08-05 21:42:19 +02:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/stream_events", {
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
update_property() {
|
|
|
|
throw new Error("subs update error");
|
|
|
|
},
|
|
|
|
});
|
2021-02-28 18:48:44 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const message_events = mock_esm("../../static/js/message_events", {
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
insert_new_messages() {
|
|
|
|
throw new Error("insert error");
|
|
|
|
},
|
|
|
|
update_messages() {
|
|
|
|
throw new Error("update error");
|
|
|
|
},
|
|
|
|
});
|
2021-02-28 00:39:51 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const server_events = zrequire("server_events");
|
|
|
|
|
2017-07-01 14:21:20 +02:00
|
|
|
server_events.home_view_loaded();
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("message_event", ({override}) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "message",
|
2017-07-01 14:21:20 +02:00
|
|
|
message: {
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "hello",
|
2017-07-01 14:21:20 +02:00
|
|
|
},
|
|
|
|
flags: [],
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let inserted;
|
2021-02-28 18:48:44 +01:00
|
|
|
override(message_events, "insert_new_messages", (messages) => {
|
|
|
|
assert.equal(messages[0].content, event.message.content);
|
|
|
|
inserted = true;
|
2017-07-01 14:21:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
server_events._get_events_success([event]);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(inserted);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-01 14:21:20 +02:00
|
|
|
|
2016-08-05 21:42:19 +02:00
|
|
|
// Start blueslip tests here
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const setup = () => {
|
2014-01-31 06:24:15 +01:00
|
|
|
server_events.home_view_loaded();
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("event_dispatch_error", () => {
|
2018-07-10 10:16:42 +02:00
|
|
|
setup();
|
2014-01-31 06:24:15 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const data = {events: [{type: "stream", op: "update", id: 1, other: "thing"}]};
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.get = (options) => {
|
2014-01-31 06:24:15 +01:00
|
|
|
options.success(data);
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Failed to process an event\nsubs update error");
|
2018-07-10 10:16:42 +02:00
|
|
|
|
2014-01-31 06:24:15 +01:00
|
|
|
server_events.restart_get_events();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const logs = blueslip.get_test_logs("error");
|
2018-07-10 10:16:42 +02:00
|
|
|
assert.equal(logs.length, 1);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(logs[0].more_info.event.type, "stream");
|
|
|
|
assert.equal(logs[0].more_info.event.op, "update");
|
2018-12-07 22:14:28 +01:00
|
|
|
assert.equal(logs[0].more_info.event.id, 1);
|
|
|
|
assert.equal(logs[0].more_info.other, undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-01-31 06:24:15 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("event_new_message_error", () => {
|
2018-07-10 10:16:42 +02:00
|
|
|
setup();
|
2014-01-31 06:24:15 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const data = {events: [{type: "message", id: 1, other: "thing", message: {}}]};
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.get = (options) => {
|
2014-01-31 06:24:15 +01:00
|
|
|
options.success(data);
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Failed to insert new messages\ninsert error");
|
2018-07-10 10:16:42 +02:00
|
|
|
|
2014-01-31 06:24:15 +01:00
|
|
|
server_events.restart_get_events();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const logs = blueslip.get_test_logs("error");
|
2018-07-10 10:16:42 +02:00
|
|
|
assert.equal(logs.length, 1);
|
|
|
|
assert.equal(logs[0].more_info, undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2014-01-31 06:24:15 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("event_edit_message_error", () => {
|
2018-07-10 10:16:42 +02:00
|
|
|
setup();
|
2020-07-15 01:29:15 +02:00
|
|
|
const data = {events: [{type: "update_message", id: 1, other: "thing"}]};
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.get = (options) => {
|
2014-01-31 06:24:15 +01:00
|
|
|
options.success(data);
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Failed to update messages\nupdate error");
|
2014-01-31 06:24:15 +01:00
|
|
|
|
|
|
|
server_events.restart_get_events();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const logs = blueslip.get_test_logs("error");
|
2018-07-10 10:16:42 +02:00
|
|
|
assert.equal(logs.length, 1);
|
|
|
|
assert.equal(logs[0].more_info, undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|