2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("$", global.make_zjquery());
|
2018-08-01 21:17:03 +02:00
|
|
|
|
|
|
|
const _people = {
|
2020-07-20 22:18:43 +02:00
|
|
|
small_avatar_url_for_person() {
|
2020-07-15 01:29:15 +02:00
|
|
|
return "http://example.com/example.png";
|
2018-06-27 20:55:56 +02:00
|
|
|
},
|
2018-08-01 21:17:03 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("people", _people);
|
2018-08-25 17:06:59 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("compose_pm_pill");
|
|
|
|
zrequire("input_pill");
|
|
|
|
zrequire("user_pill");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let pills = {
|
2018-04-10 23:57:51 +02:00
|
|
|
pill: {},
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("pills", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const othello = {
|
2018-04-10 23:57:51 +02:00
|
|
|
user_id: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "othello@example.com",
|
|
|
|
full_name: "Othello",
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const iago = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "iago@zulip.com",
|
2018-04-10 23:57:51 +02:00
|
|
|
user_id: 2,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Iago",
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const hamlet = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "hamlet@example.com",
|
2018-04-10 23:57:51 +02:00
|
|
|
user_id: 3,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Hamlet",
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
2020-03-21 19:53:16 +01:00
|
|
|
people.get_realm_users = function () {
|
2018-04-10 23:57:51 +02:00
|
|
|
return [iago, othello, hamlet];
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const recipient_stub = $("#private_message_recipient");
|
|
|
|
const pill_container_stub = $('.pill-container[data-before="You and"]');
|
2018-04-10 23:57:51 +02:00
|
|
|
recipient_stub.set_parent(pill_container_stub);
|
2019-11-02 00:06:25 +01:00
|
|
|
let create_item_handler;
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2020-02-09 04:56:27 +01:00
|
|
|
const all_pills = new Map();
|
2018-04-10 23:57:51 +02:00
|
|
|
|
|
|
|
pills.appendValidatedData = function (item) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const id = item.user_id;
|
2020-02-09 04:56:27 +01:00
|
|
|
assert(!all_pills.has(id));
|
|
|
|
all_pills.set(id, item);
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
pills.items = function () {
|
2020-02-09 04:56:27 +01:00
|
|
|
return Array.from(all_pills.values());
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let text_cleared;
|
2018-04-10 23:57:51 +02:00
|
|
|
pills.clear_text = function () {
|
|
|
|
text_cleared = true;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let pills_cleared;
|
2018-04-10 23:57:51 +02:00
|
|
|
pills.clear = function () {
|
|
|
|
pills_cleared = true;
|
|
|
|
pills = {
|
|
|
|
pill: {},
|
|
|
|
};
|
2020-02-09 04:56:27 +01:00
|
|
|
all_pills.clear();
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let appendValue_called;
|
2018-04-10 23:57:51 +02:00
|
|
|
pills.appendValue = function (value) {
|
|
|
|
appendValue_called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(value, "othello@example.com");
|
2018-04-10 23:57:51 +02:00
|
|
|
this.appendValidatedData(othello);
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let get_by_email_called = false;
|
2018-04-10 23:57:51 +02:00
|
|
|
people.get_by_email = function (user_email) {
|
|
|
|
get_by_email_called = true;
|
|
|
|
if (user_email === iago.email) {
|
|
|
|
return iago;
|
|
|
|
}
|
|
|
|
if (user_email === othello.email) {
|
|
|
|
return othello;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
let get_by_user_id_called = false;
|
|
|
|
people.get_by_user_id = function (id) {
|
|
|
|
get_by_user_id_called = true;
|
2018-04-10 23:57:51 +02:00
|
|
|
if (id === othello.user_id) {
|
|
|
|
return othello;
|
|
|
|
}
|
|
|
|
assert.equal(id, 3);
|
|
|
|
return hamlet;
|
|
|
|
};
|
|
|
|
|
|
|
|
function test_create_item(handler) {
|
|
|
|
(function test_rejection_path() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = handler(othello.email, pills.items());
|
2018-04-10 23:57:51 +02:00
|
|
|
assert(get_by_email_called);
|
|
|
|
assert.equal(item, undefined);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-04-10 23:57:51 +02:00
|
|
|
|
|
|
|
(function test_success_path() {
|
|
|
|
get_by_email_called = false;
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = handler(iago.email, pills.items());
|
2018-04-10 23:57:51 +02:00
|
|
|
assert(get_by_email_called);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof res, "object");
|
2018-04-10 23:57:51 +02:00
|
|
|
assert.equal(res.user_id, iago.user_id);
|
|
|
|
assert.equal(res.display_value, iago.full_name);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-04-10 23:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function input_pill_stub(opts) {
|
|
|
|
assert.equal(opts.container, pill_container_stub);
|
|
|
|
create_item_handler = opts.create_item_from_text;
|
|
|
|
assert(create_item_handler);
|
|
|
|
return pills;
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:17:03 +02:00
|
|
|
input_pill.create = input_pill_stub;
|
2018-04-10 23:57:51 +02:00
|
|
|
|
|
|
|
compose_pm_pill.initialize();
|
2018-07-23 02:20:57 +02:00
|
|
|
assert(compose_pm_pill.widget);
|
2018-04-10 23:57:51 +02:00
|
|
|
|
|
|
|
compose_pm_pill.set_from_typeahead(othello);
|
|
|
|
compose_pm_pill.set_from_typeahead(hamlet);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = compose_pm_pill.get_user_ids();
|
2018-04-10 23:57:51 +02:00
|
|
|
assert.deepEqual(user_ids, [othello.user_id, hamlet.user_id]);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = compose_pm_pill.get_user_ids_string();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(user_ids_string, "1,3");
|
2018-08-25 17:06:59 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = compose_pm_pill.get_emails();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(emails, "othello@example.com,hamlet@example.com");
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2020-01-08 15:58:32 +01:00
|
|
|
const persons = [othello, iago, hamlet];
|
|
|
|
const items = compose_pm_pill.filter_taken_users(persons);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(items, [{email: "iago@zulip.com", user_id: 2, full_name: "Iago"}]);
|
2018-04-10 23:57:51 +02:00
|
|
|
|
|
|
|
test_create_item(create_item_handler);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_pm_pill.set_from_emails("othello@example.com");
|
2018-07-23 02:20:57 +02:00
|
|
|
assert(compose_pm_pill.widget);
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
assert(get_by_user_id_called);
|
2018-04-10 23:57:51 +02:00
|
|
|
assert(pills_cleared);
|
|
|
|
assert(appendValue_called);
|
|
|
|
assert(text_cleared);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-09-18 01:40:27 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("has_unconverted_data", () => {
|
2018-09-18 01:40:27 +02:00
|
|
|
compose_pm_pill.widget = {
|
|
|
|
is_pending: () => true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the pill itself has pending data, we have unconverted
|
|
|
|
// data.
|
|
|
|
assert.equal(compose_pm_pill.has_unconverted_data(), true);
|
|
|
|
|
|
|
|
compose_pm_pill.widget = {
|
|
|
|
is_pending: () => false,
|
|
|
|
items: () => [{user_id: 99}],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Our pill is complete and all items containt user_id, so
|
|
|
|
// we do NOT have unconverted data.
|
|
|
|
assert.equal(compose_pm_pill.has_unconverted_data(), false);
|
|
|
|
|
|
|
|
compose_pm_pill.widget = {
|
|
|
|
is_pending: () => false,
|
2020-07-15 01:29:15 +02:00
|
|
|
items: () => [{user_id: 99}, {email: "random@mit.edu"}],
|
2018-09-18 01:40:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// One of our items only knows email (as in a bridge-with-zephyr
|
|
|
|
// scenario where we might not have registered the user yet), so
|
|
|
|
// we have some unconverted data.
|
|
|
|
assert.equal(compose_pm_pill.has_unconverted_data(), true);
|
|
|
|
});
|