2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const $ = require("./lib/zjquery");
|
2018-08-01 21:17:03 +02:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const input_pill = mock_esm("../src/input_pill");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const compose_pm_pill = zrequire("compose_pm_pill");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_realm} = zrequire("state_data");
|
|
|
|
|
|
|
|
set_realm({});
|
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: {},
|
|
|
|
};
|
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
run_test("pills", ({override, override_rewire}) => {
|
2024-09-17 22:38:04 +02:00
|
|
|
const me = {
|
|
|
|
email: "me@example.com",
|
|
|
|
user_id: 30,
|
|
|
|
full_name: "Me Myself",
|
|
|
|
date_joined: new Date(),
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-09-17 22:38:04 +02:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
people.add_active_user(me);
|
2020-07-29 03:27:36 +02:00
|
|
|
people.add_active_user(othello);
|
|
|
|
people.add_active_user(iago);
|
|
|
|
people.add_active_user(hamlet);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $recipient_stub = $("#private_message_recipient");
|
2021-02-07 18:30:58 +01:00
|
|
|
const pill_container_stub = "pill-container";
|
2022-01-25 11:36:19 +01: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;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!all_pills.has(id));
|
2020-02-09 04:56:27 +01:00
|
|
|
all_pills.set(id, item);
|
2018-04-10 23:57:51 +02:00
|
|
|
};
|
2023-03-02 01:58:25 +01:00
|
|
|
pills.items = () => [...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;
|
2023-08-04 23:40:48 +02:00
|
|
|
override_rewire(people, "get_by_email", (user_email) => {
|
2018-04-10 23:57:51 +02:00
|
|
|
get_by_email_called = true;
|
2022-04-09 23:44:38 +02:00
|
|
|
switch (user_email) {
|
|
|
|
case iago.email:
|
|
|
|
return iago;
|
|
|
|
case othello.email:
|
|
|
|
return othello;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown user email ${user_email}`);
|
2018-04-10 23:57:51 +02:00
|
|
|
}
|
2023-08-04 23:40:48 +02:00
|
|
|
});
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
let get_by_user_id_called = false;
|
2023-08-04 23:40:48 +02:00
|
|
|
override_rewire(people, "get_by_user_id", (id) => {
|
2020-02-05 14:30:59 +01:00
|
|
|
get_by_user_id_called = true;
|
2022-04-09 23:44:38 +02:00
|
|
|
switch (id) {
|
|
|
|
case othello.user_id:
|
|
|
|
return othello;
|
|
|
|
case hamlet.user_id:
|
|
|
|
return hamlet;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown user ID ${id}`);
|
2020-09-24 07:50:36 +02:00
|
|
|
}
|
2023-08-04 23:40:48 +02:00
|
|
|
});
|
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());
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(get_by_email_called);
|
2018-04-10 23:57:51 +02:00
|
|
|
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());
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(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);
|
2024-07-30 06:13:31 +02:00
|
|
|
assert.equal(res.full_name, iago.full_name);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2020-07-29 03:27:36 +02:00
|
|
|
|
|
|
|
(function test_deactivated_pill() {
|
|
|
|
people.deactivate(iago);
|
|
|
|
get_by_email_called = false;
|
|
|
|
const res = handler(iago.email, pills.items());
|
|
|
|
assert.ok(get_by_email_called);
|
|
|
|
assert.equal(typeof res, "object");
|
|
|
|
assert.equal(res.user_id, iago.user_id);
|
2024-07-30 06:13:31 +02:00
|
|
|
assert.equal(res.full_name, iago.full_name);
|
2020-07-29 03:27:36 +02:00
|
|
|
assert.ok(res.deactivated);
|
|
|
|
people.add_active_user(iago);
|
|
|
|
})();
|
2018-04-10 23:57:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function input_pill_stub(opts) {
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal(opts.$container, pill_container_stub);
|
2018-04-10 23:57:51 +02:00
|
|
|
create_item_handler = opts.create_item_from_text;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_item_handler);
|
2018-04-10 23:57:51 +02:00
|
|
|
return pills;
|
|
|
|
}
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(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) => {
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
pills.onPillRemove = (callback) => {
|
|
|
|
callback();
|
|
|
|
};
|
2020-07-29 17:32:39 +02:00
|
|
|
|
2023-07-03 05:48:47 +02:00
|
|
|
let on_pill_create_or_remove_call_count = 0;
|
2023-06-18 08:58:48 +02:00
|
|
|
compose_pm_pill.initialize({
|
2023-07-03 05:48:47 +02:00
|
|
|
on_pill_create_or_remove() {
|
|
|
|
on_pill_create_or_remove_call_count += 1;
|
|
|
|
},
|
2023-06-18 08:58:48 +02:00
|
|
|
});
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(compose_pm_pill.widget);
|
2023-07-03 05:48:47 +02:00
|
|
|
// Called two times via our overridden onPillCreate and onPillRemove methods.
|
|
|
|
// Normally these would be called via `set_from_typeahead` method.
|
|
|
|
assert.equal(on_pill_create_or_remove_call_count, 2);
|
2018-04-10 23:57:51 +02:00
|
|
|
|
|
|
|
compose_pm_pill.set_from_typeahead(othello);
|
|
|
|
compose_pm_pill.set_from_typeahead(hamlet);
|
|
|
|
|
2024-09-17 22:38:04 +02:00
|
|
|
let 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-29 03:27:36 +02:00
|
|
|
assert.deepEqual(items, [
|
|
|
|
{email: "iago@zulip.com", user_id: 2, full_name: "Iago", is_moderator: false},
|
|
|
|
]);
|
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");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(compose_pm_pill.widget);
|
2018-04-10 23:57:51 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(get_by_user_id_called);
|
|
|
|
assert.ok(pills_cleared);
|
|
|
|
assert.ok(appendValue_called);
|
|
|
|
assert.ok(text_cleared);
|
2024-09-17 22:38:04 +02:00
|
|
|
|
|
|
|
compose_pm_pill.set_from_typeahead(me);
|
|
|
|
compose_pm_pill.set_from_typeahead(othello);
|
|
|
|
|
|
|
|
user_ids = compose_pm_pill.get_user_ids();
|
|
|
|
assert.deepEqual(user_ids, [othello.user_id]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-09-18 01:40:27 +02:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
run_test("has_unconverted_data", ({override}) => {
|
|
|
|
override(compose_pm_pill.widget, "is_pending", () => true);
|
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);
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(compose_pm_pill.widget, "is_pending", () => false);
|
|
|
|
override(compose_pm_pill.widget, "items", () => [{user_id: 99}]);
|
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);
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
override(compose_pm_pill.widget, "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);
|
|
|
|
});
|