2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-02-25 16:56:54 +01:00
|
|
|
const {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");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2018-08-25 17:06:59 +02:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const compose_pm_pill = zrequire("compose_pm_pill");
|
|
|
|
const input_pill = zrequire("input_pill");
|
2021-02-25 16:56:54 +01:00
|
|
|
const compose_actions = set_global("compose_actions", {});
|
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: {},
|
|
|
|
};
|
|
|
|
|
2021-02-25 16:56:54 +01:00
|
|
|
run_test("pills", (override) => {
|
|
|
|
override(compose_actions, "update_placeholder_text", () => {});
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
people.get_realm_users = () => [iago, othello, hamlet];
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const recipient_stub = $("#private_message_recipient");
|
2021-02-07 18:30:58 +01:00
|
|
|
const pill_container_stub = "pill-container";
|
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
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.appendValidatedData = (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
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.items = () => Array.from(all_pills.values());
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let text_cleared;
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.clear_text = () => {
|
2018-04-10 23:57:51 +02:00
|
|
|
text_cleared = true;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let pills_cleared;
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.clear = () => {
|
2018-04-10 23:57:51 +02:00
|
|
|
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;
|
2021-02-23 14:37:26 +01:00
|
|
|
people.get_by_email = (user_email) => {
|
2018-04-10 23:57:51 +02:00
|
|
|
get_by_email_called = true;
|
|
|
|
if (user_email === iago.email) {
|
|
|
|
return iago;
|
|
|
|
}
|
|
|
|
if (user_email === othello.email) {
|
|
|
|
return othello;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
throw new Error(`Unknown user email ${user_email}`);
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
let get_by_user_id_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
people.get_by_user_id = (id) => {
|
2020-02-05 14:30:59 +01:00
|
|
|
get_by_user_id_called = true;
|
2018-04-10 23:57:51 +02:00
|
|
|
if (id === othello.user_id) {
|
|
|
|
return othello;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
if (id === hamlet.user_id) {
|
|
|
|
return hamlet;
|
|
|
|
}
|
|
|
|
throw new Error(`Unknown user ID ${id}`);
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-07-29 17:32:39 +02:00
|
|
|
// We stub the return value of input_pill.create(), manually add widget functions to it.
|
2021-02-25 16:56:54 +01:00
|
|
|
pills.onPillCreate = (callback) => {
|
|
|
|
// Exercise our callback for line coverage. It is
|
|
|
|
// just compose_actions.update_placeholder_text(),
|
|
|
|
// which we override.
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
pills.onPillRemove = (callback) => {
|
|
|
|
callback();
|
|
|
|
};
|
2020-07-29 17:32:39 +02:00
|
|
|
|
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", () => {
|
2021-02-28 00:38:58 +01:00
|
|
|
compose_pm_pill.__Rewire__("widget", {
|
2018-09-18 01:40:27 +02:00
|
|
|
is_pending: () => true,
|
2021-02-28 00:38:58 +01:00
|
|
|
});
|
2018-09-18 01:40:27 +02:00
|
|
|
|
|
|
|
// If the pill itself has pending data, we have unconverted
|
|
|
|
// data.
|
|
|
|
assert.equal(compose_pm_pill.has_unconverted_data(), true);
|
|
|
|
|
2021-02-28 00:38:58 +01:00
|
|
|
compose_pm_pill.__Rewire__("widget", {
|
2018-09-18 01:40:27 +02:00
|
|
|
is_pending: () => false,
|
|
|
|
items: () => [{user_id: 99}],
|
2021-02-28 00:38:58 +01:00
|
|
|
});
|
2018-09-18 01:40:27 +02:00
|
|
|
|
2020-08-11 01:47:44 +02:00
|
|
|
// Our pill is complete and all items contain user_id, so
|
2018-09-18 01:40:27 +02:00
|
|
|
// we do NOT have unconverted data.
|
|
|
|
assert.equal(compose_pm_pill.has_unconverted_data(), false);
|
|
|
|
|
2021-02-28 00:38:58 +01:00
|
|
|
compose_pm_pill.__Rewire__("widget", {
|
2018-09-18 01:40:27 +02:00
|
|
|
is_pending: () => false,
|
2020-07-15 01:29:15 +02:00
|
|
|
items: () => [{user_id: 99}, {email: "random@mit.edu"}],
|
2021-02-28 00:38:58 +01:00
|
|
|
});
|
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);
|
|
|
|
});
|