2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
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");
|
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
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const channel = mock_esm("../src/channel");
|
|
|
|
const message_lists = mock_esm("../src/message_lists");
|
|
|
|
mock_esm("../src/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
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/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
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/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
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const message_events = mock_esm("../src/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);
|
|
|
|
};
|
|
|
|
|
2023-03-29 19:07:36 +02:00
|
|
|
blueslip.expect("error", "Failed to process an event");
|
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);
|
|
|
|
};
|
|
|
|
|
2023-03-29 19:07:36 +02:00
|
|
|
blueslip.expect("error", "Failed to insert new messages");
|
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);
|
|
|
|
};
|
2023-03-29 19:07:36 +02:00
|
|
|
blueslip.expect("error", "Failed to update messages");
|
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
|
|
|
});
|