2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {$t} = require("./lib/i18n");
|
|
|
|
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
|
|
|
const $ = require("./lib/zjquery");
|
|
|
|
const {page_params} = require("./lib/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const noop = () => {};
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pills = {
|
2018-01-17 09:13:19 +01:00
|
|
|
pill: {},
|
|
|
|
};
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let create_item_handler;
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const channel = mock_esm("../src/channel");
|
|
|
|
const confirm_dialog = mock_esm("../src/confirm_dialog");
|
|
|
|
const dialog_widget = mock_esm("../src/dialog_widget");
|
|
|
|
const input_pill = mock_esm("../src/input_pill");
|
|
|
|
const typeahead_helper = mock_esm("../src/typeahead_helper");
|
|
|
|
const user_groups = mock_esm("../src/user_groups", {
|
2018-01-10 04:08:16 +01:00
|
|
|
get_user_group_from_id: noop,
|
|
|
|
remove: noop,
|
|
|
|
add: noop,
|
2021-03-07 13:57:14 +01:00
|
|
|
});
|
2023-02-22 23:04:10 +01:00
|
|
|
const ui_report = mock_esm("../src/ui_report");
|
2021-02-28 00:58:55 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const people = zrequire("people");
|
2021-06-03 15:47:03 +02:00
|
|
|
const settings_data = zrequire("settings_data");
|
2022-06-20 15:25:04 +02:00
|
|
|
const settings_user_groups_legacy = zrequire("settings_user_groups_legacy");
|
2021-03-07 13:57:14 +01:00
|
|
|
const user_pill = zrequire("user_pill");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function reset_test_setup($pill_container_stub) {
|
2018-03-13 10:46:17 +01:00
|
|
|
function input_pill_stub(opts) {
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal(opts.$container, $pill_container_stub);
|
2018-03-13 10:46:17 +01:00
|
|
|
create_item_handler = opts.create_item_from_text;
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(create_item_handler);
|
2018-03-13 10:46:17 +01:00
|
|
|
return pills;
|
|
|
|
}
|
2021-02-28 18:48:44 +01:00
|
|
|
input_pill.create = input_pill_stub;
|
2018-03-13 10:46:17 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
function test_ui(label, f) {
|
2021-02-23 12:05:28 +01:00
|
|
|
// The sloppy_$ flag lets us re-use setup from prior tests.
|
|
|
|
run_test(label, f, {sloppy_$: true});
|
2021-02-23 12:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
test_ui("can_edit", () => {
|
2021-06-03 15:47:03 +02:00
|
|
|
settings_data.user_can_edit_user_groups = () => false;
|
2022-06-20 15:25:04 +02:00
|
|
|
assert.ok(!settings_user_groups_legacy.can_edit(1));
|
2018-06-14 08:35:05 +02:00
|
|
|
|
2021-06-03 15:47:03 +02:00
|
|
|
settings_data.user_can_edit_user_groups = () => true;
|
2022-05-05 10:04:57 +02:00
|
|
|
user_groups.is_direct_member_of = (user_id, group_id) => {
|
2018-03-13 10:46:17 +01:00
|
|
|
assert.equal(group_id, 1);
|
|
|
|
assert.equal(user_id, undefined);
|
|
|
|
return false;
|
|
|
|
};
|
2022-06-20 15:25:04 +02:00
|
|
|
assert.ok(!settings_user_groups_legacy.can_edit(1));
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
page_params.is_admin = true;
|
2022-06-20 15:25:04 +02:00
|
|
|
assert.ok(settings_user_groups_legacy.can_edit(1));
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
page_params.is_admin = false;
|
2021-06-03 15:47:03 +02:00
|
|
|
page_params.is_moderator = true;
|
2022-06-20 15:25:04 +02:00
|
|
|
assert.ok(settings_user_groups_legacy.can_edit(1));
|
2019-11-02 17:58:55 +01:00
|
|
|
|
|
|
|
page_params.is_admin = false;
|
2021-06-03 15:47:03 +02:00
|
|
|
page_params.is_moderator = false;
|
2022-05-05 10:04:57 +02:00
|
|
|
user_groups.is_direct_member_of = (user_id, group_id) => {
|
2019-11-02 17:58:55 +01:00
|
|
|
assert.equal(group_id, 1);
|
|
|
|
assert.equal(user_id, undefined);
|
|
|
|
return true;
|
|
|
|
};
|
2022-06-20 15:25:04 +02:00
|
|
|
assert.ok(settings_user_groups_legacy.can_edit(1));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2021-02-03 23:23:32 +01:00
|
|
|
const user_group_selector = `#user-groups #${CSS.escape(1)}`;
|
|
|
|
const cancel_selector = `#user-groups #${CSS.escape(1)} .save-status.btn-danger`;
|
|
|
|
const saved_selector = `#user-groups #${CSS.escape(1)} .save-status.sea-green`;
|
|
|
|
const name_selector = `#user-groups #${CSS.escape(1)} .name`;
|
|
|
|
const description_selector = `#user-groups #${CSS.escape(1)} .description`;
|
|
|
|
const instructions_selector = `#user-groups #${CSS.escape(1)} .save-instructions`;
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
test_ui("populate_user_groups", ({mock_template}) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const realm_user_group = {
|
2018-01-17 09:13:19 +01:00
|
|
|
id: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Mobile",
|
|
|
|
description: "All mobile people",
|
2020-01-16 20:12:06 +01:00
|
|
|
members: new Set([2, 4]),
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const iago = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "iago@zulip.com",
|
2018-01-17 09:13:19 +01:00
|
|
|
user_id: 2,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Iago",
|
2018-01-17 09:13:19 +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-01-17 09:13:19 +01:00
|
|
|
user_id: 31,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice",
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "bob@example.com",
|
2018-01-17 09:13:19 +01:00
|
|
|
user_id: 32,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Bob",
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
|
|
|
|
2020-07-29 03:27:36 +02:00
|
|
|
people.add_active_user(iago);
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
people.get_realm_users = () => [iago, alice, bob];
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
user_groups.get_realm_user_groups = () => [realm_user_group];
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let templates_render_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_rendered_temp = $.create("fake_admin_user_group_list_template_rendered");
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("settings/admin_user_group_list.hbs", false, (args) => {
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(args.user_group.id, 1);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(args.user_group.name, "Mobile");
|
|
|
|
assert.equal(args.user_group.description, "All mobile people");
|
2018-01-17 09:13:19 +01:00
|
|
|
templates_render_called = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $fake_rendered_temp;
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_groups_list_append_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$("#user-groups").append = (rendered_temp) => {
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal(rendered_temp, $fake_rendered_temp);
|
2018-01-17 09:13:19 +01:00
|
|
|
user_groups_list_append_called = true;
|
|
|
|
};
|
|
|
|
|
2020-02-05 14:30:59 +01:00
|
|
|
let get_by_user_id_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
people.get_by_user_id = (user_id) => {
|
2018-01-17 09:13:19 +01:00
|
|
|
if (user_id === iago.user_id) {
|
|
|
|
return iago;
|
|
|
|
}
|
|
|
|
assert.equal(user_id, 4);
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "Undefined user in function append_user");
|
2020-02-05 14:30:59 +01:00
|
|
|
get_by_user_id_called = true;
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2021-04-05 12:54:38 +02:00
|
|
|
people.is_known_user = function () {
|
|
|
|
return people.get_by_user_id !== undefined && people.get_by_user_id !== noop;
|
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
page_params.is_admin = true;
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2020-02-09 04:56:27 +01:00
|
|
|
const all_pills = new Map();
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_container_stub = $(`.pill-container[data-group-pills="${CSS.escape(1)}"]`);
|
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-01-17 09:13:19 +01:00
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.items = () => Array.from(all_pills.values());
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let text_cleared;
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.clear_text = () => {
|
2018-03-06 15:03:20 +01:00
|
|
|
text_cleared = true;
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $input_field_stub = $.create("fake-input-field");
|
|
|
|
$pill_container_stub.children = () => $input_field_stub;
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let input_typeahead_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_field_stub.typeahead = (config) => {
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(config.items, 5);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(config.fixed);
|
|
|
|
assert.ok(config.dropup);
|
|
|
|
assert.ok(config.stopAdvance);
|
2020-07-15 01:29:15 +02:00
|
|
|
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");
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
(function test_highlighter() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_person = $.create("fake-person");
|
|
|
|
typeahead_helper.render_person = () => $fake_person;
|
|
|
|
assert.equal(config.highlighter(), $fake_person);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const fake_context = {
|
2020-07-15 01:29:15 +02:00
|
|
|
query: "ali",
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
|
|
|
|
2020-04-06 13:30:40 +02:00
|
|
|
const fake_context_for_email = {
|
2020-07-15 01:29:15 +02:00
|
|
|
query: "am",
|
2020-04-06 13:30:40 +02:00
|
|
|
};
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
(function test_source() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = config.source.call(fake_context, iago);
|
2020-07-02 01:39:34 +02:00
|
|
|
const emails = result.map((user) => user.email).sort();
|
2018-03-06 15:03:20 +01:00
|
|
|
assert.deepEqual(emails, [alice.email, bob.email]);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-03-06 15:03:20 +01:00
|
|
|
|
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. */
|
2019-11-02 00:06:25 +01:00
|
|
|
let result = config.matcher.call(fake_context, iago);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!result);
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
result = config.matcher.call(fake_context, alice);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(result);
|
2020-04-06 13:30:40 +02:00
|
|
|
|
2021-10-26 15:43:39 +02:00
|
|
|
bob.delivery_email = null;
|
2020-04-06 13:30:40 +02:00
|
|
|
result = config.matcher.call(fake_context_for_email, bob);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!result);
|
2020-04-06 13:30:40 +02:00
|
|
|
|
2021-10-26 15:43:39 +02:00
|
|
|
bob.delivery_email = "bob-delivery@example.com";
|
2020-04-06 13:30:40 +02:00
|
|
|
result = config.matcher.call(fake_context_for_email, bob);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(result);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
(function test_sorter() {
|
2020-10-10 14:55:42 +02:00
|
|
|
let sort_recipients_typeahead_called = false;
|
|
|
|
typeahead_helper.sort_recipients = function () {
|
|
|
|
sort_recipients_typeahead_called = true;
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2021-04-05 12:54:38 +02:00
|
|
|
config.sorter.call(fake_context, []);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(sort_recipients_typeahead_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
(function test_updater() {
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_field_stub.text("@ali");
|
2021-02-23 14:37:26 +01:00
|
|
|
user_groups.get_user_group_from_id = () => realm_user_group;
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let saved_fade_out_called = false;
|
|
|
|
let cancel_fade_to_called = false;
|
|
|
|
let instructions_fade_to_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$(saved_selector).fadeOut = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
saved_fade_out_called = true;
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(cancel_selector).css = (data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof data, "object");
|
|
|
|
assert.equal(data.display, "inline-block");
|
|
|
|
assert.equal(data.opacity, "0");
|
2018-03-15 12:10:07 +01:00
|
|
|
return $(cancel_selector);
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(cancel_selector).fadeTo = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
cancel_fade_to_called = true;
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(instructions_selector).css = (data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof data, "object");
|
|
|
|
assert.equal(data.display, "block");
|
|
|
|
assert.equal(data.opacity, "0");
|
2018-03-15 12:56:31 +01:00
|
|
|
return $(instructions_selector);
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(instructions_selector).fadeTo = () => {
|
2018-03-15 12:56:31 +01:00
|
|
|
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.
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(saved_fade_out_called);
|
|
|
|
assert.ok(cancel_fade_to_called);
|
|
|
|
assert.ok(instructions_fade_to_called);
|
2018-03-06 15:03:20 +01:00
|
|
|
assert.equal(text_cleared, true);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-17 09:13:19 +01:00
|
|
|
input_typeahead_called = true;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let get_by_email_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
people.get_by_email = (user_email) => {
|
2018-01-17 09:13:19 +01:00
|
|
|
get_by_email_called = true;
|
2022-04-09 23:44:38 +02:00
|
|
|
switch (user_email) {
|
|
|
|
case iago.email:
|
|
|
|
return iago;
|
|
|
|
case bob.email:
|
|
|
|
return bob;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error("Expected user email to be of Iago or Bob here.");
|
2018-01-17 09:13:19 +01:00
|
|
|
}
|
2018-03-06 15:03:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function test_create_item(handler) {
|
2018-01-17 09:13:19 +01:00
|
|
|
(function test_rejection_path() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = handler(iago.email, pills.items());
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(get_by_email_called);
|
2018-03-06 15:03:20 +01:00
|
|
|
assert.equal(item, undefined);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-17 09:13:19 +01:00
|
|
|
|
|
|
|
(function test_success_path() {
|
|
|
|
get_by_email_called = false;
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = handler(bob.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-03-06 15:03:20 +01:00
|
|
|
assert.equal(res.user_id, bob.user_id);
|
|
|
|
assert.equal(res.display_value, bob.full_name);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2020-07-29 03:27:36 +02:00
|
|
|
|
|
|
|
(function test_deactivated_pill() {
|
|
|
|
people.deactivate(bob);
|
|
|
|
get_by_email_called = false;
|
|
|
|
const res = handler(bob.email, pills.items());
|
|
|
|
assert.ok(get_by_email_called);
|
|
|
|
assert.equal(typeof res, "object");
|
|
|
|
assert.equal(res.user_id, bob.user_id);
|
|
|
|
assert.equal(res.display_value, bob.full_name + " (deactivated)");
|
|
|
|
assert.ok(res.deactivated);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
})();
|
2018-03-06 15:03:20 +01:00
|
|
|
}
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
pills.onPillRemove = (handler) => {
|
2020-12-01 00:02:16 +01:00
|
|
|
set_global("setTimeout", (func) => {
|
2018-03-13 23:51:53 +01:00
|
|
|
func();
|
|
|
|
});
|
2020-01-16 20:12:06 +01:00
|
|
|
realm_user_group.members = new Set([2, 31]);
|
2018-01-17 09:13:19 +01:00
|
|
|
handler();
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
reset_test_setup($pill_container_stub);
|
2022-06-20 15:25:04 +02:00
|
|
|
settings_user_groups_legacy.set_up();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(templates_render_called);
|
|
|
|
assert.ok(user_groups_list_append_called);
|
|
|
|
assert.ok(get_by_user_id_called);
|
|
|
|
assert.ok(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
|
|
|
|
2022-06-20 15:25:04 +02:00
|
|
|
// Tests for settings_user_groups_legacy.set_up workflow.
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof $("#user-groups").get_on_handler("click", ".delete"), "function");
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
typeof $("#user-groups").get_on_handler("keypress", ".user-group h4 > span"),
|
|
|
|
"function",
|
|
|
|
);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2022-01-08 10:27:06 +01:00
|
|
|
test_ui("with_external_user", ({override_rewire, mock_template}) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const realm_user_group = {
|
2018-03-13 10:46:17 +01:00
|
|
|
id: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Mobile",
|
|
|
|
description: "All mobile people",
|
2020-01-16 20:12:06 +01:00
|
|
|
members: new Set([2, 4]),
|
2018-03-13 10:46:17 +01:00
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
user_groups.get_realm_user_groups = () => [realm_user_group];
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
// We return [] because these are already tested, so we skip them
|
|
|
|
/* istanbul ignore next */
|
|
|
|
people.get_realm_users = () => [];
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template(
|
|
|
|
"settings/admin_user_group_list.hbs",
|
|
|
|
false,
|
|
|
|
() => "settings/admin_user_group_list.hbs",
|
|
|
|
);
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
people.get_by_user_id = () => "user stub";
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
override_rewire(user_pill, "append_person", noop);
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let can_edit_called = 0;
|
2022-06-20 15:25:04 +02:00
|
|
|
override_rewire(settings_user_groups_legacy, "can_edit", () => {
|
2018-03-13 10:46:17 +01:00
|
|
|
can_edit_called += 1;
|
|
|
|
return false;
|
2021-02-28 01:22:43 +01:00
|
|
|
});
|
2018-03-13 10:46:17 +01:00
|
|
|
|
|
|
|
// Reset zjquery to test stuff with user who cannot edit
|
2021-02-21 15:01:54 +01:00
|
|
|
$.clear_all_elements();
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let user_group_find_called = 0;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $user_group_stub = $(`div.user-group[id="${CSS.escape(1)}"]`);
|
|
|
|
const $name_field_stub = $.create("fake-name-field");
|
|
|
|
const $description_field_stub = $.create("fake-description-field");
|
|
|
|
const $input_stub = $.create("fake-input");
|
|
|
|
$user_group_stub.find = (elem) => {
|
2022-04-09 23:44:38 +02:00
|
|
|
user_group_find_called += 1;
|
|
|
|
switch (elem) {
|
|
|
|
case ".name":
|
|
|
|
return $name_field_stub;
|
|
|
|
case ".description":
|
|
|
|
return $description_field_stub;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown element ${elem}`);
|
2018-03-13 10:46:17 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_container_stub = $(`.pill-container[data-group-pills="${CSS.escape(1)}"]`);
|
|
|
|
const $pill_stub = $.create("fake-pill");
|
2019-11-02 00:06:25 +01:00
|
|
|
let pill_container_find_called = 0;
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_container_stub.find = (elem) => {
|
2022-04-09 23:44:38 +02:00
|
|
|
pill_container_find_called += 1;
|
|
|
|
switch (elem) {
|
|
|
|
case ".input":
|
|
|
|
return $input_stub;
|
|
|
|
case ".pill":
|
|
|
|
return $pill_stub;
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown element ${elem}`);
|
2018-03-13 10:46:17 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$input_stub.css = (property, val) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(property, "display");
|
|
|
|
assert.equal(val, "none");
|
2018-03-13 10:46:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Test the 'off' handlers on the pill-container
|
2019-11-02 00:06:25 +01:00
|
|
|
const turned_off = {};
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_container_stub.off = (event_name, sel = "whole") => {
|
2020-07-15 01:29:15 +02:00
|
|
|
turned_off[event_name + "/" + sel] = true;
|
2018-03-13 10:46:17 +01:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $exit_button = $.create("fake-pill-exit");
|
|
|
|
$pill_stub.set_find_results(".exit", $exit_button);
|
2019-11-02 00:06:25 +01:00
|
|
|
let exit_button_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$exit_button.css = (property, value) => {
|
2018-03-13 10:46:17 +01:00
|
|
|
exit_button_called = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(property, "opacity");
|
|
|
|
assert.equal(value, "0.5");
|
2018-03-13 10:46:17 +01:00
|
|
|
};
|
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
// We return [] because these are already tested, so we skip them
|
|
|
|
$pill_container_stub.children = () => [];
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2022-04-09 23:44:38 +02:00
|
|
|
$("#user-groups").append = noop;
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
reset_test_setup($pill_container_stub);
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2022-06-20 15:25:04 +02:00
|
|
|
settings_user_groups_legacy.set_up();
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let set_parents_result_called = 0;
|
|
|
|
let set_attributes_called = 0;
|
2018-03-13 10:46:17 +01:00
|
|
|
|
|
|
|
// Test different handlers with an external user
|
2020-07-15 01:29:15 +02:00
|
|
|
const delete_handler = $("#user-groups").get_on_handler("click", ".delete");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_delete = $.create("fk-#user-groups.delete_btn");
|
|
|
|
$fake_delete.set_parents_result(".user-group", $(".user-group"));
|
2018-03-13 10:46:17 +01:00
|
|
|
set_parents_result_called += 1;
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".user-group").attr("id", "1");
|
2018-03-13 10:46:17 +01:00
|
|
|
set_attributes_called += 1;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const name_update_handler = $(user_group_selector).get_on_handler("input", ".name");
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const des_update_handler = $(user_group_selector).get_on_handler("input", ".description");
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const member_change_handler = $(user_group_selector).get_on_handler("blur", ".input");
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const name_change_handler = $(user_group_selector).get_on_handler("blur", ".name");
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const des_change_handler = $(user_group_selector).get_on_handler("blur", ".description");
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2018-03-13 10:46:17 +01:00
|
|
|
stopPropagation: noop,
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
const pill_mouseenter_handler = $pill_stub.get_on_handler("mouseenter");
|
|
|
|
const pill_click_handler = $pill_container_stub.get_on_handler("click");
|
2020-07-20 21:55:26 +02:00
|
|
|
pill_mouseenter_handler(event);
|
2018-03-13 10:46:17 +01:00
|
|
|
pill_click_handler(event);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal(delete_handler.call($fake_delete), undefined);
|
2018-03-13 10:46:17 +01:00
|
|
|
assert.equal(name_update_handler(), undefined);
|
|
|
|
assert.equal(des_update_handler(), undefined);
|
|
|
|
assert.equal(member_change_handler(), undefined);
|
|
|
|
assert.equal(name_change_handler(), undefined);
|
|
|
|
assert.equal(des_change_handler(), undefined);
|
|
|
|
assert.equal(set_parents_result_called, 1);
|
|
|
|
assert.equal(set_attributes_called, 1);
|
|
|
|
assert.equal(can_edit_called, 9);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(exit_button_called);
|
2018-03-13 10:46:17 +01:00
|
|
|
assert.equal(user_group_find_called, 2);
|
|
|
|
assert.equal(pill_container_find_called, 4);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(turned_off["keydown/.pill"], true);
|
|
|
|
assert.equal(turned_off["keydown/.input"], true);
|
|
|
|
assert.equal(turned_off["click/whole"], true);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test_ui("reload", ({override_rewire}) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#user-groups").html("Some text");
|
2019-11-02 00:06:25 +01:00
|
|
|
let populate_user_groups_called = false;
|
2022-06-20 15:25:04 +02:00
|
|
|
override_rewire(settings_user_groups_legacy, "populate_user_groups", () => {
|
2018-01-10 04:08:16 +01:00
|
|
|
populate_user_groups_called = true;
|
2021-02-28 01:22:43 +01:00
|
|
|
});
|
2022-06-20 15:25:04 +02:00
|
|
|
settings_user_groups_legacy.reload();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(populate_user_groups_called);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#user-groups").html(), "");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2021-02-23 12:09:35 +01:00
|
|
|
test_ui("reset", () => {
|
2022-06-20 15:25:04 +02:00
|
|
|
settings_user_groups_legacy.reset();
|
|
|
|
const result = settings_user_groups_legacy.reload();
|
2018-03-04 17:31:22 +01:00
|
|
|
assert.equal(result, undefined);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-04 17:31:22 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test_ui("on_events", ({override_rewire, mock_template}) => {
|
2021-06-28 00:41:05 +02:00
|
|
|
mock_template("confirm_dialog/confirm_delete_user.hbs", false, (data) => {
|
2021-06-27 15:51:56 +02:00
|
|
|
assert.deepEqual(data, {
|
|
|
|
group_name: "Mobile",
|
|
|
|
});
|
|
|
|
return "stub";
|
|
|
|
});
|
|
|
|
|
2022-07-10 01:06:33 +02:00
|
|
|
page_params.is_admin = true;
|
2018-03-13 10:46:17 +01:00
|
|
|
|
2018-01-10 04:08:16 +01:00
|
|
|
(function test_admin_user_group_form_submit_triggered() {
|
2022-06-20 15:25:04 +02:00
|
|
|
const handler = settings_user_groups_legacy.add_user_group;
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2018-01-10 04:08:16 +01:00
|
|
|
stopPropagation: noop,
|
|
|
|
preventDefault: noop,
|
|
|
|
};
|
2022-03-30 08:26:33 +02:00
|
|
|
const $fake_this = $.create("#add-user-group-form");
|
2020-07-15 00:34:28 +02:00
|
|
|
const fake_object_array = [
|
|
|
|
{
|
|
|
|
name: "fake-name",
|
|
|
|
value: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "fake-name",
|
|
|
|
value: "fake-value",
|
|
|
|
},
|
|
|
|
];
|
2022-01-25 11:36:19 +01:00
|
|
|
$fake_this.serializeArray = () => fake_object_array;
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
members: "[null]",
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
|
|
|
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() {
|
2022-03-30 08:26:33 +02:00
|
|
|
$("#dialog_error").show();
|
|
|
|
$("#add-user-group-form input[type='text']").val("fake-content");
|
2021-02-23 14:37:26 +01:00
|
|
|
ui_report.success = (text, ele) => {
|
2021-04-13 05:18:25 +02:00
|
|
|
assert.equal(text, "translated HTML: User group added!");
|
2022-03-30 08:26:33 +02:00
|
|
|
assert.equal(ele, $("#dialog_error"));
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
2022-03-30 08:26:33 +02:00
|
|
|
dialog_widget.close_modal = () => {};
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.success();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2022-03-30 08:26:33 +02:00
|
|
|
assert.ok(!$("#dialog_error").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
(function test_post_error() {
|
2022-03-30 08:26:33 +02:00
|
|
|
$("#dialog_error").show();
|
2021-02-23 14:37:26 +01:00
|
|
|
ui_report.error = (error_msg, error_obj, ele) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const xhr = {
|
2018-01-10 04:08:16 +01:00
|
|
|
responseText: '{"msg":"fake-msg"}',
|
|
|
|
};
|
2021-04-13 05:18:25 +02:00
|
|
|
assert.equal(error_msg, "translated HTML: Failed");
|
2018-01-10 04:08:16 +01:00
|
|
|
assert.deepEqual(error_obj, xhr);
|
2022-03-30 08:26:33 +02:00
|
|
|
assert.equal(ele, $("#dialog_error"));
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const xhr = {
|
2018-01-10 04:08:16 +01:00
|
|
|
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
|
|
|
|
2022-03-30 08:26:33 +02:00
|
|
|
assert.ok(!$("#dialog_error").visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
|
|
|
|
2022-03-30 08:26:33 +02:00
|
|
|
handler(event);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
(function test_user_groups_delete_click_triggered() {
|
2020-07-15 01:29:15 +02:00
|
|
|
const handler = $("#user-groups").get_on_handler("click", ".delete");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_this = $.create("fake-#user-groups.delete_btn");
|
|
|
|
$fake_this.set_parents_result(".user-group", $(".user-group"));
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".user-group").attr("id", "1");
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.del = (opts) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2018-01-10 04:08:16 +01:00
|
|
|
id: 1,
|
|
|
|
};
|
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
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$fake_this.text($t({defaultMessage: "fake-text"}));
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.error();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($fake_this.text(), "translated: Failed!");
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
|
|
|
|
2018-09-12 17:43:09 +02:00
|
|
|
confirm_dialog.launch = (conf) => {
|
|
|
|
conf.on_click();
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
handler.call($fake_this);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
|
|
|
(function test_user_groups_keypress_enter_triggered() {
|
2020-07-15 01:29:15 +02:00
|
|
|
const handler = $("#user-groups").get_on_handler("keypress", ".user-group h4 > span");
|
2019-11-02 00:06:25 +01:00
|
|
|
let default_action_for_enter_stopped = false;
|
|
|
|
const event = {
|
2021-05-31 19:15:01 +02:00
|
|
|
key: "Enter",
|
2020-07-20 22:18:43 +02:00
|
|
|
preventDefault() {
|
2018-01-10 04:08:16 +01:00
|
|
|
default_action_for_enter_stopped = true;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
handler(event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(default_action_for_enter_stopped);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
(function test_do_not_blur() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const blur_event_classes = [".name", ".description", ".input"];
|
|
|
|
let api_endpoint_called = false;
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = () => {
|
2018-03-14 16:45:37 +01:00
|
|
|
api_endpoint_called = true;
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
2018-03-15 12:10:07 +01:00
|
|
|
channel.patch = noop;
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_this = $.create("fake-#user-groups_do_not_blur");
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2022-01-25 11:36:19 +01:00
|
|
|
// FIXME: event.relatedTarget should not be a jQuery object
|
|
|
|
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.
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
for (const class_name of blur_event_classes) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const handler = $(user_group_selector).get_on_handler("blur", class_name);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
2020-02-26 09:27:28 +01:00
|
|
|
for (const blur_exception of [
|
|
|
|
".pill-container",
|
|
|
|
".name",
|
|
|
|
".description",
|
|
|
|
".input",
|
|
|
|
".delete",
|
|
|
|
]) {
|
|
|
|
if (blur_exception === class_name) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-14 16:45:37 +01:00
|
|
|
api_endpoint_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$fake_this.closest = (class_name) => {
|
2018-03-15 12:10:07 +01:00
|
|
|
if (class_name === blur_exception || class_name === user_group_selector) {
|
2018-03-14 16:45:37 +01:00
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
handler.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!api_endpoint_called);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-03-14 16:45:37 +01:00
|
|
|
|
|
|
|
api_endpoint_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$fake_this.closest = (class_name) => {
|
2022-04-09 23:44:38 +02:00
|
|
|
assert.equal(class_name, ".typeahead");
|
|
|
|
return [1];
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
handler.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!api_endpoint_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
|
|
|
|
// Cancel button triggers blur event.
|
2022-06-20 15:25:04 +02:00
|
|
|
let settings_user_groups_legacy_reload_called = false;
|
|
|
|
override_rewire(settings_user_groups_legacy, "reload", () => {
|
|
|
|
settings_user_groups_legacy_reload_called = true;
|
2021-02-28 01:22:43 +01:00
|
|
|
});
|
2018-03-15 12:10:07 +01:00
|
|
|
api_endpoint_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$fake_this.closest = (class_name) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
class_name === ".save-status.btn-danger" ||
|
|
|
|
class_name === user_group_selector
|
|
|
|
) {
|
2018-03-15 12:10:07 +01:00
|
|
|
return [1];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
handler.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!api_endpoint_called);
|
2022-06-20 15:25:04 +02:00
|
|
|
assert.ok(settings_user_groups_legacy_reload_called);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2018-03-15 12:10:07 +01:00
|
|
|
(function test_update_cancel_button() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const handler_name = $(user_group_selector).get_on_handler("input", ".name");
|
|
|
|
const handler_desc = $(user_group_selector).get_on_handler("input", ".description");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $sib_des = $(description_selector);
|
|
|
|
const $sib_name = $(name_selector);
|
|
|
|
$sib_name.text($t({defaultMessage: "mobile"}));
|
|
|
|
$sib_des.text($t({defaultMessage: "All mobile members"}));
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const group_data = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "translated: mobile",
|
|
|
|
description: "translated: All mobile members",
|
2020-07-15 00:34:28 +02:00
|
|
|
members: new Set([2, 31]),
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
user_groups.get_user_group_from_id = () => group_data;
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let cancel_fade_out_called = false;
|
|
|
|
let instructions_fade_out_called = false;
|
2018-03-15 12:10:07 +01:00
|
|
|
$(cancel_selector).show();
|
2021-02-23 14:37:26 +01:00
|
|
|
$(cancel_selector).fadeOut = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
cancel_fade_out_called = true;
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(instructions_selector).fadeOut = () => {
|
2018-03-15 12:56:31 +01:00
|
|
|
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.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_this = $.create("fake-#update_cancel_button");
|
|
|
|
handler_name.call($fake_this);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cancel_fade_out_called);
|
|
|
|
assert.ok(instructions_fade_out_called);
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2018-08-25 00:32:13 +02:00
|
|
|
// Check if cancel button removed if user group error is showing.
|
2020-07-15 01:29:15 +02:00
|
|
|
$(user_group_selector + " .user-group-status").show();
|
2018-08-25 00:32:13 +02:00
|
|
|
cancel_fade_out_called = false;
|
|
|
|
instructions_fade_out_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
handler_name.call($fake_this);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cancel_fade_out_called);
|
|
|
|
assert.ok(instructions_fade_out_called);
|
2018-08-25 00:32:13 +02:00
|
|
|
|
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;
|
2022-01-25 11:36:19 +01:00
|
|
|
handler_desc.call($fake_this);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cancel_fade_out_called);
|
|
|
|
assert.ok(instructions_fade_out_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-03-15 12:10:07 +01:00
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
(function test_user_groups_save_group_changes_triggered() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const handler_name = $(user_group_selector).get_on_handler("blur", ".name");
|
|
|
|
const handler_desc = $(user_group_selector).get_on_handler("blur", ".description");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $sib_des = $(description_selector);
|
|
|
|
const $sib_name = $(name_selector);
|
|
|
|
$sib_name.text($t({defaultMessage: "mobile"}));
|
|
|
|
$sib_des.text($t({defaultMessage: "All mobile members"}));
|
2018-03-14 16:45:37 +01:00
|
|
|
|
2020-01-16 20:12:06 +01:00
|
|
|
const group_data = {members: new Set([2, 31])};
|
2021-02-23 14:37:26 +01:00
|
|
|
user_groups.get_user_group_from_id = () => group_data;
|
2019-11-02 00:06:25 +01:00
|
|
|
let api_endpoint_called = false;
|
|
|
|
let cancel_fade_out_called = false;
|
|
|
|
let saved_fade_to_called = false;
|
|
|
|
let instructions_fade_out_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$(instructions_selector).fadeOut = () => {
|
2018-03-15 12:56:31 +01:00
|
|
|
instructions_fade_out_called = true;
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(cancel_selector).fadeOut = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
cancel_fade_out_called = true;
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(saved_selector).css = (data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(typeof data, "object");
|
|
|
|
assert.equal(data.display, "inline-block");
|
|
|
|
assert.equal(data.opacity, "0");
|
2018-03-15 12:10:07 +01:00
|
|
|
return $(saved_selector);
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(saved_selector).fadeTo = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
saved_fade_to_called = true;
|
2018-08-22 00:31:48 +02:00
|
|
|
return $(saved_selector);
|
2018-03-15 12:10:07 +01:00
|
|
|
};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.patch = (opts) => {
|
2018-03-14 16:45:37 +01:00
|
|
|
assert.equal(opts.url, "/json/user_groups/1");
|
2020-07-15 01:29:15 +02: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() {
|
2020-12-01 00:02:16 +01:00
|
|
|
set_global("setTimeout", (func) => {
|
2018-03-15 12:10:07 +01:00
|
|
|
func();
|
|
|
|
});
|
2018-01-17 09:13:19 +01:00
|
|
|
opts.success();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cancel_fade_out_called);
|
|
|
|
assert.ok(instructions_fade_out_called);
|
|
|
|
assert.ok(saved_fade_to_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-08-25 00:32:13 +02:00
|
|
|
(function test_post_error() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $user_group_error = $(user_group_selector + " .user-group-status");
|
|
|
|
$user_group_error.show();
|
2021-02-23 14:37:26 +01:00
|
|
|
ui_report.error = (error_msg, error_obj, ele) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const xhr = {
|
2018-08-25 00:32:13 +02:00
|
|
|
responseText: '{"msg":"fake-msg"}',
|
|
|
|
};
|
2021-04-13 05:18:25 +02:00
|
|
|
assert.equal(error_msg, "translated HTML: Failed");
|
2018-08-25 00:32:13 +02:00
|
|
|
assert.deepEqual(error_obj, xhr);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal(ele, $user_group_error);
|
2018-08-25 00:32:13 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const xhr = {
|
2018-08-25 00:32:13 +02:00
|
|
|
responseText: '{"msg":"fake-msg", "attrib":"val"}',
|
|
|
|
};
|
|
|
|
opts.error(xhr);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($user_group_error.visible());
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-03-14 16:45:37 +01:00
|
|
|
};
|
2018-01-10 04:08:16 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_this = $.create("fake-#user-groups_blur_name");
|
|
|
|
$fake_this.closest = () => [];
|
|
|
|
$fake_this.set_parents_result(user_group_selector, $(user_group_selector));
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2022-01-25 11:36:19 +01:00
|
|
|
// FIXME: event.relatedTarget should not be a jQuery object
|
|
|
|
relatedTarget: $fake_this,
|
2018-01-10 04:08:16 +01:00
|
|
|
};
|
|
|
|
|
2018-03-14 16:45:37 +01:00
|
|
|
api_endpoint_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
handler_name.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(api_endpoint_called);
|
2018-03-14 16:45:37 +01:00
|
|
|
|
|
|
|
// 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;
|
2022-01-25 11:36:19 +01:00
|
|
|
handler_name.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!api_endpoint_called);
|
2018-03-14 16:45:37 +01:00
|
|
|
|
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;
|
2022-01-25 11:36:19 +01:00
|
|
|
handler_desc.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!api_endpoint_called);
|
2020-07-16 22:35:58 +02: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() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const handler = $(user_group_selector).get_on_handler("blur", ".input");
|
|
|
|
const realm_user_group = {
|
2018-01-17 09:13:19 +01:00
|
|
|
id: 1,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Mobile",
|
|
|
|
description: "All mobile people",
|
2020-01-16 20:12:06 +01:00
|
|
|
members: new Set([2, 4]),
|
2018-01-17 09:13:19 +01:00
|
|
|
};
|
2018-03-13 23:51:53 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
user_groups.get_user_group_from_id = (id) => {
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(id, 1);
|
|
|
|
return realm_user_group;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let cancel_fade_out_called = false;
|
|
|
|
let saved_fade_to_called = false;
|
|
|
|
let instructions_fade_out_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
$(instructions_selector).fadeOut = () => {
|
2018-03-15 12:56:31 +01:00
|
|
|
instructions_fade_out_called = true;
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(cancel_selector).fadeOut = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
cancel_fade_out_called = true;
|
|
|
|
};
|
2021-02-23 14:37:26 +01:00
|
|
|
$(saved_selector).css = () => $(saved_selector);
|
|
|
|
$(saved_selector).fadeTo = () => {
|
2018-03-15 12:10:07 +01:00
|
|
|
saved_fade_to_called = true;
|
2018-08-22 00:31:48 +02:00
|
|
|
return $(saved_selector);
|
2018-03-15 12:10:07 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let api_endpoint_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
channel.post = (opts) => {
|
2018-01-17 09:13:19 +01:00
|
|
|
assert.equal(opts.url, "/json/user_groups/1/members");
|
2020-07-15 01:29:15 +02:00
|
|
|
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();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cancel_fade_out_called);
|
|
|
|
assert.ok(instructions_fade_out_called);
|
|
|
|
assert.ok(saved_fade_to_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-03-13 23:51:53 +01:00
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $fake_this = $.create("fake-#user-groups_blur_input");
|
|
|
|
$fake_this.set_parents_result(user_group_selector, $(user_group_selector));
|
|
|
|
$fake_this.closest = () => [];
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2022-01-25 11:36:19 +01:00
|
|
|
// FIXME: event.relatedTarget should not be a jQuery object
|
|
|
|
relatedTarget: $fake_this,
|
2018-03-13 23:51:53 +01:00
|
|
|
};
|
2018-01-17 09:13:19 +01:00
|
|
|
|
2018-03-13 23:51:53 +01:00
|
|
|
api_endpoint_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
handler.call($fake_this, event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(api_endpoint_called);
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|