2017-11-09 17:32:28 +01:00
|
|
|
zrequire('compose_ui');
|
|
|
|
|
|
|
|
function make_textbox(s) {
|
|
|
|
// Simulate a jQuery textbox for testing purposes.
|
|
|
|
var widget = {};
|
|
|
|
|
|
|
|
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;
|
|
|
|
var before = widget.s.slice(0, widget.pos);
|
|
|
|
var after = widget.s.slice(widget.pos);
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.val = function () {
|
|
|
|
return widget.s;
|
|
|
|
};
|
|
|
|
|
2017-11-22 04:43:45 +01:00
|
|
|
widget.trigger = function () {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
(function test_smart_insert() {
|
|
|
|
var textbox = make_textbox('abc ');
|
|
|
|
textbox.caret(4);
|
|
|
|
|
|
|
|
compose_ui.smart_insert(textbox, ':smile:');
|
|
|
|
assert.equal(textbox.insert_pos, 4);
|
|
|
|
assert.equal(textbox.insert_text, ':smile:');
|
|
|
|
assert.equal(textbox.val(), 'abc :smile:');
|
|
|
|
assert(textbox.focused);
|
|
|
|
|
|
|
|
textbox.blur();
|
|
|
|
compose_ui.smart_insert(textbox, ':airplane:');
|
2017-11-09 17:47:54 +01:00
|
|
|
assert.equal(textbox.insert_text, ' :airplane:');
|
|
|
|
assert.equal(textbox.val(), 'abc :smile: :airplane:');
|
|
|
|
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: ');
|
|
|
|
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:');
|
|
|
|
assert.equal(textbox.insert_text, ' :heart:');
|
|
|
|
assert.equal(textbox.val(), ':octopus: abc :smile: :airplane: :heart:');
|
|
|
|
assert(textbox.focused);
|
|
|
|
|
|
|
|
// 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.
|
2017-11-09 17:32:28 +01:00
|
|
|
}());
|
|
|
|
|