2018-03-04 17:44:12 +01:00
|
|
|
zrequire('dict');
|
2018-03-06 15:03:20 +01:00
|
|
|
zrequire('user_pill');
|
2018-01-10 04:08:16 +01:00
|
|
|
zrequire('settings_user_groups');
|
|
|
|
|
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
set_global('i18n', global.stub_i18n);
|
|
|
|
|
|
|
|
var noop = function () {};
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
var pills = {
|
|
|
|
pill: {},
|
|
|
|
};
|
2018-01-10 04:08:16 +01:00
|
|
|
set_global('channel', {});
|
2018-01-17 09:13:19 +01:00
|
|
|
set_global('templates', {});
|
|
|
|
set_global('blueslip', {});
|
|
|
|
set_global('typeahead_helper', {});
|
2018-01-10 04:08:16 +01:00
|
|
|
set_global('user_groups', {
|
|
|
|
get_user_group_from_id: noop,
|
|
|
|
remove: noop,
|
|
|
|
add: noop,
|
|
|
|
});
|
|
|
|
set_global('ui_report', {});
|
|
|
|
set_global('people', {
|
|
|
|
my_current_user_id: noop,
|
|
|
|
});
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
var user_group_selector = "#user-groups #1";
|
|
|
|
var cancel_selector = "#user-groups #1 .cancel";
|
|
|
|
var saved_selector = "#user-groups #1 .saved";
|
|
|
|
var name_selector = "#user-groups #1 .name";
|
|
|
|
var description_selector = "#user-groups #1 .description";
|
2018-03-15 12:56:31 +01:00
|
|
|
var instructions_selector = "#user-groups #1 .save-instructions";
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
(function test_populate_user_groups() {
|
|
|
|
var realm_user_group = {
|
|
|
|
id: 1,
|
|
|
|
name: 'Mobile',
|
|
|
|
description: 'All mobile people',
|
2018-03-04 17:44:12 +01:00
|
|
|
members: Dict.from_array([2, 4]),
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
|
|
|
var iago = {
|
|
|
|
email: 'iago@zulip.com',
|
|
|
|
user_id: 2,
|
|
|
|
full_name: 'Iago',
|
|
|
|
};
|
|
|
|
var alice = {
|
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 31,
|
|
|
|
full_name: 'Alice',
|
|
|
|
};
|
|
|
|
var bob = {
|
|
|
|
email: 'bob@example.com',
|
|
|
|
user_id: 32,
|
|
|
|
full_name: 'Bob',
|
|
|
|
};
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
people.get_realm_persons = function () {
|
|
|
|
return [iago, alice, bob];
|
|
|
|
};
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
user_groups.get_realm_user_groups = function () {
|
|
|
|
return [realm_user_group];
|
|
|
|
};
|
|
|
|
var templates_render_called = false;
|
|
|
|
var fake_rendered_temp = $.create('fake_admin_user_group_list_template_rendered');
|
|
|
|
templates.render = function (template, args) {
|
|
|
|
assert.equal(template, 'admin_user_group_list');
|
|
|
|
assert.equal(args.user_group.id, 1);
|
|
|
|
assert.equal(args.user_group.name, 'Mobile');
|
|
|
|
assert.equal(args.user_group.description, 'All mobile people');
|
|
|
|
templates_render_called = true;
|
|
|
|
return fake_rendered_temp;
|
|
|
|
};
|
|
|
|
|
|
|
|
var user_groups_list_append_called = false;
|
|
|
|
$('#user-groups').append = function (rendered_temp) {
|
|
|
|
assert.equal(rendered_temp, fake_rendered_temp);
|
|
|
|
user_groups_list_append_called = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var get_person_from_user_id_called = false;
|
|
|
|
var blueslip_warn_called = false;
|
|
|
|
people.get_person_from_user_id = function (user_id) {
|
|
|
|
if (user_id === iago.user_id) {
|
|
|
|
return iago;
|
|
|
|
}
|
|
|
|
assert.equal(user_id, 4);
|
|
|
|
blueslip.warn = function (err_msg) {
|
|
|
|
assert.equal(err_msg, 'Unknown user ID 4 in members of user group Mobile');
|
|
|
|
blueslip_warn_called = true;
|
|
|
|
};
|
|
|
|
get_person_from_user_id_called = true;
|
|
|
|
};
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
var all_pills = {};
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
var pill_container_stub = $('.pill-container[data-group-pills="Mobile"]');
|
2018-03-06 15:03:20 +01:00
|
|
|
pills.appendValidatedData = function (item) {
|
|
|
|
var id = item.user_id;
|
|
|
|
assert.equal(all_pills[id], undefined);
|
|
|
|
all_pills[id] = item;
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2018-03-06 15:03:20 +01:00
|
|
|
pills.items = function () {
|
|
|
|
return _.values(all_pills);
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
var text_cleared;
|
|
|
|
pills.clear_text = function () {
|
|
|
|
text_cleared = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var create_item_handler;
|
|
|
|
|
|
|
|
function input_pill_stub(opts) {
|
|
|
|
assert.equal(opts.container, pill_container_stub);
|
|
|
|
create_item_handler = opts.create_item_from_text;
|
|
|
|
assert(create_item_handler);
|
2018-01-17 09:13:19 +01:00
|
|
|
return pills;
|
|
|
|
}
|
|
|
|
var input_field_stub = $.create('fake-input-field');
|
|
|
|
pill_container_stub.children = function () {
|
|
|
|
return input_field_stub;
|
|
|
|
};
|
|
|
|
|
|
|
|
var input_typeahead_called = false;
|
|
|
|
input_field_stub.typeahead = function (config) {
|
|
|
|
assert.equal(config.items, 5);
|
|
|
|
assert(config.fixed);
|
|
|
|
assert(config.dropup);
|
|
|
|
assert(config.stopAdvance);
|
|
|
|
assert.equal(typeof(config.source), 'function');
|
|
|
|
assert.equal(typeof(config.highlighter), 'function');
|
|
|
|
assert.equal(typeof(config.matcher), 'function');
|
|
|
|
assert.equal(typeof(config.sorter), 'function');
|
|
|
|
assert.equal(typeof(config.updater), 'function');
|
|
|
|
|
|
|
|
(function test_highlighter() {
|
|
|
|
var fake_person = $.create('fake-person');
|
|
|
|
typeahead_helper.render_person = function () {
|
|
|
|
return fake_person;
|
|
|
|
};
|
|
|
|
assert.equal(config.highlighter(), fake_person);
|
|
|
|
}());
|
|
|
|
|
|
|
|
var fake_context = {
|
|
|
|
query: 'ali',
|
|
|
|
};
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
(function test_source() {
|
|
|
|
var result = config.source.call(fake_context, iago);
|
|
|
|
var emails = _.pluck(result, 'email').sort();
|
|
|
|
assert.deepEqual(emails, [alice.email, bob.email]);
|
|
|
|
}());
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
(function test_matcher() {
|
|
|
|
/* Here the query doesn't begin with an '@' because typeahead is triggered
|
|
|
|
by the '@' sign and thus removed in the query. */
|
|
|
|
var result = config.matcher.call(fake_context, iago);
|
|
|
|
assert(!result);
|
|
|
|
|
|
|
|
result = config.matcher.call(fake_context, alice);
|
|
|
|
assert(result);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_sorter() {
|
|
|
|
var sort_recipientbox_typeahead_called = false;
|
|
|
|
typeahead_helper.sort_recipientbox_typeahead = function () {
|
|
|
|
sort_recipientbox_typeahead_called = true;
|
|
|
|
};
|
|
|
|
config.sorter.call(fake_context);
|
|
|
|
assert(sort_recipientbox_typeahead_called);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_updater() {
|
|
|
|
input_field_stub.text('@ali');
|
|
|
|
user_groups.get_user_group_from_id = function () {
|
|
|
|
return realm_user_group;
|
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
|
|
|
|
var saved_fade_out_called = false;
|
|
|
|
var cancel_fade_to_called = false;
|
2018-03-15 12:56:31 +01:00
|
|
|
var instructions_fade_to_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
$(saved_selector).fadeOut = function () {
|
|
|
|
saved_fade_out_called = true;
|
|
|
|
};
|
|
|
|
$(cancel_selector).css = function (data) {
|
|
|
|
if (typeof(data) === 'string') {
|
|
|
|
assert.equal(data, 'display');
|
|
|
|
}
|
|
|
|
assert.equal(typeof(data), 'object');
|
|
|
|
assert.equal(data.display, 'inline-block');
|
|
|
|
assert.equal(data.opacity, '0');
|
|
|
|
return $(cancel_selector);
|
|
|
|
};
|
|
|
|
$(cancel_selector).fadeTo = function () {
|
|
|
|
cancel_fade_to_called = true;
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2018-03-15 12:56:31 +01:00
|
|
|
$(instructions_selector).css = function (data) {
|
|
|
|
if (typeof(data) === 'string') {
|
|
|
|
assert.equal(data, 'display');
|
|
|
|
}
|
|
|
|
assert.equal(typeof(data), 'object');
|
|
|
|
assert.equal(data.display, 'block');
|
|
|
|
assert.equal(data.opacity, '0');
|
|
|
|
return $(instructions_selector);
|
|
|
|
};
|
|
|
|
$(instructions_selector).fadeTo = function () {
|
|
|
|
instructions_fade_to_called = true;
|
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
text_cleared = false;
|
2018-01-17 09:13:19 +01:00
|
|
|
config.updater(alice);
|
2018-03-15 12:10:07 +01:00
|
|
|
// update_cancel_button is called.
|
|
|
|
assert(saved_fade_out_called);
|
|
|
|
assert(cancel_fade_to_called);
|
2018-03-15 12:56:31 +01:00
|
|
|
assert(instructions_fade_to_called);
|
2018-03-06 15:03:20 +01:00
|
|
|
assert.equal(text_cleared, true);
|
2018-01-17 09:13:19 +01:00
|
|
|
}());
|
|
|
|
input_typeahead_called = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
var get_by_email_called = false;
|
|
|
|
people.get_by_email = function (user_email) {
|
|
|
|
get_by_email_called = true;
|
|
|
|
if (user_email === iago.email) {
|
|
|
|
return iago;
|
|
|
|
}
|
|
|
|
if (user_email === bob.email) {
|
|
|
|
return bob;
|
|
|
|
}
|
|
|
|
assert.equal(user_email,
|
|
|
|
'Expected user email to be of Alice or Iago here.');
|
|
|
|
};
|
|
|
|
pills.onPillCreate = function (handler) {
|
|
|
|
assert.equal(typeof(handler), 'function');
|
2018-03-06 15:03:20 +01:00
|
|
|
handler();
|
|
|
|
};
|
|
|
|
|
|
|
|
function test_create_item(handler) {
|
2018-01-17 09:13:19 +01:00
|
|
|
(function test_rejection_path() {
|
2018-03-06 15:03:20 +01:00
|
|
|
var item = handler(iago.email, pills.items());
|
2018-01-17 09:13:19 +01:00
|
|
|
assert(get_by_email_called);
|
2018-03-06 15:03:20 +01:00
|
|
|
assert.equal(item, undefined);
|
2018-01-17 09:13:19 +01:00
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_success_path() {
|
|
|
|
get_by_email_called = false;
|
2018-03-06 15:03:20 +01:00
|
|
|
var res = handler(bob.email, pills.items());
|
2018-01-17 09:13:19 +01:00
|
|
|
assert(get_by_email_called);
|
|
|
|
assert.equal(typeof(res), 'object');
|
2018-03-06 15:03:20 +01:00
|
|
|
assert.equal(res.user_id, bob.user_id);
|
|
|
|
assert.equal(res.display_value, bob.full_name);
|
2018-01-17 09:13:19 +01:00
|
|
|
}());
|
2018-03-06 15:03:20 +01:00
|
|
|
}
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
pills.onPillRemove = function (handler) {
|
2018-03-13 23:51:53 +01:00
|
|
|
global.patch_builtin('setTimeout', function (func) {
|
|
|
|
func();
|
|
|
|
});
|
2018-03-04 17:44:12 +01:00
|
|
|
realm_user_group.members = Dict.from_array([2, 31]);
|
2018-01-17 09:13:19 +01:00
|
|
|
handler();
|
|
|
|
};
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
set_global('input_pill', {
|
|
|
|
create: input_pill_stub,
|
|
|
|
});
|
2018-01-10 04:08:16 +01:00
|
|
|
settings_user_groups.set_up();
|
2018-01-17 09:13:19 +01:00
|
|
|
assert(templates_render_called);
|
|
|
|
assert(user_groups_list_append_called);
|
|
|
|
assert(get_person_from_user_id_called);
|
|
|
|
assert(blueslip_warn_called);
|
|
|
|
assert(input_typeahead_called);
|
2018-03-06 15:03:20 +01:00
|
|
|
test_create_item(create_item_handler);
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
// Tests for settings_user_groups.set_up workflow.
|
2018-01-10 04:08:16 +01:00
|
|
|
assert.equal(typeof($('.organization').get_on_handler("submit", "form.admin-user-group-form")), 'function');
|
|
|
|
assert.equal(typeof($('#user-groups').get_on_handler('click', '.delete')), 'function');
|
|
|
|
assert.equal(typeof($('#user-groups').get_on_handler('keypress', '.user-group h4 > span')), 'function');
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_reload() {
|
|
|
|
$('#user-groups').html('Some text');
|
|
|
|
var populate_user_groups_called = false;
|
|
|
|
settings_user_groups.populate_user_groups = function () {
|
|
|
|
populate_user_groups_called = true;
|
|
|
|
};
|
|
|
|
settings_user_groups.reload();
|
|
|
|
assert(populate_user_groups_called);
|
|
|
|
assert.equal($('#user-groups').html(), '');
|
|
|
|
}());
|
|
|
|
|
2018-03-04 17:31:22 +01:00
|
|
|
(function test_reset() {
|
|
|
|
settings_user_groups.reset();
|
|
|
|
var result = settings_user_groups.reload();
|
|
|
|
assert.equal(result, undefined);
|
|
|
|
}());
|
|
|
|
|
2018-01-10 04:08:16 +01:00
|
|
|
(function test_on_events() {
|
|
|
|
(function test_admin_user_group_form_submit_triggered() {
|
|
|
|
var handler = $('.organization').get_on_handler("submit", "form.admin-user-group-form");
|
|
|
|
var event = {
|
|
|
|
stopPropagation: noop,
|
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
var fake_this = $.create('fake-form.admin-user-group-form');
|
|
|
|
var fake_object_array = [{
|
|
|
|
name: 'fake-name',
|
|
|
|
value: '',
|
|
|
|
}, {
|
|
|
|
name: 'fake-name',
|
|
|
|
value: 'fake-value',
|
|
|
|
}];
|
|
|
|
fake_this.serializeArray = function () {
|
|
|
|
return fake_object_array;
|
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
channel.post = function (opts) {
|
2018-01-10 04:08:16 +01:00
|
|
|
var data = {
|
|
|
|
members: '[null]',
|
|
|
|
};
|
|
|
|
data[fake_object_array[1].name] = fake_object_array[1].value;
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(opts.url, "/json/user_groups/create");
|
|
|
|
assert.deepEqual(opts.data, data);
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
(function test_post_success() {
|
|
|
|
$('#admin-user-group-status').show();
|
|
|
|
$("form.admin-user-group-form input[type='text']").val('fake-content');
|
|
|
|
ui_report.success = function (text, ele) {
|
|
|
|
assert.equal(text, 'translated: User group added!');
|
|
|
|
assert.equal(ele, $('#admin-user-group-status'));
|
|
|
|
};
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.success();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
assert(!$('#admin-user-group-status').visible());
|
|
|
|
assert.equal($("form.admin-user-group-form input[type='text']").val(), '');
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_post_error() {
|
|
|
|
$('#admin-user-group-status').show();
|
|
|
|
ui_report.error = function (error_msg, error_obj, ele) {
|
|
|
|
var xhr = {
|
|
|
|
responseText: '{"msg":"fake-msg"}',
|
|
|
|
};
|
|
|
|
assert.equal(error_msg, 'translated: Failed');
|
|
|
|
assert.deepEqual(error_obj, xhr);
|
|
|
|
assert.equal(ele, $('#admin-user-group-status'));
|
|
|
|
};
|
|
|
|
var xhr = {
|
|
|
|
responseText: '{"msg":"fake-msg", "attrib":"val"}',
|
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.error(xhr);
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
assert(!$('#admin-user-group-status').visible());
|
|
|
|
}());
|
|
|
|
};
|
|
|
|
|
|
|
|
handler.call(fake_this, event);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_user_groups_delete_click_triggered() {
|
|
|
|
var handler = $('#user-groups').get_on_handler("click", ".delete");
|
|
|
|
var fake_this = $.create('fake-#user-groups.delete_btn');
|
|
|
|
fake_this.set_parents_result('.user-group', $('.user-group'));
|
|
|
|
$('.user-group').attr('id', '1');
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
channel.del = function (opts) {
|
2018-01-10 04:08:16 +01:00
|
|
|
var data = {
|
|
|
|
id: 1,
|
|
|
|
};
|
|
|
|
var settings_user_groups_reload_called = false;
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(opts.url, "/json/user_groups/1");
|
|
|
|
assert.deepEqual(opts.data, data);
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
settings_user_groups.reload = function () {
|
|
|
|
settings_user_groups_reload_called = true;
|
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.success();
|
2018-01-10 04:08:16 +01:00
|
|
|
assert(settings_user_groups_reload_called);
|
|
|
|
|
|
|
|
fake_this.text(i18n.t('fake-text'));
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.error();
|
2018-01-10 04:08:16 +01:00
|
|
|
assert.equal(fake_this.text(), 'translated: Failed!');
|
|
|
|
};
|
|
|
|
|
|
|
|
handler.call(fake_this);
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_user_groups_keypress_enter_triggered() {
|
|
|
|
var handler = $('#user-groups').get_on_handler("keypress", ".user-group h4 > span");
|
|
|
|
var default_action_for_enter_stopped = false;
|
|
|
|
var event = {
|
|
|
|
which: 13,
|
|
|
|
preventDefault: function () {
|
|
|
|
default_action_for_enter_stopped = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
handler(event);
|
|
|
|
assert(default_action_for_enter_stopped);
|
|
|
|
}());
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
(function test_do_not_blur() {
|
|
|
|
var blur_event_classes = [".name", ".description", ".input"];
|
|
|
|
var api_endpoint_called = false;
|
|
|
|
channel.post = function () {
|
|
|
|
api_endpoint_called = true;
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
channel.patch = noop;
|
2018-03-14 16:45:37 +01:00
|
|
|
var fake_this = $.create('fake-#user-groups_do_not_blur');
|
|
|
|
var event = {
|
|
|
|
relatedTarget: fake_this,
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
// Any of the blur_exceptions trigger blur event.
|
2018-03-14 16:45:37 +01:00
|
|
|
_.each(blur_event_classes, function (class_name) {
|
2018-03-15 12:10:07 +01:00
|
|
|
var handler = $(user_group_selector).get_on_handler("blur", class_name);
|
2018-03-14 16:45:37 +01:00
|
|
|
var blur_exceptions = _.without([".pill-container", ".name", ".description", ".input", ".delete"],
|
|
|
|
class_name);
|
|
|
|
_.each(blur_exceptions, function (blur_exception) {
|
|
|
|
api_endpoint_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
fake_this.closest = function (class_name) {
|
|
|
|
if (class_name === blur_exception || class_name === user_group_selector) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
handler.call(fake_this, event);
|
|
|
|
assert(!api_endpoint_called);
|
|
|
|
});
|
|
|
|
|
|
|
|
api_endpoint_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
fake_this.closest = function (class_name) {
|
|
|
|
if (class_name === ".typeahead") {
|
2018-03-14 16:45:37 +01:00
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
return [];
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
2018-03-14 16:45:37 +01:00
|
|
|
handler.call(fake_this, event);
|
|
|
|
assert(!api_endpoint_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
|
|
|
|
// Cancel button triggers blur event.
|
|
|
|
var settings_user_groups_reload_called = false;
|
|
|
|
settings_user_groups.reload = function () {
|
|
|
|
settings_user_groups_reload_called = true;
|
|
|
|
};
|
|
|
|
api_endpoint_called = false;
|
|
|
|
fake_this.closest = function (class_name) {
|
|
|
|
if (class_name === ".cancel" || class_name === user_group_selector) {
|
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
handler.call(fake_this, event);
|
|
|
|
assert(!api_endpoint_called);
|
|
|
|
assert(settings_user_groups_reload_called);
|
2018-03-14 16:45:37 +01:00
|
|
|
});
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
}());
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
(function test_update_cancel_button() {
|
|
|
|
var handler_name = $(user_group_selector).get_on_handler("input", ".name");
|
|
|
|
var handler_desc = $(user_group_selector).get_on_handler("input", ".description");
|
|
|
|
var sib_des = $(description_selector);
|
|
|
|
var sib_name = $(name_selector);
|
|
|
|
sib_name.text(i18n.t('mobile'));
|
|
|
|
sib_des.text(i18n.t('All mobile members'));
|
|
|
|
|
|
|
|
var group_data = {
|
|
|
|
name: 'translated: mobile',
|
|
|
|
description: 'translated: All mobile members',
|
|
|
|
members: Dict.from_array([2, 31])};
|
|
|
|
user_groups.get_user_group_from_id = function () {
|
|
|
|
return group_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
var cancel_fade_out_called = false;
|
2018-03-15 12:56:31 +01:00
|
|
|
var instructions_fade_out_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
$(cancel_selector).show();
|
|
|
|
$(cancel_selector).fadeOut = function () {
|
|
|
|
cancel_fade_out_called = true;
|
|
|
|
};
|
2018-03-15 12:56:31 +01:00
|
|
|
$(instructions_selector).fadeOut = function () {
|
|
|
|
instructions_fade_out_called = true;
|
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
|
|
|
|
// Cancel button removed if user group if user group has no changes.
|
|
|
|
var fake_this = $.create('fake-#update_cancel_button');
|
|
|
|
handler_name.call(fake_this);
|
|
|
|
assert(cancel_fade_out_called);
|
2018-03-15 12:56:31 +01:00
|
|
|
assert(instructions_fade_out_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
|
|
|
|
// Check for handler_desc to achieve 100% coverage.
|
|
|
|
cancel_fade_out_called = false;
|
2018-03-15 12:56:31 +01:00
|
|
|
instructions_fade_out_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
handler_desc.call(fake_this);
|
|
|
|
assert(cancel_fade_out_called);
|
2018-03-15 12:56:31 +01:00
|
|
|
assert(instructions_fade_out_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
}());
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
(function test_user_groups_save_group_changes_triggered() {
|
2018-03-15 12:10:07 +01:00
|
|
|
var handler_name = $(user_group_selector).get_on_handler("blur", ".name");
|
|
|
|
var handler_desc = $(user_group_selector).get_on_handler("blur", ".description");
|
|
|
|
var sib_des = $(description_selector);
|
|
|
|
var sib_name = $(name_selector);
|
2018-01-10 04:08:16 +01:00
|
|
|
sib_name.text(i18n.t('mobile'));
|
|
|
|
sib_des.text(i18n.t('All mobile members'));
|
2018-03-14 16:45:37 +01:00
|
|
|
|
|
|
|
var group_data = {members: Dict.from_array([2, 31])};
|
2018-01-10 04:08:16 +01:00
|
|
|
user_groups.get_user_group_from_id = function () {
|
|
|
|
return group_data;
|
|
|
|
};
|
2018-03-14 16:45:37 +01:00
|
|
|
var api_endpoint_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
var cancel_fade_out_called = false;
|
|
|
|
var saved_fade_to_called = false;
|
2018-03-15 12:56:31 +01:00
|
|
|
var instructions_fade_out_called = false;
|
|
|
|
$(instructions_selector).fadeOut = function () {
|
|
|
|
instructions_fade_out_called = true;
|
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
$(cancel_selector).fadeOut = function () {
|
|
|
|
cancel_fade_out_called = true;
|
|
|
|
};
|
|
|
|
$(saved_selector).css = function (data) {
|
|
|
|
if (typeof(data) === 'string') {
|
|
|
|
assert.equal(data, 'display');
|
|
|
|
}
|
|
|
|
assert.equal(typeof(data), 'object');
|
|
|
|
assert.equal(data.display, 'inline-block');
|
|
|
|
assert.equal(data.opacity, '0');
|
|
|
|
return $(saved_selector);
|
|
|
|
};
|
|
|
|
$(saved_selector).fadeTo = function () {
|
|
|
|
saved_fade_to_called = true;
|
|
|
|
};
|
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
channel.patch = function (opts) {
|
2018-03-14 16:45:37 +01:00
|
|
|
assert.equal(opts.url, "/json/user_groups/1");
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(opts.data.name, 'translated: mobile');
|
|
|
|
assert.equal(opts.data.description, 'translated: All mobile members');
|
2018-03-14 16:45:37 +01:00
|
|
|
api_endpoint_called = true;
|
2018-01-10 04:08:16 +01:00
|
|
|
(function test_post_success() {
|
2018-03-15 12:10:07 +01:00
|
|
|
global.patch_builtin('setTimeout', function (func) {
|
|
|
|
func();
|
|
|
|
});
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.success();
|
2018-03-15 12:10:07 +01:00
|
|
|
assert(cancel_fade_out_called);
|
2018-03-15 12:56:31 +01:00
|
|
|
assert(instructions_fade_out_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
assert(saved_fade_to_called);
|
2018-01-10 04:08:16 +01:00
|
|
|
}());
|
2018-03-14 16:45:37 +01:00
|
|
|
};
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
var fake_this = $.create('fake-#user-groups_blur_name');
|
|
|
|
fake_this.closest = function () {
|
|
|
|
return [];
|
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
fake_this.set_parents_result(user_group_selector, $(user_group_selector));
|
2018-03-14 16:45:37 +01:00
|
|
|
var event = {
|
|
|
|
relatedTarget: fake_this,
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
api_endpoint_called = false;
|
|
|
|
handler_name.call(fake_this, event);
|
|
|
|
assert(api_endpoint_called);
|
|
|
|
|
|
|
|
// Check API endpoint isn't called if name and desc haven't changed.
|
|
|
|
group_data.name = "translated: mobile";
|
|
|
|
group_data.description = "translated: All mobile members";
|
|
|
|
api_endpoint_called = false;
|
|
|
|
handler_name.call(fake_this, event);
|
|
|
|
assert(!api_endpoint_called);
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
// Check for handler_desc to achieve 100% coverage.
|
2018-03-14 16:45:37 +01:00
|
|
|
api_endpoint_called = false;
|
|
|
|
handler_desc.call(fake_this, event);
|
|
|
|
assert(!api_endpoint_called);
|
2018-01-10 04:08:16 +01:00
|
|
|
}());
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
(function test_user_groups_save_member_changes_triggered() {
|
2018-03-15 12:10:07 +01:00
|
|
|
var handler = $(user_group_selector).get_on_handler("blur", ".input");
|
2018-01-17 09:13:19 +01:00
|
|
|
var realm_user_group = {
|
|
|
|
id: 1,
|
|
|
|
name: 'Mobile',
|
|
|
|
description: 'All mobile people',
|
2018-03-04 17:44:12 +01:00
|
|
|
members: Dict.from_array([2, 4]),
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2018-03-13 23:51:53 +01:00
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
user_groups.get_user_group_from_id = function (id) {
|
|
|
|
assert.equal(id, 1);
|
|
|
|
return realm_user_group;
|
|
|
|
};
|
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
var cancel_fade_out_called = false;
|
|
|
|
var saved_fade_to_called = false;
|
2018-03-15 12:56:31 +01:00
|
|
|
var instructions_fade_out_called = false;
|
|
|
|
$(instructions_selector).fadeOut = function () {
|
|
|
|
instructions_fade_out_called = true;
|
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
$(cancel_selector).fadeOut = function () {
|
|
|
|
cancel_fade_out_called = true;
|
|
|
|
};
|
|
|
|
$(saved_selector).css = function () {
|
|
|
|
return $(saved_selector);
|
|
|
|
};
|
|
|
|
$(saved_selector).fadeTo = function () {
|
|
|
|
saved_fade_to_called = true;
|
|
|
|
};
|
|
|
|
|
2018-03-13 23:51:53 +01:00
|
|
|
var api_endpoint_called = false;
|
2018-01-17 09:13:19 +01:00
|
|
|
channel.post = function (opts) {
|
|
|
|
assert.equal(opts.url, "/json/user_groups/1/members");
|
|
|
|
assert.equal(opts.data.add, '[31]');
|
|
|
|
assert.equal(opts.data.delete, '[4]');
|
2018-03-13 23:51:53 +01:00
|
|
|
api_endpoint_called = true;
|
2018-03-15 12:10:07 +01:00
|
|
|
|
|
|
|
(function test_post_success() {
|
|
|
|
opts.success();
|
|
|
|
assert(cancel_fade_out_called);
|
2018-03-15 12:56:31 +01:00
|
|
|
assert(instructions_fade_out_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
assert(saved_fade_to_called);
|
|
|
|
}());
|
2018-03-13 23:51:53 +01:00
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
var fake_this = $.create('fake-#user-groups_blur_input');
|
2018-03-15 12:10:07 +01:00
|
|
|
fake_this.set_parents_result(user_group_selector, $(user_group_selector));
|
2018-03-13 23:51:53 +01:00
|
|
|
fake_this.closest = function () {
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
var event = {
|
|
|
|
relatedTarget: fake_this,
|
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2018-03-13 23:51:53 +01:00
|
|
|
api_endpoint_called = false;
|
|
|
|
handler.call(fake_this, event);
|
|
|
|
assert(api_endpoint_called);
|
2018-01-17 09:13:19 +01:00
|
|
|
}());
|
2018-01-10 04:08:16 +01:00
|
|
|
}());
|