2018-03-07 15:28:11 +01:00
|
|
|
zrequire('people');
|
2018-06-27 20:55:56 +02:00
|
|
|
set_global('md5', function (s) {
|
|
|
|
return 'md5-' + s;
|
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
zrequire('user_pill');
|
|
|
|
|
|
|
|
set_global('page_params', {
|
|
|
|
});
|
|
|
|
|
|
|
|
var alice = {
|
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 99,
|
|
|
|
full_name: 'Alice Barson',
|
|
|
|
};
|
|
|
|
|
|
|
|
var isaac = {
|
|
|
|
email: 'isaac@example.com',
|
|
|
|
user_id: 102,
|
|
|
|
full_name: 'Isaac Newton',
|
|
|
|
};
|
|
|
|
|
|
|
|
var bogus_item = {
|
|
|
|
email: 'bogus@example.com',
|
|
|
|
display_value: 'bogus@example.com',
|
|
|
|
};
|
|
|
|
|
|
|
|
var isaac_item = {
|
|
|
|
email: 'isaac@example.com',
|
|
|
|
display_value: 'Isaac Newton',
|
|
|
|
user_id: isaac.user_id,
|
2018-06-27 20:55:56 +02:00
|
|
|
img_src: 'https://secure.gravatar.com/avatar/md5-isaac@example.com?d=identicon&s=50',
|
2018-03-07 15:28:11 +01:00
|
|
|
};
|
|
|
|
|
2018-06-02 09:27:38 +02:00
|
|
|
run_test('setup', () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
people.add_in_realm(alice);
|
|
|
|
people.add_in_realm(isaac);
|
2018-06-02 09:27:38 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('create_item', () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
|
|
|
|
function test_create_item(email, current_items, expected_item) {
|
|
|
|
var item = user_pill.create_item_from_email(email, current_items);
|
|
|
|
assert.deepEqual(item, expected_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = true;
|
|
|
|
|
|
|
|
test_create_item('bogus@example.com', [], bogus_item);
|
|
|
|
test_create_item('bogus@example.com', [bogus_item], undefined);
|
|
|
|
|
|
|
|
test_create_item('isaac@example.com', [], isaac_item);
|
|
|
|
test_create_item('isaac@example.com', [isaac_item], undefined);
|
|
|
|
|
|
|
|
page_params.realm_is_zephyr_mirror_realm = false;
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_email', () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('append', () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
var appended;
|
|
|
|
var cleared;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pill_widget = {
|
|
|
|
appendValidatedData: fake_append,
|
|
|
|
clear_text: fake_clear,
|
|
|
|
};
|
|
|
|
|
|
|
|
user_pill.append_person({
|
|
|
|
person: isaac,
|
|
|
|
pill_widget: pill_widget,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(appended);
|
|
|
|
assert(cleared);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 15:28:11 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_items', () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
var items = [isaac_item, bogus_item];
|
|
|
|
|
|
|
|
var pill_widget = {
|
|
|
|
items: function () { return items; },
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('typeahead', () => {
|
2018-03-07 15:28:11 +01:00
|
|
|
var items = [isaac_item, bogus_item];
|
|
|
|
|
|
|
|
var pill_widget = {
|
|
|
|
items: function () { return items; },
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
var result = user_pill.typeahead_source(pill_widget);
|
|
|
|
assert.deepEqual(result, [alice]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|