2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-09-04 23:49:49 +02:00
|
|
|
const autosize = require("autosize");
|
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
const {$t} = require("../zjsunit/i18n");
|
2021-03-11 05:43:45 +01:00
|
|
|
const {mock_cjs, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {
|
2020-07-20 22:18:43 +02:00
|
|
|
execCommand() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return false;
|
|
|
|
},
|
2017-12-08 16:17:20 +01:00
|
|
|
});
|
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const compose_ui = zrequire("compose_ui");
|
|
|
|
const people = zrequire("people");
|
|
|
|
const user_status = zrequire("user_status");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@zulip.com",
|
2019-07-23 20:13:43 +02:00
|
|
|
user_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2019-07-23 20:13:43 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bob@zulip.com",
|
2019-07-23 20:13:43 +02:00
|
|
|
user_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bob",
|
2019-07-23 20:13:43 +02:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(bob);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
function make_textbox(s) {
|
|
|
|
// Simulate a jQuery textbox for testing purposes.
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget = {};
|
2017-11-09 17:32:28 +01:00
|
|
|
|
|
|
|
widget.s = s;
|
|
|
|
widget.focused = false;
|
|
|
|
|
2021-02-23 20:32:35 +01:00
|
|
|
widget.caret = function (arg) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if (typeof arg === "number") {
|
2017-11-09 17:32:28 +01:00
|
|
|
widget.pos = arg;
|
2020-09-24 07:50:36 +02:00
|
|
|
return this;
|
2017-11-09 17:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg) {
|
|
|
|
widget.insert_pos = widget.pos;
|
|
|
|
widget.insert_text = arg;
|
2019-11-02 00:06:25 +01:00
|
|
|
const before = widget.s.slice(0, widget.pos);
|
|
|
|
const after = widget.s.slice(widget.pos);
|
2017-11-09 17:32:28 +01:00
|
|
|
widget.s = before + arg + after;
|
|
|
|
widget.pos += arg.length;
|
2020-09-24 07:50:36 +02:00
|
|
|
return this;
|
2017-11-09 17:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return widget.pos;
|
|
|
|
};
|
|
|
|
|
2021-02-23 20:32:35 +01:00
|
|
|
widget.val = function (new_val) {
|
2018-08-15 03:55:44 +02:00
|
|
|
if (new_val) {
|
|
|
|
widget.s = new_val;
|
2020-09-24 07:50:36 +02:00
|
|
|
return this;
|
2018-08-15 03:55:44 +02:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return widget.s;
|
2017-11-09 17:32:28 +01:00
|
|
|
};
|
|
|
|
|
2021-02-23 20:32:35 +01:00
|
|
|
widget.trigger = function (type) {
|
2020-07-20 21:24:26 +02:00
|
|
|
if (type === "focus") {
|
|
|
|
widget.focused = true;
|
|
|
|
} else if (type === "blur") {
|
|
|
|
widget.focused = false;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return this;
|
2017-11-22 04:43:45 +01:00
|
|
|
};
|
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2021-02-14 08:44:04 +01:00
|
|
|
run_test("autosize_textarea", (override) => {
|
2020-09-04 23:49:49 +02:00
|
|
|
const textarea_autosized = {};
|
|
|
|
|
2021-02-14 08:44:04 +01:00
|
|
|
override(autosize, "update", (textarea) => {
|
2020-09-04 23:49:49 +02:00
|
|
|
textarea_autosized.textarea = textarea;
|
|
|
|
textarea_autosized.autosized = true;
|
|
|
|
});
|
2021-02-14 08:44:04 +01:00
|
|
|
|
|
|
|
// Call autosize_textarea with an argument
|
|
|
|
const container = "container-stub";
|
|
|
|
compose_ui.autosize_textarea(container);
|
|
|
|
assert.equal(textarea_autosized.textarea, container);
|
|
|
|
assert(textarea_autosized.autosized);
|
2020-09-04 23:49:49 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("insert_syntax_and_focus", () => {
|
|
|
|
$("#compose-textarea").val("xyz ");
|
2021-02-23 20:32:35 +01:00
|
|
|
$("#compose-textarea").caret = function (syntax) {
|
2017-12-08 16:17:20 +01:00
|
|
|
if (syntax !== undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose-textarea").val($("#compose-textarea").val() + syntax);
|
2020-09-24 07:50:36 +02:00
|
|
|
return this;
|
2017-12-08 16:17:20 +01:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return 4;
|
2017-12-08 16:17:20 +01:00
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.insert_syntax_and_focus(":octopus:");
|
|
|
|
assert.equal($("#compose-textarea").caret(), 4);
|
|
|
|
assert.equal($("#compose-textarea").val(), "xyz :octopus: ");
|
2017-12-08 16:17:20 +01:00
|
|
|
assert($("#compose-textarea").is_focused());
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-12-08 16:17:20 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("smart_insert", () => {
|
|
|
|
let textbox = make_textbox("abc");
|
2017-11-09 17:32:28 +01:00
|
|
|
textbox.caret(4);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.smart_insert(textbox, ":smile:");
|
2017-11-09 17:32:28 +01:00
|
|
|
assert.equal(textbox.insert_pos, 4);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textbox.insert_text, " :smile: ");
|
|
|
|
assert.equal(textbox.val(), "abc :smile: ");
|
2017-11-09 17:32:28 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
2020-07-20 21:24:26 +02:00
|
|
|
textbox.trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.smart_insert(textbox, ":airplane:");
|
|
|
|
assert.equal(textbox.insert_text, ":airplane: ");
|
|
|
|
assert.equal(textbox.val(), "abc :smile: :airplane: ");
|
2017-11-09 17:47:54 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
|
|
|
textbox.caret(0);
|
2020-07-20 21:24:26 +02:00
|
|
|
textbox.trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.smart_insert(textbox, ":octopus:");
|
|
|
|
assert.equal(textbox.insert_text, ":octopus: ");
|
|
|
|
assert.equal(textbox.val(), ":octopus: abc :smile: :airplane: ");
|
2017-11-09 17:32:28 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
2017-11-09 17:57:59 +01:00
|
|
|
textbox.caret(textbox.val().length);
|
2020-07-20 21:24:26 +02:00
|
|
|
textbox.trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.smart_insert(textbox, ":heart:");
|
|
|
|
assert.equal(textbox.insert_text, ":heart: ");
|
|
|
|
assert.equal(textbox.val(), ":octopus: abc :smile: :airplane: :heart: ");
|
2017-11-09 17:57:59 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
2019-02-27 22:17:27 +01:00
|
|
|
// Test handling of spaces for ```quote
|
2020-07-15 01:29:15 +02:00
|
|
|
textbox = make_textbox("");
|
2019-02-27 22:17:27 +01:00
|
|
|
textbox.caret(0);
|
2020-07-20 21:24:26 +02:00
|
|
|
textbox.trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.smart_insert(textbox, "```quote\nquoted message\n```\n");
|
|
|
|
assert.equal(textbox.insert_text, "```quote\nquoted message\n```\n");
|
|
|
|
assert.equal(textbox.val(), "```quote\nquoted message\n```\n");
|
2019-02-27 22:17:27 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
textbox = make_textbox("");
|
2019-02-27 22:17:27 +01:00
|
|
|
textbox.caret(0);
|
2020-07-20 21:24:26 +02:00
|
|
|
textbox.trigger("blur");
|
2019-02-27 22:17:27 +01:00
|
|
|
compose_ui.smart_insert(textbox, "[Quoting…]\n");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textbox.insert_text, "[Quoting…]\n");
|
|
|
|
assert.equal(textbox.val(), "[Quoting…]\n");
|
2019-02-27 22:17:27 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
textbox = make_textbox("abc");
|
2019-02-27 22:17:27 +01:00
|
|
|
textbox.caret(3);
|
2020-07-20 21:24:26 +02:00
|
|
|
textbox.trigger("blur");
|
2019-02-27 22:17:27 +01:00
|
|
|
compose_ui.smart_insert(textbox, " test with space");
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(textbox.insert_text, " test with space ");
|
|
|
|
assert.equal(textbox.val(), "abc test with space ");
|
2019-02-27 22:17:27 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
2017-11-09 17:57:59 +01:00
|
|
|
// Note that we don't have any special logic for strings that are
|
|
|
|
// already surrounded by spaces, since we are usually inserting things
|
|
|
|
// like emojis and file links.
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-11-09 17:32:28 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("replace_syntax", () => {
|
|
|
|
$("#compose-textarea").val("abcabc");
|
2018-08-15 03:55:44 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.replace_syntax("a", "A");
|
|
|
|
assert.equal($("#compose-textarea").val(), "Abcabc");
|
2018-08-15 03:55:44 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.replace_syntax(/b/g, "B");
|
|
|
|
assert.equal($("#compose-textarea").val(), "ABcaBc");
|
2019-04-13 19:50:25 +02:00
|
|
|
|
|
|
|
// Verify we correctly handle `$`s in the replacement syntax
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.replace_syntax("Bca", "$$\\pi$$");
|
|
|
|
assert.equal($("#compose-textarea").val(), "A$$\\pi$$Bc");
|
2018-08-15 03:55:44 +02:00
|
|
|
});
|
2019-07-23 20:13:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("compute_placeholder_text", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
message_type: "stream",
|
|
|
|
stream: "",
|
|
|
|
topic: "",
|
|
|
|
private_message_recipient: "",
|
2019-07-23 20:13:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Stream narrows
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
$t({defaultMessage: "Compose your message here"}),
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
opts.stream = "all";
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), $t({defaultMessage: "Message #all"}));
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
opts.topic = "Test";
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
$t({defaultMessage: "Message #all > Test"}),
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
// PM Narrows
|
|
|
|
opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
message_type: "private",
|
|
|
|
stream: "",
|
|
|
|
topic: "",
|
|
|
|
private_message_recipient: "",
|
2019-07-23 20:13:43 +02:00
|
|
|
};
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
$t({defaultMessage: "Compose your message here"}),
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.private_message_recipient = "bob@zulip.com";
|
2019-07-23 20:13:43 +02:00
|
|
|
user_status.set_status_text({
|
|
|
|
user_id: bob.user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
status_text: "out to lunch",
|
2019-07-23 20:13:43 +02:00
|
|
|
});
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
$t({defaultMessage: "Message Bob (out to lunch)"}),
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.private_message_recipient = "alice@zulip.com";
|
2019-07-23 20:13:43 +02:00
|
|
|
user_status.set_status_text({
|
|
|
|
user_id: alice.user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
status_text: "",
|
2019-07-23 20:13:43 +02:00
|
|
|
});
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), $t({defaultMessage: "Message Alice"}));
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
// Group PM
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.private_message_recipient = "alice@zulip.com,bob@zulip.com";
|
2021-04-13 06:51:54 +02:00
|
|
|
assert.equal(
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
$t({defaultMessage: "Message Alice, Bob"}),
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
});
|