2018-04-10 23:57:51 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
2018-08-01 21:17:03 +02:00
|
|
|
|
|
|
|
const _people = {
|
2018-06-27 20:55:56 +02:00
|
|
|
small_avatar_url_for_person: function () {
|
|
|
|
return 'http://example.com/example.png';
|
|
|
|
},
|
2018-08-01 21:17:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
set_global('people', _people);
|
2018-08-25 17:06:59 +02:00
|
|
|
zrequire('util');
|
|
|
|
|
2018-08-01 21:17:03 +02:00
|
|
|
zrequire('compose_pm_pill');
|
|
|
|
zrequire('input_pill');
|
|
|
|
zrequire('user_pill');
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let pills = {
|
2018-04-10 23:57:51 +02:00
|
|
|
pill: {},
|
|
|
|
};
|
|
|
|
|
2018-05-15 12:40:07 +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,
|
|
|
|
email: 'othello@example.com',
|
|
|
|
full_name: 'Othello',
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const iago = {
|
2018-04-10 23:57:51 +02:00
|
|
|
email: 'iago@zulip.com',
|
|
|
|
user_id: 2,
|
|
|
|
full_name: 'Iago',
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const hamlet = {
|
2018-04-10 23:57:51 +02:00
|
|
|
email: 'hamlet@example.com',
|
|
|
|
user_id: 3,
|
|
|
|
full_name: 'Hamlet',
|
|
|
|
};
|
|
|
|
|
|
|
|
people.get_realm_persons = function () {
|
|
|
|
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
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let all_pills = {};
|
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;
|
2018-04-10 23:57:51 +02:00
|
|
|
assert.equal(all_pills[id], undefined);
|
|
|
|
all_pills[id] = item;
|
|
|
|
};
|
|
|
|
pills.items = function () {
|
|
|
|
return _.values(all_pills);
|
|
|
|
};
|
|
|
|
|
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: {},
|
|
|
|
};
|
2018-06-04 21:13:07 +02:00
|
|
|
all_pills = {};
|
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;
|
|
|
|
assert.equal(value, 'othello@example.com');
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let get_person_from_user_id_called = false;
|
2018-04-10 23:57:51 +02:00
|
|
|
people.get_person_from_user_id = function (id) {
|
|
|
|
get_person_from_user_id_called = true;
|
|
|
|
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);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(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);
|
2018-06-05 08:12:06 +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);
|
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2018-08-25 17:06:59 +02:00
|
|
|
assert.equal(user_ids_string, '1,3');
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = compose_pm_pill.get_emails();
|
2018-04-10 23:57:51 +02:00
|
|
|
assert.equal(emails, 'othello@example.com,hamlet@example.com');
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = compose_pm_pill.get_typeahead_items();
|
2018-04-10 23:57:51 +02:00
|
|
|
assert.deepEqual(items, [{email: 'iago@zulip.com', user_id: 2, full_name: 'Iago'}]);
|
|
|
|
|
|
|
|
test_create_item(create_item_handler);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
assert(get_person_from_user_id_called);
|
|
|
|
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
|
|
|
|
|
|
|
run_test('has_unconverted_data', () => {
|
|
|
|
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,
|
|
|
|
items: () => [{user_id: 99}, {email: 'random@mit.edu'}],
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|