2017-11-09 17:32:28 +01:00
|
|
|
zrequire('compose_ui');
|
2019-07-23 20:13:43 +02:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('user_status');
|
2017-11-09 17:32:28 +01:00
|
|
|
|
2017-12-08 16:17:20 +01:00
|
|
|
set_global('document', {
|
|
|
|
execCommand: function () { return false; },
|
|
|
|
});
|
|
|
|
|
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
set_global('blueslip', {});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2019-07-23 20:13:43 +02:00
|
|
|
email: 'alice@zulip.com',
|
|
|
|
user_id: 101,
|
|
|
|
full_name: 'Alice',
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2019-07-23 20:13:43 +02:00
|
|
|
email: 'bob@zulip.com',
|
|
|
|
user_id: 102,
|
|
|
|
full_name: 'Bob',
|
|
|
|
};
|
|
|
|
|
2020-03-21 21:54:16 +01:00
|
|
|
people.add(alice);
|
|
|
|
people.add(bob);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const noop = function () {};
|
2017-12-08 16:17:20 +01: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;
|
|
|
|
|
|
|
|
widget.caret = function (arg) {
|
|
|
|
if (typeof arg === 'number') {
|
|
|
|
widget.pos = arg;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return widget.pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.focus = function () {
|
|
|
|
widget.focused = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.blur = function () {
|
|
|
|
widget.focused = false;
|
|
|
|
};
|
|
|
|
|
2018-08-15 03:55:44 +02:00
|
|
|
widget.val = function (new_val) {
|
|
|
|
if (new_val) {
|
|
|
|
widget.s = new_val;
|
|
|
|
} else {
|
|
|
|
return widget.s;
|
|
|
|
}
|
2017-11-09 17:32:28 +01:00
|
|
|
};
|
|
|
|
|
2017-11-22 04:43:45 +01:00
|
|
|
widget.trigger = function () {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('insert_syntax_and_focus', () => {
|
2017-12-08 16:17:20 +01:00
|
|
|
blueslip.error = noop;
|
|
|
|
blueslip.log = noop;
|
|
|
|
$('#compose-textarea').val("xyz ");
|
|
|
|
$('#compose-textarea').caret = function (syntax) {
|
|
|
|
if (syntax !== undefined) {
|
|
|
|
$('#compose-textarea').val($('#compose-textarea').val() + syntax);
|
|
|
|
} else {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
compose_ui.insert_syntax_and_focus(':octopus:');
|
|
|
|
assert.equal($('#compose-textarea').caret(), 4);
|
2018-03-04 18:24:41 +01:00
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('smart_insert', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let textbox = make_textbox('abc');
|
2017-11-09 17:32:28 +01:00
|
|
|
textbox.caret(4);
|
|
|
|
|
|
|
|
compose_ui.smart_insert(textbox, ':smile:');
|
|
|
|
assert.equal(textbox.insert_pos, 4);
|
2018-03-04 18:24:41 +01:00
|
|
|
assert.equal(textbox.insert_text, ' :smile: ');
|
|
|
|
assert.equal(textbox.val(), 'abc :smile: ');
|
2017-11-09 17:32:28 +01:00
|
|
|
assert(textbox.focused);
|
|
|
|
|
|
|
|
textbox.blur();
|
|
|
|
compose_ui.smart_insert(textbox, ':airplane:');
|
2018-03-04 18:24:41 +01:00
|
|
|
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);
|
|
|
|
textbox.blur();
|
|
|
|
compose_ui.smart_insert(textbox, ':octopus:');
|
2017-11-09 17:57:59 +01:00
|
|
|
assert.equal(textbox.insert_text, ':octopus: ');
|
2018-03-04 18:24:41 +01:00
|
|
|
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);
|
|
|
|
textbox.blur();
|
|
|
|
compose_ui.smart_insert(textbox, ':heart:');
|
2018-03-04 18:24:41 +01:00
|
|
|
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
|
|
|
|
textbox = make_textbox('');
|
|
|
|
textbox.caret(0);
|
|
|
|
textbox.blur();
|
|
|
|
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');
|
|
|
|
assert(textbox.focused);
|
|
|
|
|
|
|
|
textbox = make_textbox('');
|
|
|
|
textbox.caret(0);
|
|
|
|
textbox.blur();
|
|
|
|
compose_ui.smart_insert(textbox, "[Quoting…]\n");
|
|
|
|
assert.equal(textbox.insert_text, '[Quoting…]\n');
|
|
|
|
assert.equal(textbox.val(), '[Quoting…]\n');
|
|
|
|
assert(textbox.focused);
|
|
|
|
|
|
|
|
textbox = make_textbox('abc');
|
|
|
|
textbox.caret(3);
|
|
|
|
textbox.blur();
|
|
|
|
compose_ui.smart_insert(textbox, " test with space");
|
|
|
|
assert.equal(textbox.insert_text, ' test with space ');
|
|
|
|
assert.equal(textbox.val(), 'abc test with space ');
|
|
|
|
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
|
|
|
|
2018-08-15 03:55:44 +02:00
|
|
|
run_test('replace_syntax', () => {
|
|
|
|
$('#compose-textarea').val('abcabc');
|
|
|
|
|
|
|
|
compose_ui.replace_syntax('a', 'A');
|
|
|
|
assert.equal($('#compose-textarea').val(), 'Abcabc');
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
run_test('compute_placeholder_text', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let opts = {
|
2019-07-23 20:13:43 +02:00
|
|
|
message_type: 'stream',
|
|
|
|
stream: '',
|
|
|
|
topic: '',
|
|
|
|
private_message_recipient: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Stream narrows
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Compose your message here"));
|
|
|
|
|
|
|
|
opts.stream = "all";
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Message #all"));
|
|
|
|
|
|
|
|
opts.topic = "Test";
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Message #all > Test"));
|
|
|
|
|
|
|
|
// PM Narrows
|
|
|
|
opts = {
|
|
|
|
message_type: 'private',
|
|
|
|
stream: '',
|
|
|
|
topic: '',
|
|
|
|
private_message_recipient: '',
|
|
|
|
};
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Compose your message here"));
|
|
|
|
|
|
|
|
opts.private_message_recipient = 'bob@zulip.com';
|
|
|
|
user_status.set_status_text({
|
|
|
|
user_id: bob.user_id,
|
|
|
|
status_text: 'out to lunch',
|
|
|
|
});
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Message Bob (out to lunch)"));
|
|
|
|
|
|
|
|
opts.private_message_recipient = 'alice@zulip.com';
|
|
|
|
user_status.set_status_text({
|
|
|
|
user_id: alice.user_id,
|
|
|
|
status_text: '',
|
|
|
|
});
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Message Alice"));
|
|
|
|
|
|
|
|
// Group PM
|
|
|
|
opts.private_message_recipient = 'alice@zulip.com,bob@zulip.com';
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), i18n.t("Message Alice, Bob"));
|
|
|
|
});
|