2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
const {JSDOM} = require("jsdom");
|
2021-02-05 21:20:14 +01:00
|
|
|
const MockDate = require("mockdate");
|
2020-02-28 23:59:07 +01:00
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
const {stub_templates} = require("../zjsunit/handlebars");
|
2021-04-13 06:51:54 +02:00
|
|
|
const {$t, $t_html} = require("../zjsunit/i18n");
|
2021-03-11 05:43:45 +01:00
|
|
|
const {mock_cjs, 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-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const noop = () => {};
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("DOMParser", new JSDOM().window.DOMParser);
|
2021-02-28 18:48:44 +01:00
|
|
|
|
|
|
|
let compose_actions_start_checked;
|
|
|
|
let compose_actions_expected_opts;
|
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/compose_actions", {
|
2020-07-29 17:32:39 +02:00
|
|
|
update_placeholder_text: noop,
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2021-02-28 18:48:44 +01:00
|
|
|
start(msg_type, opts) {
|
|
|
|
assert.equal(msg_type, "stream");
|
|
|
|
assert.deepEqual(opts, compose_actions_expected_opts);
|
|
|
|
compose_actions_start_checked = true;
|
|
|
|
},
|
2020-07-29 17:32:39 +02:00
|
|
|
});
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const server_events = mock_esm("../../static/js/server_events");
|
2018-08-01 21:17:03 +02:00
|
|
|
const _navigator = {
|
2020-07-15 01:29:15 +02:00
|
|
|
platform: "",
|
2018-08-01 21:17:03 +02:00
|
|
|
};
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2018-08-01 21:17:03 +02:00
|
|
|
const _document = {
|
2020-07-20 22:18:43 +02:00
|
|
|
execCommand() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return false;
|
|
|
|
},
|
2017-12-06 14:46:23 +01:00
|
|
|
location: {},
|
2020-12-01 23:21:38 +01:00
|
|
|
to_$: () => $("document-stub"),
|
2018-08-01 21:17:03 +02:00
|
|
|
};
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", _document);
|
2021-03-10 06:10:32 +01:00
|
|
|
const channel = mock_esm("../../static/js/channel");
|
|
|
|
const loading = mock_esm("../../static/js/loading");
|
|
|
|
const markdown = mock_esm("../../static/js/markdown");
|
|
|
|
const reminder = mock_esm("../../static/js/reminder", {
|
2021-03-07 13:57:14 +01:00
|
|
|
is_deferred_delivery: noop,
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
const resize = mock_esm("../../static/js/resize");
|
|
|
|
const sent_messages = mock_esm("../../static/js/sent_messages", {
|
2021-03-07 13:57:14 +01:00
|
|
|
start_tracking_message: noop,
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
const stream_edit = mock_esm("../../static/js/stream_edit");
|
|
|
|
const subs = mock_esm("../../static/js/subs");
|
|
|
|
const transmit = mock_esm("../../static/js/transmit");
|
|
|
|
const ui_util = mock_esm("../../static/js/ui_util");
|
|
|
|
mock_esm("../../static/js/drafts", {
|
2021-03-07 13:57:14 +01:00
|
|
|
delete_draft_after_send: noop,
|
|
|
|
});
|
2021-04-15 16:18:36 +02:00
|
|
|
mock_esm("../../static/js/giphy", {
|
|
|
|
is_giphy_enabled: () => true,
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/notifications", {
|
2021-03-07 13:57:14 +01:00
|
|
|
notify_above_composebox: noop,
|
|
|
|
clear_compose_notifications: noop,
|
|
|
|
});
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/rendered_markdown", {
|
2021-02-24 22:31:50 +01:00
|
|
|
update_elements: () => {},
|
|
|
|
});
|
2021-04-28 21:29:12 +02:00
|
|
|
mock_esm("../../static/js/settings_data", {
|
|
|
|
user_can_subscribe_other_users: () => true,
|
|
|
|
});
|
2021-03-06 17:37:51 +01:00
|
|
|
set_global("navigator", _navigator);
|
2021-02-10 04:53:22 +01:00
|
|
|
|
2017-07-06 21:57:25 +02:00
|
|
|
// Setting these up so that we can test that links to uploads within messages are
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2017-07-06 21:57:25 +02:00
|
|
|
// automatically converted to server relative links.
|
2020-12-01 00:57:57 +01:00
|
|
|
document.location.protocol = "https:";
|
|
|
|
document.location.host = "foo.com";
|
2020-07-15 01:29:15 +02:00
|
|
|
|
2021-02-05 21:20:14 +01:00
|
|
|
const fake_now = 555;
|
|
|
|
MockDate.set(new Date(fake_now * 1000));
|
|
|
|
|
2021-05-11 22:04:35 +02:00
|
|
|
const compose_closed_ui = zrequire("compose_closed_ui");
|
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 compose_fade = zrequire("compose_fade");
|
2021-01-12 21:38:01 +01:00
|
|
|
const peer_data = zrequire("peer_data");
|
2020-07-15 01:29:15 +02:00
|
|
|
const util = zrequire("util");
|
2021-02-10 04:53:22 +01:00
|
|
|
const rtl = zrequire("rtl");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const compose_state = zrequire("compose_state");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const compose_pm_pill = zrequire("compose_pm_pill");
|
|
|
|
const echo = zrequire("echo");
|
2021-02-24 22:31:50 +01:00
|
|
|
const compose = zrequire("compose");
|
2021-02-10 04:53:22 +01:00
|
|
|
const upload = zrequire("upload");
|
2020-09-11 16:07:06 +02:00
|
|
|
const settings_config = zrequire("settings_config");
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
people.small_avatar_url_for_person = () => "http://example.com/example.png";
|
2018-06-27 20:55:56 +02:00
|
|
|
|
2020-04-08 00:23:15 +02:00
|
|
|
function reset_jquery() {
|
|
|
|
// Avoid leaks.
|
2021-02-21 15:01:54 +01:00
|
|
|
$.clear_all_elements();
|
2020-04-08 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2020-02-04 21:50:55 +01:00
|
|
|
const new_user = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "new_user@example.com",
|
2020-02-04 21:50:55 +01:00
|
|
|
user_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "New User",
|
2020-02-04 21:50:55 +01:00
|
|
|
date_joined: new Date(),
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "me@example.com",
|
2017-02-24 23:51:23 +01:00
|
|
|
user_id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Me Myself",
|
2018-08-13 00:46:29 +02:00
|
|
|
date_joined: new Date(),
|
2017-02-24 23:51:23 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2017-02-24 23:51:23 +01:00
|
|
|
user_id: 31,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2017-02-24 23:51:23 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bob@example.com",
|
2017-02-24 23:51:23 +01:00
|
|
|
user_id: 32,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bob",
|
2017-02-24 23:51:23 +01:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(new_user);
|
|
|
|
people.add_active_user(me);
|
2017-02-24 23:51:23 +01:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(bob);
|
2017-02-24 23:51:23 +01:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
function test_ui(label, f) {
|
2021-02-23 12:05:28 +01:00
|
|
|
// The sloppy_$ flag lets us re-use setup from prior tests.
|
|
|
|
run_test(label, f, {sloppy_$: true});
|
2021-02-23 12:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
test_ui("validate_stream_message_address_info", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2017-06-28 12:55:04 +02:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "social",
|
2017-06-28 12:55:04 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(compose.validate_stream_message_address_info("social"));
|
2017-06-28 13:10:46 +02:00
|
|
|
|
2017-06-28 19:24:53 +02:00
|
|
|
sub.subscribed = false;
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(template_name, "compose_not_subscribed");
|
|
|
|
return "compose_not_subscribed_stub";
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!compose.validate_stream_message_address_info("social"));
|
|
|
|
assert.equal($("#compose-error-msg").html(), "compose_not_subscribed_stub");
|
2017-06-28 19:24:53 +02:00
|
|
|
|
2018-08-01 21:17:03 +02:00
|
|
|
page_params.narrow_stream = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (payload) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(payload.data.stream, "social");
|
2017-06-28 19:24:53 +02:00
|
|
|
payload.data.subscribed = true;
|
|
|
|
payload.success(payload.data);
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(compose.validate_stream_message_address_info("social"));
|
2017-06-28 19:24:53 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
sub.name = "Frontend";
|
2017-06-28 19:24:53 +02:00
|
|
|
sub.stream_id = 102;
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (payload) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(payload.data.stream, "Frontend");
|
2017-06-28 19:24:53 +02:00
|
|
|
payload.data.subscribed = false;
|
|
|
|
payload.success(payload.data);
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!compose.validate_stream_message_address_info("Frontend"));
|
|
|
|
assert.equal($("#compose-error-msg").html(), "compose_not_subscribed_stub");
|
2017-06-28 19:24:53 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (payload) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(payload.data.stream, "Frontend");
|
2017-06-28 19:24:53 +02:00
|
|
|
payload.error({status: 404});
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!compose.validate_stream_message_address_info("Frontend"));
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
"translated HTML: <p>The stream <b>Frontend</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-06-28 19:24:53 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (payload) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(payload.data.stream, "social");
|
2017-06-28 19:24:53 +02:00
|
|
|
payload.error({status: 500});
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!compose.validate_stream_message_address_info("social"));
|
2021-04-13 01:44:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Error checking subscription"}),
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-02-24 23:51:23 +01:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("validate", () => {
|
2018-03-06 15:07:55 +01:00
|
|
|
function initialize_pm_pill() {
|
2021-02-21 15:01:54 +01:00
|
|
|
reset_jquery();
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-send-button").trigger("focus");
|
2018-03-06 15:07:55 +01:00
|
|
|
$("#sending-indicator").hide();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const pm_pill_container = $.create("fake-pm-pill-container");
|
2020-08-01 03:56:10 +02:00
|
|
|
$("#private_message_recipient")[0] = {};
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#private_message_recipient").set_parent(pm_pill_container);
|
|
|
|
pm_pill_container.set_find_results(".input", $("#private_message_recipient"));
|
|
|
|
$("#private_message_recipient").before = noop;
|
2018-03-06 15:07:55 +01:00
|
|
|
|
|
|
|
compose_pm_pill.initialize();
|
|
|
|
|
2018-08-01 21:17:03 +02:00
|
|
|
ui_util.place_caret_at_end = noop;
|
2018-03-06 15:07:55 +01:00
|
|
|
|
|
|
|
$("#zephyr-mirror-error").is = noop;
|
2018-03-31 13:22:29 +02:00
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((fn) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(fn, "input_pill");
|
|
|
|
return "<div>pill-html</div>";
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-03-06 15:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function add_content_to_compose_box() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("foobarfoobar");
|
2018-03-06 15:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize_pm_pill();
|
2017-06-27 14:55:11 +02:00
|
|
|
assert(!compose.validate());
|
|
|
|
assert(!$("#sending-indicator").visible());
|
|
|
|
assert(!$("#compose-send-button").is_focused());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2021-04-13 01:44:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "You have nothing to send!"}),
|
|
|
|
);
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2018-07-26 09:46:10 +02:00
|
|
|
reminder.is_deferred_delivery = () => true;
|
|
|
|
compose.validate();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#sending-indicator").text(), "translated: Scheduling...");
|
2018-07-26 09:46:10 +02:00
|
|
|
reminder.is_deferred_delivery = noop;
|
|
|
|
|
2018-03-06 15:07:55 +01:00
|
|
|
add_content_to_compose_box();
|
2019-11-02 00:06:25 +01:00
|
|
|
let zephyr_checked = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#zephyr-mirror-error").is = () => {
|
2017-06-27 14:55:11 +02:00
|
|
|
if (!zephyr_checked) {
|
|
|
|
zephyr_checked = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
assert(!compose.validate());
|
|
|
|
assert(zephyr_checked);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({
|
|
|
|
defaultMessage: "You need to be running Zephyr mirroring in order to send messages!",
|
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2018-03-06 15:07:55 +01:00
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
|
|
|
|
2018-07-23 08:19:48 +02:00
|
|
|
// test validating private messages
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("private");
|
2018-07-23 08:19:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.private_message_recipient("");
|
2017-06-27 14:55:11 +02:00
|
|
|
assert(!compose.validate());
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2018-03-06 15:07:55 +01:00
|
|
|
initialize_pm_pill();
|
|
|
|
add_content_to_compose_box();
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.private_message_recipient("foo@zulip.com");
|
2017-06-27 14:55:11 +02:00
|
|
|
|
|
|
|
assert(!compose.validate());
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.private_message_recipient("foo@zulip.com,alice@zulip.com");
|
2017-06-27 14:55:11 +02:00
|
|
|
assert(!compose.validate());
|
2018-03-06 15:07:55 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({defaultMessage: "Please specify at least one valid recipient"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(bob);
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.private_message_recipient("bob@example.com");
|
2017-06-27 14:55:11 +02:00
|
|
|
assert(compose.validate());
|
|
|
|
|
2018-07-23 08:19:48 +02:00
|
|
|
page_params.realm_is_zephyr_mirror_realm = true;
|
|
|
|
assert(compose.validate());
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
compose_state.stream_name("");
|
2017-06-27 14:55:11 +02:00
|
|
|
assert(!compose.validate());
|
2021-04-13 01:44:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Please specify a stream"}),
|
|
|
|
);
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.stream_name("Denmark");
|
2018-08-01 21:17:03 +02:00
|
|
|
page_params.realm_mandatory_topics = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.topic("");
|
2017-06-27 14:55:11 +02:00
|
|
|
assert(!compose.validate());
|
2021-04-13 01:44:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "Please specify a topic"}),
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-27 14:55:11 +02:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
test_ui("get_invalid_recipient_emails", (override) => {
|
2020-01-18 08:56:19 +01:00
|
|
|
const welcome_bot = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "welcome-bot@example.com",
|
2017-06-28 21:36:57 +02:00
|
|
|
user_id: 124,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Welcome Bot",
|
2017-06-28 21:36:57 +02:00
|
|
|
};
|
2020-02-25 12:16:26 +01:00
|
|
|
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.user_id = me.user_id;
|
2020-02-25 12:16:26 +01:00
|
|
|
|
|
|
|
const params = {};
|
|
|
|
params.realm_users = [];
|
|
|
|
params.realm_non_active_users = [];
|
|
|
|
params.cross_realm_bots = [welcome_bot];
|
|
|
|
|
|
|
|
people.initialize(page_params.user_id, params);
|
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
override(compose_state, "private_message_recipient", () => "welcome-bot@example.com");
|
2017-06-28 21:36:57 +02:00
|
|
|
assert.deepEqual(compose.get_invalid_recipient_emails(), []);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 21:36:57 +02:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("test_wildcard_mention_allowed", () => {
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.user_id = me.user_id;
|
|
|
|
|
2020-09-11 16:07:06 +02:00
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_everyone.code;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert(compose.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.nobody.code;
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert(!compose.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_members.code;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert(!compose.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.is_guest = false;
|
|
|
|
assert(compose.wildcard_mention_allowed());
|
|
|
|
|
2021-05-02 10:11:02 +02:00
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_moderators_only.code;
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
assert(!compose.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
assert(compose.wildcard_mention_allowed());
|
|
|
|
|
2020-09-11 16:07:06 +02:00
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_stream_admins_only.code;
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert(!compose.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
// TODO: Add a by_admins_only case when we implement stream-level administrators.
|
|
|
|
|
|
|
|
page_params.is_admin = true;
|
|
|
|
assert(compose.wildcard_mention_allowed());
|
|
|
|
|
|
|
|
page_params.realm_wildcard_mention_policy =
|
|
|
|
settings_config.wildcard_mention_policy_values.by_full_members.code;
|
|
|
|
const person = people.get_by_user_id(page_params.user_id);
|
|
|
|
person.date_joined = new Date(Date.now());
|
|
|
|
page_params.realm_waiting_period_threshold = 10;
|
|
|
|
|
|
|
|
assert(compose.wildcard_mention_allowed());
|
|
|
|
page_params.is_admin = false;
|
|
|
|
assert(!compose.wildcard_mention_allowed());
|
|
|
|
});
|
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("validate_stream_message", (override) => {
|
2017-06-28 20:28:18 +02:00
|
|
|
// This test is in kind of continuation to test_validate but since it is
|
2017-11-09 16:26:38 +01:00
|
|
|
// primarily used to get coverage over functions called from validate()
|
|
|
|
// we are separating it up in different test. Though their relative position
|
2017-06-28 20:28:18 +02:00
|
|
|
// of execution should not be changed.
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.user_id = me.user_id;
|
2018-08-01 21:17:03 +02:00
|
|
|
page_params.realm_mandatory_topics = false;
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2017-06-28 20:28:18 +02:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "social",
|
2017-06-28 20:28:18 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.stream_name("social");
|
2017-06-28 20:28:18 +02:00
|
|
|
assert(compose.validate());
|
|
|
|
assert(!$("#compose-all-everyone").visible());
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$("#compose-send-status").visible());
|
2017-06-28 20:28:18 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
peer_data.get_subscriber_count = (stream_id) => {
|
2020-07-06 12:36:55 +02:00
|
|
|
assert.equal(stream_id, 101);
|
2017-06-28 20:28:18 +02:00
|
|
|
return 16;
|
|
|
|
};
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(template_name, "compose_all_everyone");
|
2017-06-28 20:28:18 +02:00
|
|
|
assert.equal(data.count, 16);
|
2020-07-15 01:29:15 +02:00
|
|
|
return "compose_all_everyone_stub";
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
let compose_content;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#compose-all-everyone").append = (data) => {
|
2017-06-28 20:28:18 +02:00
|
|
|
compose_content = data;
|
|
|
|
};
|
2020-09-11 16:07:06 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "wildcard_mention_allowed", () => true);
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.message_content("Hey @**all**");
|
2017-06-28 20:28:18 +02:00
|
|
|
assert(!compose.validate());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$("#compose-send-status").visible());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(compose_content, "compose_all_everyone_stub");
|
2017-06-28 20:28:18 +02:00
|
|
|
assert($("#compose-all-everyone").visible());
|
2020-09-11 16:07:06 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "wildcard_mention_allowed", () => false);
|
2020-09-11 16:07:06 +02:00
|
|
|
assert(!compose.validate());
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({
|
|
|
|
defaultMessage: "You do not have permission to use wildcard mentions in this stream.",
|
|
|
|
}),
|
2020-09-11 16:07:06 +02:00
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 20:28:18 +02:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("test_validate_stream_message_post_policy_admin_only", () => {
|
2020-03-28 01:25:56 +01:00
|
|
|
// This test is in continuation with test_validate but it has been separated out
|
|
|
|
// for better readability. Their relative position of execution should not be changed.
|
2018-05-14 12:06:56 +02:00
|
|
|
// Although the position with respect to test_validate_stream_message does not matter
|
2020-07-06 12:36:55 +02:00
|
|
|
// as different stream is used for this test.
|
2018-08-01 21:17:03 +02:00
|
|
|
page_params.is_admin = false;
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2018-05-14 12:06:56 +02:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "stream102",
|
2018-05-14 12:06:56 +02:00
|
|
|
subscribed: true,
|
2020-02-04 21:50:55 +01:00
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.admins.code,
|
2018-05-14 12:06:56 +02:00
|
|
|
};
|
2020-07-06 12:36:55 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.topic("subject102");
|
|
|
|
compose_state.stream_name("stream102");
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2018-05-14 12:06:56 +02:00
|
|
|
assert(!compose.validate());
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({defaultMessage: "Only organization admins are allowed to post to this stream."}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2018-05-14 12:06:56 +02:00
|
|
|
|
2020-10-08 20:30:11 +02:00
|
|
|
// Reset error message.
|
|
|
|
compose_state.stream_name("social");
|
|
|
|
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
|
|
|
|
compose_state.topic("subject102");
|
|
|
|
compose_state.stream_name("stream102");
|
|
|
|
assert(!compose.validate());
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({defaultMessage: "Only organization admins are allowed to post to this stream."}),
|
2020-10-08 20:30:11 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-05-01 16:19:33 +02:00
|
|
|
test_ui("test_validate_stream_message_post_policy_moderators_only", () => {
|
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_moderator = false;
|
|
|
|
page_params.is_guest = false;
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
stream_id: 104,
|
|
|
|
name: "stream104",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.moderators.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
compose_state.topic("subject104");
|
|
|
|
compose_state.stream_name("stream104");
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
assert(!compose.validate());
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({
|
|
|
|
defaultMessage:
|
|
|
|
"Only organization admins and moderators are allowed to post to this stream.",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reset error message.
|
|
|
|
compose_state.stream_name("social");
|
|
|
|
|
|
|
|
page_params.is_guest = true;
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({
|
|
|
|
defaultMessage:
|
|
|
|
"Only organization admins and moderators are allowed to post to this stream.",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("test_validate_stream_message_post_policy_full_members_only", () => {
|
2020-10-08 20:30:11 +02:00
|
|
|
page_params.is_admin = false;
|
|
|
|
page_params.is_guest = true;
|
|
|
|
const sub = {
|
|
|
|
stream_id: 103,
|
|
|
|
name: "stream103",
|
|
|
|
subscribed: true,
|
|
|
|
stream_post_policy: stream_data.stream_post_policy_values.non_new_members.code,
|
|
|
|
};
|
|
|
|
|
|
|
|
compose_state.topic("subject103");
|
|
|
|
compose_state.stream_name("stream103");
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
assert(!compose.validate());
|
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
2021-04-13 01:44:36 +02:00
|
|
|
$t_html({defaultMessage: "Guests are not allowed to post to this stream."}),
|
2020-10-08 20:30:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// reset compose_state.stream_name to 'social' again so that any tests occurring after this
|
2018-05-14 12:06:56 +02:00
|
|
|
// do not reproduce this error.
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.stream_name("social");
|
2020-10-08 20:30:11 +02:00
|
|
|
// Reset page_params
|
|
|
|
page_params.is_guest = false;
|
2018-05-14 12:06:56 +02:00
|
|
|
});
|
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("markdown_rtl", (override) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const textarea = $("#compose-textarea");
|
2018-07-11 14:07:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-16 23:29:01 +02:00
|
|
|
keyCode: 65, // A
|
2018-07-11 14:07:26 +02:00
|
|
|
};
|
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
override(rtl, "get_direction", (text) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(text, " foo");
|
|
|
|
return "rtl";
|
2021-02-28 00:37:11 +01:00
|
|
|
});
|
2018-07-11 14:07:26 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea.hasClass("rtl"), false);
|
2018-07-11 14:07:26 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
textarea.val("```quote foo");
|
2018-08-11 06:43:38 +02:00
|
|
|
compose.handle_keyup(event, $("#compose-textarea"));
|
2018-07-11 14:07:26 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea.hasClass("rtl"), true);
|
2018-07-11 14:07:26 +02:00
|
|
|
});
|
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("markdown_ltr", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const textarea = $("#compose-textarea");
|
2018-07-11 14:07:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-16 23:29:01 +02:00
|
|
|
keyCode: 65, // A
|
2018-07-11 14:07:26 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea.hasClass("rtl"), true);
|
|
|
|
textarea.val("```quote foo");
|
2018-08-11 06:43:38 +02:00
|
|
|
compose.handle_keyup(event, textarea);
|
2018-07-11 14:07:26 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textarea.hasClass("rtl"), false);
|
2018-07-11 14:07:26 +02:00
|
|
|
});
|
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("markdown_shortcuts", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let queryCommandEnabled = true;
|
|
|
|
const event = {
|
2017-12-06 14:46:23 +01:00
|
|
|
keyCode: 66,
|
|
|
|
target: {
|
2020-07-15 01:29:15 +02:00
|
|
|
id: "compose-textarea",
|
2017-12-06 14:46:23 +01:00
|
|
|
},
|
|
|
|
stopPropagation: noop,
|
|
|
|
preventDefault: noop,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let input_text = "";
|
|
|
|
let range_start = 0;
|
|
|
|
let range_length = 0;
|
|
|
|
let compose_value = $("#compose_textarea").val();
|
|
|
|
let selected_word = "";
|
2017-12-06 14:46:23 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
document.queryCommandEnabled = () => queryCommandEnabled;
|
|
|
|
document.execCommand = (cmd, bool, markdown) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const compose_textarea = $("#compose-textarea");
|
|
|
|
const value = compose_textarea.val();
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#compose-textarea").val(
|
2020-10-07 09:41:22 +02:00
|
|
|
value.slice(0, compose_textarea.range().start) +
|
2020-07-15 00:34:28 +02:00
|
|
|
markdown +
|
2020-10-07 09:41:22 +02:00
|
|
|
value.slice(compose_textarea.range().end),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2017-12-06 14:46:23 +01:00
|
|
|
};
|
|
|
|
|
2020-10-07 10:13:26 +02:00
|
|
|
$("#compose-textarea")[0] = {};
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#compose-textarea").range = () => ({
|
|
|
|
start: range_start,
|
|
|
|
end: range_start + range_length,
|
|
|
|
length: range_length,
|
|
|
|
range: noop,
|
|
|
|
text: $("#compose-textarea")
|
|
|
|
.val()
|
|
|
|
.slice(range_start, range_length + range_start),
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").caret = noop;
|
2017-12-06 14:46:23 +01:00
|
|
|
|
2018-02-17 17:21:45 +01:00
|
|
|
function test_i_typed(isCtrl, isCmd) {
|
|
|
|
// Test 'i' is typed correctly.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("i");
|
2018-02-17 17:21:45 +01:00
|
|
|
event.keyCode = undefined;
|
|
|
|
event.which = 73;
|
|
|
|
event.metaKey = isCmd;
|
|
|
|
event.ctrlKey = isCtrl;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("i", $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function all_markdown_test(isCtrl, isCmd) {
|
|
|
|
input_text = "Any text.";
|
|
|
|
$("#compose-textarea").val(input_text);
|
|
|
|
compose_value = $("#compose-textarea").val();
|
|
|
|
// Select "text" word in compose box.
|
|
|
|
selected_word = "text";
|
|
|
|
range_start = compose_value.search(selected_word);
|
|
|
|
range_length = selected_word.length;
|
|
|
|
|
|
|
|
// Test bold:
|
2020-08-11 02:09:14 +02:00
|
|
|
// Mac env = Cmd+b
|
|
|
|
// Windows/Linux = Ctrl+b
|
2018-02-17 17:21:45 +01:00
|
|
|
event.keyCode = 66;
|
|
|
|
event.ctrlKey = isCtrl;
|
|
|
|
event.metaKey = isCmd;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Any **text**.", $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
// Test if no text is selected.
|
|
|
|
range_start = 0;
|
|
|
|
// Change cursor to first position.
|
|
|
|
range_length = 0;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("****Any **text**.", $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
|
|
|
|
// Test italic:
|
2020-08-11 02:09:14 +02:00
|
|
|
// Mac = Cmd+I
|
|
|
|
// Windows/Linux = Ctrl+I
|
2018-02-17 17:21:45 +01:00
|
|
|
$("#compose-textarea").val(input_text);
|
|
|
|
range_start = compose_value.search(selected_word);
|
|
|
|
range_length = selected_word.length;
|
|
|
|
event.keyCode = 73;
|
|
|
|
event.shiftKey = false;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Any *text*.", $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
// Test if no text is selected.
|
|
|
|
range_length = 0;
|
|
|
|
// Change cursor to first position.
|
|
|
|
range_start = 0;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("**Any *text*.", $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
|
|
|
|
// Test link insertion:
|
2020-08-11 02:09:14 +02:00
|
|
|
// Mac = Cmd+Shift+L
|
|
|
|
// Windows/Linux = Ctrl+Shift+L
|
2018-02-17 17:21:45 +01:00
|
|
|
$("#compose-textarea").val(input_text);
|
|
|
|
range_start = compose_value.search(selected_word);
|
|
|
|
range_length = selected_word.length;
|
|
|
|
event.keyCode = 76;
|
|
|
|
event.which = undefined;
|
|
|
|
event.shiftKey = true;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("Any [text](url).", $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
// Test if exec command is not enabled in browser.
|
|
|
|
queryCommandEnabled = false;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2018-02-17 17:21:45 +01:00
|
|
|
}
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
// This function cross tests the Cmd/Ctrl + Markdown shortcuts in
|
2018-02-17 17:21:45 +01:00
|
|
|
// Mac and Linux/Windows environments. So in short, this tests
|
|
|
|
// that e.g. Cmd+B should be ignored on Linux/Windows and Ctrl+B
|
|
|
|
// should be ignored on Mac.
|
|
|
|
function os_specific_markdown_test(isCtrl, isCmd) {
|
|
|
|
input_text = "Any text.";
|
|
|
|
$("#compose-textarea").val(input_text);
|
|
|
|
compose_value = $("#compose-textarea").val();
|
|
|
|
selected_word = "text";
|
|
|
|
range_start = compose_value.search(selected_word);
|
|
|
|
range_length = selected_word.length;
|
|
|
|
event.metaKey = isCmd;
|
|
|
|
event.ctrlKey = isCtrl;
|
|
|
|
|
|
|
|
event.keyCode = 66;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(input_text, $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
|
|
|
|
event.keyCode = 73;
|
|
|
|
event.shiftKey = false;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(input_text, $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
|
|
|
|
event.keyCode = 76;
|
|
|
|
event.shiftKey = true;
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $("#compose-textarea"));
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(input_text, $("#compose-textarea").val());
|
2018-02-17 17:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// These keyboard shortcuts differ as to what key one should use
|
|
|
|
// on MacOS vs. other platforms: Cmd (Mac) vs. Ctrl (non-Mac).
|
|
|
|
|
|
|
|
// Default (Linux/Windows) userAgent tests:
|
|
|
|
test_i_typed(false, false);
|
2020-08-11 02:09:14 +02:00
|
|
|
// Check all the Ctrl + Markdown shortcuts work correctly
|
2018-02-17 17:21:45 +01:00
|
|
|
all_markdown_test(true, false);
|
2020-08-11 01:47:49 +02:00
|
|
|
// The Cmd + Markdown shortcuts should do nothing on Linux/Windows
|
2018-02-17 17:21:45 +01:00
|
|
|
os_specific_markdown_test(false, true);
|
|
|
|
|
2019-06-24 14:11:21 +02:00
|
|
|
// Setting following platform to test in mac env
|
|
|
|
_navigator.platform = "MacIntel";
|
2018-02-17 17:21:45 +01:00
|
|
|
|
|
|
|
// Mac userAgent tests:
|
|
|
|
test_i_typed(false, false);
|
2020-08-11 02:09:14 +02:00
|
|
|
// The Ctrl + Markdown shortcuts should do nothing on mac
|
2018-02-17 17:21:45 +01:00
|
|
|
os_specific_markdown_test(true, false);
|
2020-08-11 01:47:49 +02:00
|
|
|
// Check all the Cmd + Markdown shortcuts work correctly
|
2018-02-17 17:21:45 +01:00
|
|
|
all_markdown_test(false, true);
|
|
|
|
|
|
|
|
// Reset userAgent
|
2018-08-01 21:17:03 +02:00
|
|
|
_navigator.userAgent = "";
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-06 14:46:23 +01:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("send_message_success", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("foobarfoobar");
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-textarea").trigger("blur");
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").show();
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", true);
|
2017-06-29 06:54:10 +02:00
|
|
|
$("#sending-indicator").show();
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let reify_message_id_checked;
|
2021-02-23 14:37:26 +01:00
|
|
|
echo.reify_message_id = (local_id, message_id) => {
|
2020-02-12 09:32:25 +01:00
|
|
|
assert.equal(local_id, "1001");
|
2017-06-29 06:54:10 +02:00
|
|
|
assert.equal(message_id, 12);
|
|
|
|
reify_message_id_checked = true;
|
|
|
|
};
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
|
2020-02-12 09:32:25 +01:00
|
|
|
compose.send_message_success("1001", 12, false);
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-textarea").val(), "");
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").is_focused());
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$("#compose-send-status").visible());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2017-06-29 06:54:10 +02:00
|
|
|
assert(!$("#sending-indicator").visible());
|
|
|
|
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
assert(reify_message_id_checked);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-29 14:31:26 +02:00
|
|
|
|
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
|
|
|
test_ui("send_message", (override) => {
|
2017-07-06 21:57:25 +02:00
|
|
|
// This is the common setup stuff for all of the four tests.
|
2019-11-02 00:06:25 +01:00
|
|
|
let stub_state;
|
2017-07-06 21:57:25 +02:00
|
|
|
function initialize_state_stub_dict() {
|
|
|
|
stub_state = {};
|
2018-02-20 13:08:50 +01:00
|
|
|
stub_state.send_msg_called = 0;
|
2017-07-06 21:57:25 +02:00
|
|
|
stub_state.get_events_running_called = 0;
|
|
|
|
stub_state.reify_message_id_checked = 0;
|
|
|
|
return stub_state;
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
set_global("setTimeout", (func) => {
|
2017-07-06 21:57:25 +02:00
|
|
|
func();
|
|
|
|
});
|
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
|
|
|
|
|
|
|
override(server_events, "assert_get_events_running", () => {
|
|
|
|
stub_state.get_events_running_called += 1;
|
2020-12-01 00:53:02 +01:00
|
|
|
});
|
2017-07-06 21:57:25 +02:00
|
|
|
|
|
|
|
// Tests start here.
|
|
|
|
(function test_message_send_success_codepath() {
|
|
|
|
stub_state = initialize_state_stub_dict();
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.topic("");
|
|
|
|
compose_state.set_message_type("private");
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.user_id = new_user.user_id;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_state, "private_message_recipient", () => "alice@example.com");
|
2018-03-31 13:22:29 +02:00
|
|
|
|
2020-04-09 19:55:20 +02:00
|
|
|
const server_message_id = 127;
|
2021-03-26 14:39:40 +01:00
|
|
|
override(echo, "insert_message", (message) => {
|
2020-04-09 17:59:58 +02:00
|
|
|
assert.equal(message.timestamp, fake_now);
|
2021-03-26 14:39:40 +01:00
|
|
|
});
|
2020-04-09 19:55:20 +02:00
|
|
|
|
2020-04-09 17:59:58 +02:00
|
|
|
markdown.apply_markdown = () => {};
|
|
|
|
markdown.add_topic_links = () => {};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
echo.try_deliver_locally = (message_request) => {
|
2020-04-09 19:55:20 +02:00
|
|
|
const local_id_float = 123.04;
|
2020-04-09 17:59:58 +02:00
|
|
|
return echo.insert_local_message(message_request, local_id_float);
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
transmit.send_message = (payload, success) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const single_msg = {
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "private",
|
|
|
|
content: "[foobar](/user_uploads/123456)",
|
2021-04-03 19:07:13 +02:00
|
|
|
sender_id: new_user.user_id,
|
2018-05-07 03:30:13 +02:00
|
|
|
queue_id: undefined,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "",
|
|
|
|
topic: "",
|
2019-05-23 22:18:58 +02:00
|
|
|
to: `[${alice.user_id}]`,
|
2020-07-15 01:29:15 +02:00
|
|
|
reply_to: "alice@example.com",
|
|
|
|
private_message_recipient: "alice@example.com",
|
|
|
|
to_user_ids: "31",
|
|
|
|
local_id: "123.04",
|
2018-05-07 03:30:13 +02:00
|
|
|
locally_echoed: true,
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
2019-05-23 22:18:58 +02:00
|
|
|
|
2018-02-20 13:08:50 +01:00
|
|
|
assert.deepEqual(payload, single_msg);
|
2020-04-09 19:55:20 +02:00
|
|
|
payload.id = server_message_id;
|
2018-02-20 13:08:50 +01:00
|
|
|
success(payload);
|
|
|
|
stub_state.send_msg_called += 1;
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
echo.reify_message_id = (local_id, message_id) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof local_id, "string");
|
|
|
|
assert.equal(typeof message_id, "number");
|
2020-04-09 19:55:20 +02:00
|
|
|
assert.equal(message_id, server_message_id);
|
2017-07-06 21:57:25 +02:00
|
|
|
stub_state.reify_message_id_checked += 1;
|
|
|
|
};
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
|
2017-07-06 21:57:25 +02:00
|
|
|
// Setting message content with a host server link and we will assert
|
|
|
|
// later that this has been converted to a relative link.
|
2020-10-07 13:17:55 +02:00
|
|
|
$("#compose-textarea").val("[foobar](https://foo.com/user_uploads/123456)");
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-textarea").trigger("blur");
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").show();
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", true);
|
2017-07-06 21:57:25 +02:00
|
|
|
$("#sending-indicator").show();
|
|
|
|
|
|
|
|
compose.send_message();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2017-07-06 21:57:25 +02:00
|
|
|
get_events_running_called: 1,
|
|
|
|
reify_message_id_checked: 1,
|
2018-02-20 13:08:50 +01:00
|
|
|
send_msg_called: 1,
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(stub_state, state);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-textarea").val(), "");
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").is_focused());
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$("#compose-send-status").visible());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2017-07-06 21:57:25 +02:00
|
|
|
assert(!$("#sending-indicator").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-06 21:57:25 +02:00
|
|
|
|
|
|
|
// This is the additional setup which is common to both the tests below.
|
2021-02-23 14:37:26 +01:00
|
|
|
transmit.send_message = (payload, success, error) => {
|
2018-02-20 13:08:50 +01:00
|
|
|
stub_state.send_msg_called += 1;
|
2020-07-15 01:29:15 +02:00
|
|
|
error("Error sending message: Server says 408");
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let echo_error_msg_checked;
|
2018-02-20 13:08:50 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
echo.message_send_error = (local_id, error_response) => {
|
2020-04-09 17:43:30 +02:00
|
|
|
assert.equal(local_id, 123.04);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(error_response, "Error sending message: Server says 408");
|
2017-07-06 21:57:25 +02:00
|
|
|
echo_error_msg_checked = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests start here.
|
|
|
|
(function test_param_error_function_passed_from_send_message() {
|
|
|
|
stub_state = initialize_state_stub_dict();
|
|
|
|
|
|
|
|
compose.send_message();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2017-07-06 21:57:25 +02:00
|
|
|
get_events_running_called: 1,
|
|
|
|
reify_message_id_checked: 0,
|
2018-02-20 13:08:50 +01:00
|
|
|
send_msg_called: 1,
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(stub_state, state);
|
|
|
|
assert(echo_error_msg_checked);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-06 21:57:25 +02:00
|
|
|
|
|
|
|
(function test_error_codepath_local_id_undefined() {
|
|
|
|
stub_state = initialize_state_stub_dict();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("foobarfoobar");
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-textarea").trigger("blur");
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").show();
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", true);
|
2017-07-06 21:57:25 +02:00
|
|
|
$("#sending-indicator").show();
|
2020-07-21 09:44:41 +02:00
|
|
|
$("#compose-textarea").off("select");
|
2017-07-06 21:57:25 +02:00
|
|
|
echo_error_msg_checked = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
echo.try_deliver_locally = () => {};
|
2017-07-06 21:57:25 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
sent_messages.get_new_local_id = () => "loc-55";
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
|
2017-07-06 21:57:25 +02:00
|
|
|
compose.send_message();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const state = {
|
2017-07-06 21:57:25 +02:00
|
|
|
get_events_running_called: 1,
|
|
|
|
reify_message_id_checked: 0,
|
2018-02-20 13:08:50 +01:00
|
|
|
send_msg_called: 1,
|
2017-07-06 21:57:25 +02:00
|
|
|
};
|
|
|
|
assert.deepEqual(stub_state, state);
|
|
|
|
assert(!echo_error_msg_checked);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal($("#compose-error-msg").html(), "Error sending message: Server says 408");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-textarea").val(), "foobarfoobar");
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").is_focused());
|
2017-11-26 19:58:36 +01:00
|
|
|
assert($("#compose-send-status").visible());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2017-07-06 21:57:25 +02:00
|
|
|
assert(!$("#sending-indicator").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-06 21:57:25 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("enter_with_preview_open", (override) => {
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.user_id = new_user.user_id;
|
|
|
|
|
2017-07-07 19:31:32 +02:00
|
|
|
// Test sending a message with content.
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("stream");
|
|
|
|
$("#compose-textarea").val("message me");
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").hide();
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .undo_markdown_preview").show();
|
|
|
|
$("#compose .preview_message_area").show();
|
|
|
|
$("#compose .markdown_preview").hide();
|
2017-07-07 00:33:46 +02:00
|
|
|
page_params.enter_sends = true;
|
2019-11-02 00:06:25 +01:00
|
|
|
let send_message_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "send_message", () => {
|
2017-07-07 00:33:46 +02:00
|
|
|
send_message_called = true;
|
2021-02-28 00:51:57 +01:00
|
|
|
});
|
2017-07-07 00:33:46 +02:00
|
|
|
compose.enter_with_preview_open();
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").visible());
|
2021-04-20 19:50:01 +02:00
|
|
|
assert(!$("#compose .undo_markdown_preview").visible());
|
|
|
|
assert(!$("#compose .preview_message_area").visible());
|
|
|
|
assert($("#compose .markdown_preview").visible());
|
2017-07-07 00:33:46 +02:00
|
|
|
assert(send_message_called);
|
|
|
|
|
|
|
|
page_params.enter_sends = false;
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-textarea").trigger("blur");
|
2017-07-07 00:33:46 +02:00
|
|
|
compose.enter_with_preview_open();
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").is_focused());
|
2017-07-07 19:31:32 +02:00
|
|
|
|
|
|
|
// Test sending a message without content.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("");
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .preview_message_area").show();
|
2017-07-07 19:31:32 +02:00
|
|
|
$("#enter_sends").prop("checked", true);
|
|
|
|
page_params.enter_sends = true;
|
|
|
|
|
|
|
|
compose.enter_with_preview_open();
|
|
|
|
|
|
|
|
assert($("#enter_sends").prop("checked"));
|
2021-04-13 01:44:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "You have nothing to send!"}),
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-07 00:33:46 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("finish", (override) => {
|
2017-07-07 00:48:51 +02:00
|
|
|
(function test_when_compose_validation_fails() {
|
|
|
|
$("#compose_invite_users").show();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-send-button").trigger("focus");
|
2017-07-07 00:48:51 +02:00
|
|
|
$("#sending-indicator").hide();
|
2020-07-21 09:44:41 +02:00
|
|
|
$("#compose-textarea").off("select");
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("");
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = compose.finish();
|
2017-07-07 00:48:51 +02:00
|
|
|
assert.equal(res, false);
|
|
|
|
assert(!$("#compose_invite_users").visible());
|
|
|
|
assert(!$("#sending-indicator").visible());
|
|
|
|
assert(!$("#compose-send-button").is_focused());
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose-send-button").prop("disabled"), false);
|
2021-04-13 01:44:36 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#compose-error-msg").html(),
|
|
|
|
$t_html({defaultMessage: "You have nothing to send!"}),
|
|
|
|
);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-07 00:48:51 +02:00
|
|
|
|
|
|
|
(function test_when_compose_validation_succeed() {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").hide();
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .undo_markdown_preview").show();
|
|
|
|
$("#compose .preview_message_area").show();
|
|
|
|
$("#compose .markdown_preview").hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("foobarfoobar");
|
|
|
|
compose_state.set_message_type("private");
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_state, "private_message_recipient", () => "bob@example.com");
|
2018-03-31 13:22:29 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let compose_finished_event_checked = false;
|
2020-07-21 05:53:07 +02:00
|
|
|
$(document).on("compose_finished.zulip", () => {
|
2018-04-12 18:06:39 +02:00
|
|
|
compose_finished_event_checked = true;
|
2020-07-21 05:53:07 +02:00
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
let send_message_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "send_message", () => {
|
2017-07-07 00:48:51 +02:00
|
|
|
send_message_called = true;
|
2021-02-28 00:51:57 +01:00
|
|
|
});
|
2017-07-07 00:48:51 +02:00
|
|
|
assert(compose.finish());
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").visible());
|
2021-04-20 19:50:01 +02:00
|
|
|
assert(!$("#compose .undo_markdown_preview").visible());
|
|
|
|
assert(!$("#compose .preview_message_area").visible());
|
|
|
|
assert($("#compose .markdown_preview").visible());
|
2017-07-07 00:48:51 +02:00
|
|
|
assert(send_message_called);
|
|
|
|
assert(compose_finished_event_checked);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-07 00:48:51 +02:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("warn_if_private_stream_is_linked", () => {
|
2021-01-12 16:05:24 +01:00
|
|
|
const test_sub = {
|
2020-02-09 22:02:55 +01:00
|
|
|
name: compose_state.stream_name(),
|
2020-02-07 21:18:20 +01:00
|
|
|
stream_id: 99,
|
2021-01-12 16:05:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
stream_data.add_sub(test_sub);
|
2021-01-13 22:03:25 +01:00
|
|
|
peer_data.set_subscribers(test_sub.stream_id, [1, 2]);
|
2020-01-14 15:57:16 +01:00
|
|
|
|
|
|
|
let denmark = {
|
2021-01-12 16:05:24 +01:00
|
|
|
stream_id: 100,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2020-01-14 15:57:16 +01:00
|
|
|
};
|
2021-01-30 13:38:55 +01:00
|
|
|
stream_data.add_sub(denmark);
|
|
|
|
|
2021-01-13 22:03:25 +01:00
|
|
|
peer_data.set_subscribers(denmark.stream_id, [1, 2, 3]);
|
2020-01-14 15:57:16 +01:00
|
|
|
|
|
|
|
function test_noop_case(invite_only) {
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("stream");
|
2020-01-14 15:57:16 +01:00
|
|
|
denmark.invite_only = invite_only;
|
|
|
|
compose.warn_if_private_stream_is_linked(denmark);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_private_stream_alert").visible(), false);
|
2020-01-14 15:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
test_noop_case(false);
|
|
|
|
// invite_only=true and current compose stream subscribers are a subset
|
|
|
|
// of mentioned_stream subscribers.
|
|
|
|
test_noop_case(true);
|
|
|
|
|
|
|
|
$("#compose_private").hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("stream");
|
2020-01-14 15:57:16 +01:00
|
|
|
|
|
|
|
const checks = [
|
|
|
|
(function () {
|
|
|
|
let called;
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, context) => {
|
2020-01-14 15:57:16 +01:00
|
|
|
called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(template_name, "compose_private_stream_alert");
|
|
|
|
assert.equal(context.stream_name, "Denmark");
|
|
|
|
return "fake-compose_private_stream_alert-template";
|
2020-01-14 15:57:16 +01:00
|
|
|
});
|
2020-07-15 00:34:28 +02:00
|
|
|
return function () {
|
|
|
|
assert(called);
|
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})(),
|
2020-01-14 15:57:16 +01:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
let called;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#compose_private_stream_alert").append = (html) => {
|
2020-01-14 15:57:16 +01:00
|
|
|
called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(html, "fake-compose_private_stream_alert-template");
|
2020-01-14 15:57:16 +01:00
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
return function () {
|
|
|
|
assert(called);
|
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})(),
|
2020-01-14 15:57:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
denmark = {
|
|
|
|
invite_only: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Denmark",
|
2021-01-29 17:17:32 +01:00
|
|
|
stream_id: 22,
|
2020-01-14 15:57:16 +01:00
|
|
|
};
|
2021-01-29 17:17:32 +01:00
|
|
|
stream_data.add_sub(denmark);
|
2020-01-14 15:57:16 +01:00
|
|
|
|
|
|
|
compose.warn_if_private_stream_is_linked(denmark);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_private_stream_alert").visible(), true);
|
2020-01-14 15:57:16 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const f of checks) {
|
|
|
|
f();
|
|
|
|
}
|
2020-01-14 15:57:16 +01:00
|
|
|
});
|
|
|
|
|
2021-03-05 22:45:18 +01:00
|
|
|
test_ui("initialize", (override) => {
|
2017-07-07 15:14:00 +02:00
|
|
|
// In this test we mostly do the setup stuff in addition to testing the
|
|
|
|
// normal workflow of the function. All the tests for the on functions are
|
|
|
|
// done in subsequent tests directly below this test.
|
|
|
|
|
2021-03-05 22:45:18 +01:00
|
|
|
override(compose, "compute_show_video_chat_button", () => false);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let resize_watch_manual_resize_checked = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
resize.watch_manual_resize = (elem) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal("#compose-textarea", elem);
|
2017-07-07 15:14:00 +02:00
|
|
|
resize_watch_manual_resize_checked = true;
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let xmlhttprequest_checked = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("XMLHttpRequest", function () {
|
2017-07-07 15:14:00 +02:00
|
|
|
this.upload = true;
|
|
|
|
xmlhttprequest_checked = true;
|
|
|
|
});
|
2021-04-20 16:22:50 +02:00
|
|
|
$("#compose .compose_upload_file").addClass("notdisplayed");
|
2017-07-07 15:14:00 +02:00
|
|
|
|
2020-12-01 00:53:02 +01:00
|
|
|
set_global("document", "document-stub");
|
2017-07-07 15:14:00 +02:00
|
|
|
|
2020-05-08 06:57:19 +02:00
|
|
|
page_params.max_file_upload_size_mib = 512;
|
2017-07-07 15:14:00 +02:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
let setup_upload_called = false;
|
|
|
|
let uppy_cancel_all_called = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(upload, "setup_upload", (config) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
assert.equal(config.mode, "compose");
|
|
|
|
setup_upload_called = true;
|
|
|
|
return {
|
|
|
|
cancelAll: () => {
|
|
|
|
uppy_cancel_all_called = true;
|
|
|
|
},
|
|
|
|
};
|
2021-02-28 00:52:20 +01:00
|
|
|
});
|
2017-07-07 15:14:00 +02:00
|
|
|
|
2021-04-14 13:10:34 +02:00
|
|
|
stub_templates((template_name, context) => {
|
|
|
|
assert.equal(template_name, "compose");
|
|
|
|
assert.equal(context.embedded, false);
|
2021-04-20 02:34:08 +02:00
|
|
|
assert.equal(context.file_upload_enabled, true);
|
2021-04-15 16:18:36 +02:00
|
|
|
assert.equal(context.giphy_enabled, true);
|
2021-04-14 13:10:34 +02:00
|
|
|
return "fake-compose-template";
|
|
|
|
});
|
|
|
|
|
2017-07-07 15:14:00 +02:00
|
|
|
compose.initialize();
|
|
|
|
|
|
|
|
assert(resize_watch_manual_resize_checked);
|
|
|
|
assert(xmlhttprequest_checked);
|
2021-04-20 16:22:50 +02:00
|
|
|
assert(!$("#compose .compose_upload_file").hasClass("notdisplayed"));
|
2019-11-21 05:24:55 +01:00
|
|
|
assert(setup_upload_called);
|
2017-07-07 15:14:00 +02:00
|
|
|
|
|
|
|
function set_up_compose_start_mock(expected_opts) {
|
|
|
|
compose_actions_start_checked = false;
|
2021-02-28 18:48:44 +01:00
|
|
|
compose_actions_expected_opts = expected_opts;
|
2017-07-07 15:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(function test_page_params_narrow_path() {
|
|
|
|
page_params.narrow = true;
|
|
|
|
|
|
|
|
reset_jquery();
|
|
|
|
set_up_compose_start_mock({});
|
|
|
|
|
|
|
|
compose.initialize();
|
|
|
|
|
|
|
|
assert(compose_actions_start_checked);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-07 15:14:00 +02:00
|
|
|
|
|
|
|
(function test_page_params_narrow_topic() {
|
2020-07-15 01:29:15 +02:00
|
|
|
page_params.narrow_topic = "testing";
|
2017-07-07 15:14:00 +02:00
|
|
|
|
|
|
|
reset_jquery();
|
2020-07-15 01:29:15 +02:00
|
|
|
set_up_compose_start_mock({topic: "testing"});
|
2017-07-07 15:14:00 +02:00
|
|
|
|
|
|
|
compose.initialize();
|
|
|
|
|
|
|
|
assert(compose_actions_start_checked);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
(function test_abort_xhr() {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("#compose-send-button").prop("disabled", true);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
reset_jquery();
|
|
|
|
compose.initialize();
|
|
|
|
|
|
|
|
compose.abort_xhr();
|
|
|
|
|
|
|
|
assert.equal($("#compose-send-button").attr(), undefined);
|
|
|
|
assert(uppy_cancel_all_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-07 03:59:08 +02:00
|
|
|
|
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
|
|
|
test_ui("update_fade", (override) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
const selector =
|
|
|
|
"#stream_message_recipient_stream,#stream_message_recipient_topic,#private_message_recipient";
|
2020-07-15 01:29:15 +02:00
|
|
|
const keyup_handler_func = $(selector).get_on_handler("keyup");
|
2017-07-07 15:41:13 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let set_focused_recipient_checked = false;
|
|
|
|
let update_all_called = false;
|
2017-07-07 15:41:13 +02:00
|
|
|
|
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
|
|
|
override(compose_fade, "set_focused_recipient", (msg_type) => {
|
|
|
|
assert.equal(msg_type, "private");
|
|
|
|
set_focused_recipient_checked = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
override(compose_fade, "update_all", () => {
|
|
|
|
update_all_called = true;
|
2020-12-01 00:53:02 +01:00
|
|
|
});
|
2017-07-07 15:41:13 +02:00
|
|
|
|
|
|
|
compose_state.set_message_type(false);
|
|
|
|
keyup_handler_func();
|
|
|
|
assert(!set_focused_recipient_checked);
|
2018-04-22 16:25:46 +02:00
|
|
|
assert(!update_all_called);
|
2017-07-07 15:41:13 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("private");
|
2017-07-07 15:41:13 +02:00
|
|
|
keyup_handler_func();
|
|
|
|
assert(set_focused_recipient_checked);
|
2018-04-22 16:25:46 +02:00
|
|
|
assert(update_all_called);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-07 15:41:13 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("trigger_submit_compose_form", (override) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let prevent_default_checked = false;
|
|
|
|
let compose_finish_checked = false;
|
|
|
|
const e = {
|
2020-07-20 22:18:43 +02:00
|
|
|
preventDefault() {
|
2017-07-07 15:42:51 +02:00
|
|
|
prevent_default_checked = true;
|
|
|
|
},
|
|
|
|
};
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "finish", () => {
|
2017-07-07 15:42:51 +02:00
|
|
|
compose_finish_checked = true;
|
2021-02-28 00:51:57 +01:00
|
|
|
});
|
2017-07-07 15:42:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const submit_handler = $("#compose form").get_on_handler("submit");
|
2017-07-07 15:42:51 +02:00
|
|
|
|
|
|
|
submit_handler(e);
|
|
|
|
|
|
|
|
assert(prevent_default_checked);
|
|
|
|
assert(compose_finish_checked);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-07 15:42:51 +02:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("needs_subscribe_warning", () => {
|
2020-07-13 22:49:18 +02:00
|
|
|
const invalid_user_id = 999;
|
2018-05-18 16:23:35 +02:00
|
|
|
|
2020-07-13 22:49:18 +02:00
|
|
|
const test_bot = {
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Test Bot",
|
|
|
|
email: "test-bot@example.com",
|
2020-07-13 22:49:18 +02:00
|
|
|
user_id: 135,
|
|
|
|
is_bot: true,
|
2018-05-18 16:23:35 +02:00
|
|
|
};
|
2020-07-13 22:49:18 +02:00
|
|
|
people.add_active_user(test_bot);
|
2018-05-18 16:23:35 +02:00
|
|
|
|
2020-07-13 22:49:18 +02:00
|
|
|
const sub = {
|
|
|
|
stream_id: 110,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "stream",
|
2018-05-18 16:23:35 +02:00
|
|
|
};
|
|
|
|
|
2020-07-13 22:49:18 +02:00
|
|
|
stream_data.add_sub(sub);
|
2021-01-13 22:03:25 +01:00
|
|
|
peer_data.set_subscribers(sub.stream_id, [bob.user_id, me.user_id]);
|
2020-07-13 22:49:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Unknown user_id in get_by_user_id: 999");
|
2020-07-13 22:49:18 +02:00
|
|
|
// Test with an invalid user id.
|
|
|
|
assert.equal(compose.needs_subscribe_warning(invalid_user_id, sub.stream_id), false);
|
|
|
|
|
|
|
|
// Test with bot user.
|
|
|
|
assert.equal(compose.needs_subscribe_warning(test_bot.user_id, sub.stream_id), false);
|
|
|
|
|
|
|
|
// Test when user is subscribed to the stream.
|
|
|
|
assert.equal(compose.needs_subscribe_warning(bob.user_id, sub.stream_id), false);
|
|
|
|
|
2021-01-12 21:38:01 +01:00
|
|
|
peer_data.remove_subscriber(sub.stream_id, bob.user_id);
|
2020-07-13 22:49:18 +02:00
|
|
|
// Test when the user is not subscribed.
|
|
|
|
assert.equal(compose.needs_subscribe_warning(bob.user_id, sub.stream_id), true);
|
2018-05-18 16:23:35 +02:00
|
|
|
});
|
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("warn_if_mentioning_unsubscribed_user", (override) => {
|
2020-07-16 23:29:01 +02:00
|
|
|
let mentioned = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "foo@bar.com",
|
2020-01-14 18:26:48 +01:00
|
|
|
};
|
2017-07-08 00:06:38 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose_invite_users .compose_invite_user").length = 0;
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-01-14 18:39:10 +01:00
|
|
|
function test_noop_case(is_private, is_zephyr_mirror, is_broadcast) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const msg_type = is_private ? "private" : "stream";
|
2020-01-14 18:26:48 +01:00
|
|
|
compose_state.set_message_type(msg_type);
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = is_zephyr_mirror;
|
2020-01-14 18:39:10 +01:00
|
|
|
mentioned.is_broadcast = is_broadcast;
|
2020-01-14 18:26:48 +01:00
|
|
|
compose.warn_if_mentioning_unsubscribed_user(mentioned);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_invite_users").visible(), false);
|
2020-01-14 18:26:48 +01:00
|
|
|
}
|
2017-07-08 00:06:38 +02:00
|
|
|
|
2020-01-14 18:39:10 +01:00
|
|
|
test_noop_case(true, false, false);
|
|
|
|
test_noop_case(false, true, false);
|
|
|
|
test_noop_case(false, false, true);
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-01-14 18:26:48 +01:00
|
|
|
$("#compose_invite_users").hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.set_message_type("stream");
|
2020-01-14 18:26:48 +01:00
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-06-22 21:36:21 +02:00
|
|
|
// Test with empty stream name in compose box. It should return noop.
|
|
|
|
assert.equal(compose_state.stream_name(), "");
|
|
|
|
compose.warn_if_mentioning_unsubscribed_user(mentioned);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_invite_users").visible(), false);
|
2020-06-22 21:36:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_state.stream_name("random");
|
2020-06-22 21:36:21 +02:00
|
|
|
const sub = {
|
|
|
|
stream_id: 111,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "random",
|
2020-06-22 21:36:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Test with invalid stream in compose box. It should return noop.
|
|
|
|
compose.warn_if_mentioning_unsubscribed_user(mentioned);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_invite_users").visible(), false);
|
2020-06-22 21:36:21 +02:00
|
|
|
|
|
|
|
// Test mentioning a user that should gets a warning.
|
|
|
|
|
2020-01-14 18:26:48 +01:00
|
|
|
const checks = [
|
|
|
|
(function () {
|
|
|
|
let called;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "needs_subscribe_warning", (user_id, stream_id) => {
|
2020-01-14 18:26:48 +01:00
|
|
|
called = true;
|
2020-06-05 16:21:22 +02:00
|
|
|
assert.equal(user_id, 34);
|
2020-07-08 22:28:35 +02:00
|
|
|
assert.equal(stream_id, 111);
|
2020-01-14 18:26:48 +01:00
|
|
|
return true;
|
2021-02-28 00:51:57 +01:00
|
|
|
});
|
2020-07-15 00:34:28 +02:00
|
|
|
return function () {
|
|
|
|
assert(called);
|
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})(),
|
2020-01-14 18:26:48 +01:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
let called;
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((template_name, context) => {
|
2020-01-14 18:26:48 +01:00
|
|
|
called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(template_name, "compose_invite_users");
|
2020-06-04 19:41:45 +02:00
|
|
|
assert.equal(context.user_id, 34);
|
2020-06-22 21:36:21 +02:00
|
|
|
assert.equal(context.stream_id, 111);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(context.name, "Foo Barson");
|
|
|
|
return "fake-compose-invite-user-template";
|
2020-01-14 18:26:48 +01:00
|
|
|
});
|
2020-07-15 00:34:28 +02:00
|
|
|
return function () {
|
|
|
|
assert(called);
|
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})(),
|
2020-01-14 18:26:48 +01:00
|
|
|
|
|
|
|
(function () {
|
|
|
|
let called;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#compose_invite_users").append = (html) => {
|
2020-01-14 18:26:48 +01:00
|
|
|
called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(html, "fake-compose-invite-user-template");
|
2020-01-14 18:26:48 +01:00
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
return function () {
|
|
|
|
assert(called);
|
|
|
|
};
|
2020-07-16 22:35:58 +02:00
|
|
|
})(),
|
2020-01-14 18:26:48 +01:00
|
|
|
];
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-01-14 18:26:48 +01:00
|
|
|
mentioned = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "foo@bar.com",
|
2020-06-04 19:41:45 +02:00
|
|
|
user_id: 34,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Foo Barson",
|
2020-01-14 18:26:48 +01:00
|
|
|
};
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-06-22 21:36:21 +02:00
|
|
|
stream_data.add_sub(sub);
|
2020-01-14 18:26:48 +01:00
|
|
|
compose.warn_if_mentioning_unsubscribed_user(mentioned);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_invite_users").visible(), true);
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
for (const f of checks) {
|
|
|
|
f();
|
|
|
|
}
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-01-14 18:26:48 +01:00
|
|
|
// Simulate that the row was added to the DOM.
|
2020-07-15 01:29:15 +02:00
|
|
|
const warning_row = $("<warning row>");
|
2017-07-09 14:13:42 +02:00
|
|
|
|
2020-01-14 18:26:48 +01:00
|
|
|
let looked_for_existing;
|
2021-02-23 14:37:26 +01:00
|
|
|
warning_row.data = (field) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (field === "user-id") {
|
2020-06-22 21:36:21 +02:00
|
|
|
looked_for_existing = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
return "34";
|
2020-06-22 21:36:21 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (field === "stream-id") {
|
|
|
|
return "111";
|
2020-06-22 21:36:21 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
throw new Error(`Unknown field ${field}`);
|
2020-01-14 18:26:48 +01:00
|
|
|
};
|
2017-07-08 00:09:13 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const previous_users = $("#compose_invite_users .compose_invite_user");
|
2020-01-14 18:26:48 +01:00
|
|
|
previous_users.length = 1;
|
|
|
|
previous_users[0] = warning_row;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose_invite_users").hide();
|
2020-01-14 18:26:48 +01:00
|
|
|
|
|
|
|
// Now try to mention the same person again. The template should
|
|
|
|
// not render.
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(noop);
|
2020-01-14 18:26:48 +01:00
|
|
|
compose.warn_if_mentioning_unsubscribed_user(mentioned);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#compose_invite_users").visible(), true);
|
2020-01-14 18:26:48 +01:00
|
|
|
assert(looked_for_existing);
|
|
|
|
});
|
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("on_events", (override) => {
|
2017-07-08 23:16:19 +02:00
|
|
|
function setup_parents_and_mock_remove(container_sel, target_sel, parent) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const container = $.create("fake " + container_sel);
|
2019-11-02 00:06:25 +01:00
|
|
|
let container_removed = false;
|
2017-07-08 00:09:13 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
container.remove = () => {
|
2017-07-08 00:09:13 +02:00
|
|
|
container_removed = true;
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const target = $.create("fake click target (" + target_sel + ")");
|
2017-07-08 23:16:19 +02:00
|
|
|
|
|
|
|
target.set_parents_result(parent, container);
|
2017-07-08 00:09:13 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2017-07-08 23:16:19 +02:00
|
|
|
preventDefault: noop,
|
2020-07-20 22:18:43 +02:00
|
|
|
target,
|
2017-07-08 23:16:19 +02:00
|
|
|
};
|
2019-02-02 15:44:51 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = {
|
2020-07-20 22:18:43 +02:00
|
|
|
event,
|
|
|
|
container,
|
|
|
|
target,
|
2019-02-02 15:44:51 +01:00
|
|
|
container_was_removed: () => container_removed,
|
|
|
|
};
|
|
|
|
|
|
|
|
return helper;
|
2017-07-08 23:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(function test_compose_all_everyone_confirm_clicked() {
|
2020-07-15 00:34:28 +02:00
|
|
|
const handler = $("#compose-all-everyone").get_on_handler(
|
|
|
|
"click",
|
|
|
|
".compose-all-everyone-confirm",
|
|
|
|
);
|
2017-07-08 23:16:19 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = setup_parents_and_mock_remove(
|
2020-07-15 01:29:15 +02:00
|
|
|
"compose-all-everyone",
|
|
|
|
"compose-all-everyone",
|
|
|
|
".compose-all-everyone",
|
2019-02-02 15:44:51 +01:00
|
|
|
);
|
2017-07-08 00:09:13 +02:00
|
|
|
|
|
|
|
$("#compose-all-everyone").show();
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").show();
|
2017-07-08 00:09:13 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let compose_finish_checked = false;
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose, "finish", () => {
|
2017-07-08 00:09:13 +02:00
|
|
|
compose_finish_checked = true;
|
2021-02-28 00:51:57 +01:00
|
|
|
});
|
2017-07-08 00:09:13 +02:00
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
handler(helper.event);
|
2017-07-08 00:09:13 +02:00
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
assert(helper.container_was_removed());
|
2017-07-08 00:09:13 +02:00
|
|
|
assert(compose_finish_checked);
|
|
|
|
assert(!$("#compose-all-everyone").visible());
|
2017-11-26 19:58:36 +01:00
|
|
|
assert(!$("#compose-send-status").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-08 00:11:52 +02:00
|
|
|
|
|
|
|
(function test_compose_invite_users_clicked() {
|
2020-07-15 00:34:28 +02:00
|
|
|
const handler = $("#compose_invite_users").get_on_handler("click", ".compose_invite_link");
|
2019-11-02 00:06:25 +01:00
|
|
|
const subscription = {
|
2017-07-08 00:11:52 +02:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "test",
|
2017-07-08 00:11:52 +02:00
|
|
|
subscribed: true,
|
|
|
|
};
|
2020-06-03 21:38:43 +02:00
|
|
|
const mentioned = {
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Foo Barson",
|
|
|
|
email: "foo@bar.com",
|
2020-06-03 21:38:43 +02:00
|
|
|
user_id: 34,
|
|
|
|
};
|
|
|
|
people.add_active_user(mentioned);
|
2019-11-02 00:06:25 +01:00
|
|
|
let invite_user_to_stream_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
stream_edit.invite_user_to_stream = (user_ids, sub, success) => {
|
2017-07-08 00:11:52 +02:00
|
|
|
invite_user_to_stream_called = true;
|
2020-06-03 21:38:43 +02:00
|
|
|
assert.deepEqual(user_ids, [mentioned.user_id]);
|
2017-07-08 00:11:52 +02:00
|
|
|
assert.equal(sub, subscription);
|
2020-07-16 23:29:01 +02:00
|
|
|
success(); // This will check success callback path.
|
2017-07-08 00:11:52 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = setup_parents_and_mock_remove(
|
2020-07-15 01:29:15 +02:00
|
|
|
"compose_invite_users",
|
|
|
|
"compose_invite_link",
|
|
|
|
".compose_invite_user",
|
2019-02-02 15:44:51 +01:00
|
|
|
);
|
2017-07-08 00:11:52 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
helper.container.data = (field) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (field === "user-id") {
|
|
|
|
return "34";
|
2020-06-22 21:36:21 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
if (field === "stream-id") {
|
|
|
|
return "102";
|
2020-06-22 21:36:21 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
throw new Error(`Unknown field ${field}`);
|
2017-07-08 00:11:52 +02:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
helper.target.prop("disabled", false);
|
2017-07-08 00:11:52 +02:00
|
|
|
|
|
|
|
// !sub will result in true here and we check the success code path.
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(subscription);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#stream_message_recipient_stream").val("test");
|
2019-11-02 00:06:25 +01:00
|
|
|
let all_invite_children_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#compose_invite_users").children = () => {
|
2017-07-08 00:11:52 +02:00
|
|
|
all_invite_children_called = true;
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
$("#compose_invite_users").show();
|
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
handler(helper.event);
|
2017-07-08 00:11:52 +02:00
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
assert(helper.container_was_removed());
|
2017-07-08 00:11:52 +02:00
|
|
|
assert(!$("#compose_invite_users").visible());
|
|
|
|
assert(invite_user_to_stream_called);
|
|
|
|
assert(all_invite_children_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-08 00:13:14 +02:00
|
|
|
|
|
|
|
(function test_compose_invite_close_clicked() {
|
2020-07-15 00:34:28 +02:00
|
|
|
const handler = $("#compose_invite_users").get_on_handler("click", ".compose_invite_close");
|
2017-07-08 00:13:14 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = setup_parents_and_mock_remove(
|
2020-07-15 01:29:15 +02:00
|
|
|
"compose_invite_users_close",
|
|
|
|
"compose_invite_close",
|
|
|
|
".compose_invite_user",
|
2019-02-02 15:44:51 +01:00
|
|
|
);
|
2017-07-08 00:13:14 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let all_invite_children_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#compose_invite_users").children = () => {
|
2017-07-08 00:13:14 +02:00
|
|
|
all_invite_children_called = true;
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
$("#compose_invite_users").show();
|
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
handler(helper.event);
|
2017-07-08 00:13:14 +02:00
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
assert(helper.container_was_removed());
|
2017-07-08 00:13:14 +02:00
|
|
|
assert(all_invite_children_called);
|
|
|
|
assert(!$("#compose_invite_users").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-08 00:22:11 +02:00
|
|
|
|
2018-04-07 05:01:53 +02:00
|
|
|
(function test_compose_not_subscribed_clicked() {
|
2020-07-15 00:34:28 +02:00
|
|
|
const handler = $("#compose-send-status").get_on_handler("click", ".sub_unsub_button");
|
2019-11-02 00:06:25 +01:00
|
|
|
const subscription = {
|
2018-04-07 05:01:53 +02:00
|
|
|
stream_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "test",
|
2018-04-07 05:01:53 +02:00
|
|
|
subscribed: false,
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let compose_not_subscribed_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
subs.sub_or_unsub = () => {
|
2018-04-07 05:01:53 +02:00
|
|
|
compose_not_subscribed_called = true;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = setup_parents_and_mock_remove(
|
2020-07-15 01:29:15 +02:00
|
|
|
"compose-send-status",
|
|
|
|
"sub_unsub_button",
|
|
|
|
".compose_not_subscribed",
|
2019-02-02 15:44:51 +01:00
|
|
|
);
|
2018-04-07 05:01:53 +02:00
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
handler(helper.event);
|
2018-04-07 05:01:53 +02:00
|
|
|
|
|
|
|
assert(compose_not_subscribed_called);
|
|
|
|
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(subscription);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#stream_message_recipient_stream").val("test");
|
2018-04-07 05:01:53 +02:00
|
|
|
$("#compose-send-status").show();
|
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
handler(helper.event);
|
2018-04-07 05:01:53 +02:00
|
|
|
|
|
|
|
assert(!$("#compose-send-status").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-04-07 05:01:53 +02:00
|
|
|
|
|
|
|
(function test_compose_not_subscribed_close_clicked() {
|
2020-07-15 00:34:28 +02:00
|
|
|
const handler = $("#compose-send-status").get_on_handler(
|
|
|
|
"click",
|
|
|
|
"#compose_not_subscribed_close",
|
|
|
|
);
|
2018-04-07 05:01:53 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = setup_parents_and_mock_remove(
|
2020-07-15 01:29:15 +02:00
|
|
|
"compose_user_not_subscribed_close",
|
|
|
|
"compose_not_subscribed_close",
|
|
|
|
".compose_not_subscribed",
|
2019-02-02 15:44:51 +01:00
|
|
|
);
|
2018-04-07 05:01:53 +02:00
|
|
|
|
|
|
|
$("#compose-send-status").show();
|
|
|
|
|
2019-02-02 15:44:51 +01:00
|
|
|
handler(helper.event);
|
2018-04-07 05:01:53 +02:00
|
|
|
|
|
|
|
assert(!$("#compose-send-status").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-04-07 05:01:53 +02:00
|
|
|
|
2017-07-08 00:22:11 +02:00
|
|
|
(function test_attach_files_compose_clicked() {
|
2021-04-20 16:22:50 +02:00
|
|
|
const handler = $("#compose").get_on_handler("click", ".compose_upload_file");
|
2021-04-20 16:14:55 +02:00
|
|
|
$("#compose .file_input").clone = (param) => {
|
2017-07-08 00:22:11 +02:00
|
|
|
assert(param);
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
let compose_file_input_clicked = false;
|
2021-04-20 16:14:55 +02:00
|
|
|
$("#compose .file_input").on("click", () => {
|
2017-07-08 00:22:11 +02:00
|
|
|
compose_file_input_clicked = true;
|
2020-07-21 05:53:07 +02:00
|
|
|
});
|
2019-02-02 15:48:30 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2019-02-02 15:48:30 +01:00
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
2017-07-08 00:22:11 +02:00
|
|
|
handler(event);
|
|
|
|
assert(compose_file_input_clicked);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-08 01:21:20 +02:00
|
|
|
|
|
|
|
(function test_markdown_preview_compose_clicked() {
|
|
|
|
// Tests setup
|
|
|
|
function setup_visibilities() {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").show();
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .markdown_preview").show();
|
|
|
|
$("#compose .undo_markdown_preview").hide();
|
|
|
|
$("#compose .preview_message_area").hide();
|
2017-07-08 01:21:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function assert_visibilities() {
|
2017-11-26 20:37:44 +01:00
|
|
|
assert(!$("#compose-textarea").visible());
|
2021-04-20 19:50:01 +02:00
|
|
|
assert(!$("#compose .markdown_preview").visible());
|
|
|
|
assert($("#compose .undo_markdown_preview").visible());
|
|
|
|
assert($("#compose .preview_message_area").visible());
|
2017-07-08 01:21:20 +02:00
|
|
|
}
|
|
|
|
|
2017-07-29 02:51:33 +02:00
|
|
|
function setup_mock_markdown_contains_backend_only_syntax(msg_content, return_val) {
|
2021-02-23 14:37:26 +01:00
|
|
|
markdown.contains_backend_only_syntax = (msg) => {
|
2017-07-08 01:21:20 +02:00
|
|
|
assert.equal(msg, msg_content);
|
|
|
|
return return_val;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:29:44 +01:00
|
|
|
function setup_mock_markdown_is_status_message(msg_content, return_val) {
|
2021-02-23 14:37:26 +01:00
|
|
|
markdown.is_status_message = (content) => {
|
2018-01-21 19:27:36 +01:00
|
|
|
assert.equal(content, msg_content);
|
|
|
|
return return_val;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-08 01:21:20 +02:00
|
|
|
function test_post_success(success_callback) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const resp = {
|
2020-07-15 01:29:15 +02:00
|
|
|
rendered: "Server: foobarfoobar",
|
2017-07-08 01:21:20 +02:00
|
|
|
};
|
|
|
|
success_callback(resp);
|
2021-04-20 19:50:01 +02:00
|
|
|
assert.equal($("#compose .preview_content").html(), "Server: foobarfoobar");
|
2017-07-08 01:21:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_post_error(error_callback) {
|
|
|
|
error_callback();
|
2021-04-14 00:59:25 +02:00
|
|
|
assert.equal(
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .preview_content").html(),
|
2021-04-14 00:59:25 +02:00
|
|
|
"translated HTML: Failed to generate preview",
|
|
|
|
);
|
2017-07-08 01:21:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function mock_channel_post(msg) {
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (payload) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(payload.url, "/json/messages/render");
|
2017-07-08 01:21:20 +02:00
|
|
|
assert(payload.idempotent);
|
|
|
|
assert(payload.data);
|
|
|
|
assert.deepEqual(payload.data.content, msg);
|
|
|
|
|
|
|
|
function test(func, param) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let destroy_indicator_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
loading.destroy_indicator = (spinner) => {
|
2021-04-20 19:50:01 +02:00
|
|
|
assert.equal(spinner, $("#compose .markdown_preview_spinner"));
|
2017-07-08 01:21:20 +02:00
|
|
|
destroy_indicator_called = true;
|
|
|
|
};
|
2017-07-29 02:51:33 +02:00
|
|
|
setup_mock_markdown_contains_backend_only_syntax(msg, true);
|
2017-07-08 01:21:20 +02:00
|
|
|
|
|
|
|
func(param);
|
|
|
|
|
|
|
|
assert(destroy_indicator_called);
|
|
|
|
}
|
|
|
|
|
|
|
|
test(test_post_error, payload.error);
|
|
|
|
test(test_post_success, payload.success);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:50:01 +02:00
|
|
|
const handler = $("#compose").get_on_handler("click", ".markdown_preview");
|
2017-07-08 01:21:20 +02:00
|
|
|
|
|
|
|
// Tests start here
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("");
|
2017-07-08 01:21:20 +02:00
|
|
|
setup_visibilities();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2019-02-02 15:48:30 +01:00
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
2017-07-08 01:21:20 +02:00
|
|
|
handler(event);
|
|
|
|
|
2021-04-20 19:50:01 +02:00
|
|
|
assert.equal($("#compose .preview_content").html(), "translated HTML: Nothing to preview");
|
2017-07-08 01:21:20 +02:00
|
|
|
assert_visibilities();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let make_indicator_called = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("```foobarfoobar```");
|
2017-07-08 01:21:20 +02:00
|
|
|
setup_visibilities();
|
2020-07-15 01:29:15 +02:00
|
|
|
setup_mock_markdown_contains_backend_only_syntax("```foobarfoobar```", true);
|
|
|
|
setup_mock_markdown_is_status_message("```foobarfoobar```", false);
|
2021-02-23 14:37:26 +01:00
|
|
|
loading.make_indicator = (spinner) => {
|
2021-04-20 19:50:01 +02:00
|
|
|
assert.equal(spinner.selector, "#compose .markdown_preview_spinner");
|
2017-07-08 01:21:20 +02:00
|
|
|
make_indicator_called = true;
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
mock_channel_post("```foobarfoobar```");
|
2017-07-08 01:21:20 +02:00
|
|
|
|
|
|
|
handler(event);
|
|
|
|
|
|
|
|
assert(make_indicator_called);
|
|
|
|
assert_visibilities();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let apply_markdown_called = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val("foobarfoobar");
|
2017-07-08 01:21:20 +02:00
|
|
|
setup_visibilities();
|
2020-07-15 01:29:15 +02:00
|
|
|
setup_mock_markdown_contains_backend_only_syntax("foobarfoobar", false);
|
|
|
|
setup_mock_markdown_is_status_message("foobarfoobar", false);
|
|
|
|
mock_channel_post("foobarfoobar");
|
2021-02-23 14:37:26 +01:00
|
|
|
markdown.apply_markdown = (msg) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(msg.raw_content, "foobarfoobar");
|
2017-07-08 01:21:20 +02:00
|
|
|
apply_markdown_called = true;
|
|
|
|
return msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
handler(event);
|
|
|
|
|
|
|
|
assert(apply_markdown_called);
|
|
|
|
assert_visibilities();
|
2021-04-20 19:50:01 +02:00
|
|
|
assert.equal($("#compose .preview_content").html(), "Server: foobarfoobar");
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2017-07-08 01:27:11 +02:00
|
|
|
|
|
|
|
(function test_undo_markdown_preview_clicked() {
|
2021-04-20 19:50:01 +02:00
|
|
|
const handler = $("#compose").get_on_handler("click", ".undo_markdown_preview");
|
2017-07-08 01:27:11 +02:00
|
|
|
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").hide();
|
2021-04-20 19:50:01 +02:00
|
|
|
$("#compose .undo_markdown_preview").show();
|
|
|
|
$("#compose .preview_message_area").show();
|
|
|
|
$("#compose .markdown_preview").hide();
|
2017-07-08 01:27:11 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2019-02-02 15:48:30 +01:00
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
2017-07-08 01:27:11 +02:00
|
|
|
handler(event);
|
|
|
|
|
2017-11-26 20:37:44 +01:00
|
|
|
assert($("#compose-textarea").visible());
|
2021-04-20 19:50:01 +02:00
|
|
|
assert(!$("#compose .undo_markdown_preview").visible());
|
|
|
|
assert(!$("#compose .preview_message_area").visible());
|
|
|
|
assert($("#compose .markdown_preview").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-08 00:06:38 +02:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
test_ui("create_message_object", (override) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = {
|
2017-02-24 16:18:56 +01:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "social",
|
2017-02-24 16:18:56 +01:00
|
|
|
subscribed: true,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(sub);
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2021-02-21 15:04:46 +01:00
|
|
|
$("#stream_message_recipient_stream").val("social");
|
|
|
|
$("#stream_message_recipient_topic").val("lunch");
|
|
|
|
$("#compose-textarea").val("burrito");
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_state, "get_message_type", () => "stream");
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = compose.create_message_object();
|
2020-02-07 21:18:20 +01:00
|
|
|
assert.equal(message.to, sub.stream_id);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(message.topic, "lunch");
|
|
|
|
assert.equal(message.content, "burrito");
|
2020-02-07 21:18:20 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Trying to send message with bad stream name: BOGUS STREAM");
|
2020-02-07 21:18:20 +01:00
|
|
|
|
2021-02-21 15:04:46 +01:00
|
|
|
$("#stream_message_recipient_stream").val("BOGUS STREAM");
|
2020-02-07 21:18:20 +01:00
|
|
|
message = compose.create_message_object();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(message.to, "BOGUS STREAM");
|
|
|
|
assert.equal(message.topic, "lunch");
|
|
|
|
assert.equal(message.content, "burrito");
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2021-03-07 17:01:11 +01:00
|
|
|
override(compose_state, "get_message_type", () => "private");
|
2021-02-28 00:50:52 +01:00
|
|
|
compose_state.__Rewire__(
|
|
|
|
"private_message_recipient",
|
|
|
|
() => "alice@example.com, bob@example.com",
|
|
|
|
);
|
2018-03-31 13:22:29 +02:00
|
|
|
|
2017-03-29 08:54:26 +02:00
|
|
|
message = compose.create_message_object();
|
2019-05-23 22:18:58 +02:00
|
|
|
assert.deepEqual(message.to, [alice.user_id, bob.user_id]);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(message.to_user_ids, "31,32");
|
|
|
|
assert.equal(message.content, "burrito");
|
2017-02-24 16:18:56 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
const {email_list_to_user_ids_string} = people;
|
2019-05-23 22:18:58 +02:00
|
|
|
people.email_list_to_user_ids_string = () => undefined;
|
|
|
|
message = compose.create_message_object();
|
|
|
|
assert.deepEqual(message.to, [alice.email, bob.email]);
|
|
|
|
people.email_list_to_user_ids_string = email_list_to_user_ids_string;
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-07-23 02:41:55 +02:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("narrow_button_titles", () => {
|
2020-07-02 01:41:40 +02:00
|
|
|
util.is_mobile = () => false;
|
2018-08-13 23:17:45 +02:00
|
|
|
|
2021-05-11 22:04:35 +02:00
|
|
|
compose_closed_ui.update_buttons_for_private();
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#left_bar_compose_stream_button_big").text(),
|
|
|
|
$t({defaultMessage: "New stream message"}),
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
$("#left_bar_compose_private_button_big").text(),
|
|
|
|
$t({defaultMessage: "New private message"}),
|
|
|
|
);
|
2018-08-13 23:17:45 +02:00
|
|
|
|
2021-05-11 22:04:35 +02:00
|
|
|
compose_closed_ui.update_buttons_for_stream();
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
$("#left_bar_compose_stream_button_big").text(),
|
|
|
|
$t({defaultMessage: "New topic"}),
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
$("#left_bar_compose_private_button_big").text(),
|
|
|
|
$t({defaultMessage: "New private message"}),
|
|
|
|
);
|
2018-08-13 23:17:45 +02:00
|
|
|
});
|
2020-04-08 00:23:15 +02:00
|
|
|
|
2021-02-05 21:20:14 +01:00
|
|
|
MockDate.reset();
|