2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const page_params = set_global("page_params", {});
|
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const user_pill = zrequire("user_pill");
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2018-03-07 15:28:11 +01:00
|
|
|
user_id: 99,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Barson",
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const isaac = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "isaac@example.com",
|
2018-03-07 15:28:11 +01:00
|
|
|
user_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Isaac Newton",
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bogus_item = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bogus@example.com",
|
|
|
|
display_value: "bogus@example.com",
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const isaac_item = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "isaac@example.com",
|
|
|
|
display_value: "Isaac Newton",
|
2018-03-07 15:28:11 +01:00
|
|
|
user_id: isaac.user_id,
|
2020-06-24 14:25:17 +02:00
|
|
|
img_src: `/avatar/${isaac.user_id}&s=50`,
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("setup", () => {
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(isaac);
|
2018-06-02 09:27:38 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("create_item", () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
function test_create_item(email, current_items, expected_item) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = user_pill.create_item_from_email(email, current_items);
|
2018-03-07 15:28:11 +01:00
|
|
|
assert.deepEqual(item, expected_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = true;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test_create_item("bogus@example.com", [], bogus_item);
|
|
|
|
test_create_item("bogus@example.com", [bogus_item], undefined);
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test_create_item("isaac@example.com", [], isaac_item);
|
|
|
|
test_create_item("isaac@example.com", [isaac_item], undefined);
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
test_create_item("bogus@example.com", [], undefined);
|
|
|
|
test_create_item("isaac@example.com", [], isaac_item);
|
|
|
|
test_create_item("isaac@example.com", [isaac_item], undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_email", () => {
|
|
|
|
assert.equal(user_pill.get_email_from_item({email: "foo@example.com"}), "foo@example.com");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("append", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let appended;
|
|
|
|
let cleared;
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
function fake_append(opts) {
|
|
|
|
appended = true;
|
|
|
|
assert.equal(opts.email, isaac.email);
|
|
|
|
assert.equal(opts.display_value, isaac.full_name);
|
|
|
|
assert.equal(opts.user_id, isaac.user_id);
|
2018-06-27 20:55:56 +02:00
|
|
|
assert.equal(opts.img_src, isaac_item.img_src);
|
2018-03-07 15:28:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function fake_clear() {
|
|
|
|
cleared = true;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pill_widget = {
|
2018-03-07 15:28:11 +01:00
|
|
|
appendValidatedData: fake_append,
|
|
|
|
clear_text: fake_clear,
|
|
|
|
};
|
|
|
|
|
|
|
|
user_pill.append_person({
|
|
|
|
person: isaac,
|
2020-07-20 22:18:43 +02:00
|
|
|
pill_widget,
|
2018-03-07 15:28:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert(appended);
|
|
|
|
assert(cleared);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_items", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = [isaac_item, bogus_item];
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pill_widget = {
|
2020-07-20 22:18:43 +02:00
|
|
|
items() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return items;
|
|
|
|
},
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.deepEqual(user_pill.get_user_ids(pill_widget), [isaac.user_id]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("typeahead", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = [isaac_item, bogus_item];
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pill_widget = {
|
2020-07-20 22:18:43 +02:00
|
|
|
items() {
|
2020-07-15 00:34:28 +02:00
|
|
|
return items;
|
|
|
|
},
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Both alice and isaac are in our realm, but isaac will be
|
|
|
|
// excluded by virtue of already being one of the widget items.
|
|
|
|
// And then bogus_item is just a red herring to test robustness.
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = user_pill.typeahead_source(pill_widget);
|
2018-03-07 15:28:11 +01:00
|
|
|
assert.deepEqual(result, [alice]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|