2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
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
|
|
|
const {rewiremock, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const noop = () => {};
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2021-02-23 03:54:07 +01:00
|
|
|
const page_params = set_global("page_params", {});
|
2021-03-06 17:37:51 +01:00
|
|
|
const channel = rewiremock("../../static/js/channel").with({});
|
|
|
|
const reload = rewiremock("../../static/js/reload").with({});
|
|
|
|
const reload_state = rewiremock("../../static/js/reload_state").with({});
|
|
|
|
|
2021-02-28 00:50:19 +01:00
|
|
|
const sent_messages = {
|
2018-02-20 13:08:50 +01:00
|
|
|
start_tracking_message: noop,
|
|
|
|
report_server_ack: noop,
|
2021-02-28 00:50:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
rewiremock("../../static/js/sent_messages").with(sent_messages);
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const transmit = zrequire("transmit");
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("transmit_message_ajax", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let success_func_called;
|
2021-02-23 14:37:26 +01:00
|
|
|
const success = () => {
|
2018-02-20 13:08:50 +01:00
|
|
|
success_func_called = true;
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const request = {foo: "bar"};
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/messages");
|
|
|
|
assert.equal(opts.data.foo, "bar");
|
2018-02-20 13:08:50 +01:00
|
|
|
opts.success();
|
|
|
|
};
|
|
|
|
|
|
|
|
transmit.send_message(request, success);
|
|
|
|
|
|
|
|
assert(success_func_called);
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.xhr_error_message = (msg) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(msg, "Error sending message");
|
2018-02-20 13:08:50 +01:00
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/messages");
|
|
|
|
assert.equal(opts.data.foo, "bar");
|
|
|
|
const xhr = "whatever";
|
|
|
|
opts.error(xhr, "timeout");
|
2018-02-20 13:08:50 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let error_func_called;
|
2021-02-23 14:37:26 +01:00
|
|
|
const error = (response) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(response, "Error sending message");
|
2018-02-20 13:08:50 +01:00
|
|
|
error_func_called = true;
|
|
|
|
};
|
|
|
|
transmit.send_message(request, success, error);
|
|
|
|
assert(error_func_called);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("transmit_message_ajax_reload_pending", () => {
|
2021-02-23 14:37:26 +01:00
|
|
|
const success = () => {
|
2020-10-07 13:01:09 +02:00
|
|
|
throw new Error("unexpected success");
|
2020-07-15 00:34:28 +02:00
|
|
|
};
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
reload_state.is_pending = () => true;
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let reload_initiated;
|
2021-02-23 14:37:26 +01:00
|
|
|
reload.initiate = (opts) => {
|
2018-02-20 13:08:50 +01:00
|
|
|
reload_initiated = true;
|
|
|
|
assert.deepEqual(opts, {
|
2018-05-07 03:30:13 +02:00
|
|
|
immediate: true,
|
|
|
|
save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
|
|
|
send_after_reload: true,
|
2018-02-20 13:08:50 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const request = {foo: "bar"};
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let error_func_called;
|
2021-02-23 14:37:26 +01:00
|
|
|
const error = (response) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(response, "Error sending message");
|
2018-02-20 13:08:50 +01:00
|
|
|
error_func_called = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
error_func_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/messages");
|
|
|
|
assert.equal(opts.data.foo, "bar");
|
|
|
|
const xhr = "whatever";
|
|
|
|
opts.error(xhr, "bad request");
|
2018-02-20 13:08:50 +01:00
|
|
|
};
|
|
|
|
transmit.send_message(request, success, error);
|
|
|
|
assert(!error_func_called);
|
|
|
|
assert(reload_initiated);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("reply_message_stream", () => {
|
2018-02-23 16:18:27 +01:00
|
|
|
const stream_message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
|
|
|
stream: "social",
|
|
|
|
topic: "lunch",
|
|
|
|
sender_full_name: "Alice",
|
2018-10-13 02:31:49 +02:00
|
|
|
sender_id: 123,
|
2018-02-23 16:18:27 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const content = "hello";
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let send_message_args;
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2021-02-28 00:51:22 +01:00
|
|
|
transmit.__Rewire__("send_message", (args) => {
|
2018-02-23 16:18:27 +01:00
|
|
|
send_message_args = args;
|
2021-02-28 00:51:22 +01:00
|
|
|
});
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
page_params.user_id = 44;
|
|
|
|
page_params.queue_id = 66;
|
2020-02-12 09:32:25 +01:00
|
|
|
sent_messages.get_new_local_id = () => "99";
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
transmit.reply_message({
|
|
|
|
message: stream_message,
|
2020-07-20 22:18:43 +02:00
|
|
|
content,
|
2018-02-23 16:18:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(send_message_args, {
|
|
|
|
sender_id: 44,
|
|
|
|
queue_id: 66,
|
2020-07-15 01:29:15 +02:00
|
|
|
local_id: "99",
|
|
|
|
type: "stream",
|
|
|
|
to: "social",
|
|
|
|
content: "@**Alice** hello",
|
|
|
|
topic: "lunch",
|
2018-02-23 16:18:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("reply_message_private", () => {
|
2018-02-23 16:18:27 +01:00
|
|
|
const fred = {
|
|
|
|
user_id: 3,
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "fred@example.com",
|
|
|
|
full_name: "Fred Frost",
|
2018-02-23 16:18:27 +01:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(fred);
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
people.is_my_user_id = () => false;
|
|
|
|
|
|
|
|
const pm_message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
2020-07-15 00:34:28 +02:00
|
|
|
display_recipient: [{id: fred.user_id}],
|
2018-02-23 16:18:27 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const content = "hello";
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let send_message_args;
|
2018-02-23 16:18:27 +01:00
|
|
|
|
2021-02-28 00:51:22 +01:00
|
|
|
transmit.__Rewire__("send_message", (args) => {
|
2018-02-23 16:18:27 +01:00
|
|
|
send_message_args = args;
|
2021-02-28 00:51:22 +01:00
|
|
|
});
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
page_params.user_id = 155;
|
|
|
|
page_params.queue_id = 177;
|
2020-02-12 09:32:25 +01:00
|
|
|
sent_messages.get_new_local_id = () => "199";
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
transmit.reply_message({
|
|
|
|
message: pm_message,
|
2020-07-20 22:18:43 +02:00
|
|
|
content,
|
2018-02-23 16:18:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(send_message_args, {
|
|
|
|
sender_id: 155,
|
|
|
|
queue_id: 177,
|
2020-07-15 01:29:15 +02:00
|
|
|
local_id: "199",
|
|
|
|
type: "private",
|
2018-02-23 16:18:27 +01:00
|
|
|
to: '["fred@example.com"]',
|
2020-07-15 01:29:15 +02:00
|
|
|
content: "hello",
|
2018-02-23 16:18:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("reply_message_errors", () => {
|
2018-02-23 16:18:27 +01:00
|
|
|
const bogus_message = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "bogus",
|
2018-02-23 16:18:27 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "unknown message type: bogus");
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
transmit.reply_message({
|
|
|
|
message: bogus_message,
|
|
|
|
});
|
|
|
|
});
|