2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
const {stub_templates} = require("../zjsunit/handlebars");
|
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
|
|
|
const {rewiremock, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-23 03:54:07 +01:00
|
|
|
const page_params = set_global("page_params", {});
|
2021-02-28 00:36:14 +01:00
|
|
|
const loading = {__esModule: true};
|
|
|
|
|
|
|
|
rewiremock("../../static/js/loading").with(loading);
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2018-08-15 11:35:18 +02:00
|
|
|
const SHORT_TEXT_ID = 1;
|
|
|
|
const CHOICE_ID = 3;
|
2019-05-27 10:59:55 +02:00
|
|
|
const EXTERNAL_ACCOUNT_ID = 7;
|
|
|
|
|
|
|
|
const SHORT_TEXT_NAME = "Short Text";
|
|
|
|
const CHOICE_NAME = "Choice";
|
|
|
|
const EXTERNAL_ACCOUNT_NAME = "External account";
|
2018-08-15 11:35:18 +02:00
|
|
|
|
2018-08-15 13:37:40 +02:00
|
|
|
page_params.custom_profile_fields = {};
|
2019-05-27 10:59:55 +02:00
|
|
|
page_params.realm_default_external_accounts = JSON.stringify({});
|
2018-08-15 13:37:40 +02:00
|
|
|
|
2018-08-15 11:35:18 +02:00
|
|
|
page_params.custom_profile_field_types = {
|
|
|
|
SHORT_TEXT: {
|
|
|
|
id: SHORT_TEXT_ID,
|
2019-05-27 10:59:55 +02:00
|
|
|
name: SHORT_TEXT_NAME,
|
2018-08-15 11:35:18 +02:00
|
|
|
},
|
|
|
|
CHOICE: {
|
|
|
|
id: CHOICE_ID,
|
2019-05-27 10:59:55 +02:00
|
|
|
name: CHOICE_NAME,
|
|
|
|
},
|
|
|
|
EXTERNAL_ACCOUNT: {
|
|
|
|
id: EXTERNAL_ACCOUNT_ID,
|
|
|
|
name: EXTERNAL_ACCOUNT_NAME,
|
2018-08-15 11:35:18 +02:00
|
|
|
},
|
|
|
|
};
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2021-02-24 22:31:50 +01:00
|
|
|
const settings_profile_fields = zrequire("settings_profile_fields");
|
2018-08-15 13:17:15 +02:00
|
|
|
|
2018-06-11 22:32:11 +02:00
|
|
|
function test_populate(opts) {
|
|
|
|
const fields_data = opts.fields_data;
|
|
|
|
|
|
|
|
page_params.is_admin = opts.is_admin;
|
2020-07-15 01:29:15 +02:00
|
|
|
const table = $("#admin_profile_fields_table");
|
|
|
|
const rows = $.create("rows");
|
|
|
|
const form = $.create("forms");
|
|
|
|
table.set_find_results("tr.profile-field-row", rows);
|
|
|
|
table.set_find_results("tr.profile-field-form", form);
|
2018-06-11 22:32:11 +02:00
|
|
|
|
2021-02-22 17:01:29 +01:00
|
|
|
table[0] = "stub";
|
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
rows.remove = () => {};
|
|
|
|
form.remove = () => {};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let num_appends = 0;
|
2018-06-11 22:32:11 +02:00
|
|
|
table.append = () => {
|
|
|
|
num_appends += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
loading.destroy_indicator = () => {};
|
|
|
|
|
|
|
|
const template_data = [];
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((fn, data) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(fn, "admin_profile_field_list");
|
2018-06-11 22:32:11 +02:00
|
|
|
template_data.push(data);
|
2020-07-15 01:29:15 +02:00
|
|
|
return "whatever";
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-06-11 22:32:11 +02:00
|
|
|
|
|
|
|
settings_profile_fields.do_populate_profile_fields(fields_data);
|
|
|
|
|
|
|
|
assert.deepEqual(template_data, opts.expected_template_data);
|
|
|
|
assert.equal(num_appends, fields_data.length);
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:44:32 +01:00
|
|
|
run_test("populate_profile_fields", (override) => {
|
|
|
|
override(settings_profile_fields, "create_sortable", () => {});
|
2018-06-11 22:32:11 +02:00
|
|
|
const fields_data = [
|
|
|
|
{
|
2018-08-15 11:35:18 +02:00
|
|
|
type: SHORT_TEXT_ID,
|
2018-06-11 22:32:11 +02:00
|
|
|
id: 10,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "favorite color",
|
|
|
|
hint: "blue?",
|
|
|
|
field_data: "",
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
{
|
2018-08-15 11:35:18 +02:00
|
|
|
type: CHOICE_ID,
|
2018-06-11 22:32:11 +02:00
|
|
|
id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "meal",
|
|
|
|
hint: "lunch",
|
2018-06-11 22:32:11 +02:00
|
|
|
field_data: JSON.stringify([
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
text: "lunch",
|
2018-06-11 22:32:11 +02:00
|
|
|
order: 0,
|
|
|
|
},
|
|
|
|
{
|
2020-07-15 01:29:15 +02:00
|
|
|
text: "dinner",
|
2018-06-11 22:32:11 +02:00
|
|
|
order: 1,
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
},
|
2019-05-27 10:59:55 +02:00
|
|
|
{
|
|
|
|
type: EXTERNAL_ACCOUNT_ID,
|
|
|
|
id: 20,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "github profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
field_data: JSON.stringify({
|
2020-07-15 01:29:15 +02:00
|
|
|
subtype: "github",
|
2019-05-27 10:59:55 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: EXTERNAL_ACCOUNT_ID,
|
|
|
|
id: 21,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "zulip profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
field_data: JSON.stringify({
|
2020-07-15 01:29:15 +02:00
|
|
|
subtype: "custom",
|
|
|
|
url_pattern: "https://chat.zulip.com/%(username)s",
|
2019-05-27 10:59:55 +02:00
|
|
|
}),
|
|
|
|
},
|
2018-06-11 22:32:11 +02:00
|
|
|
];
|
|
|
|
const expected_template_data = [
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 10,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "favorite color",
|
|
|
|
hint: "blue?",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: SHORT_TEXT_NAME,
|
2018-06-11 22:32:11 +02:00
|
|
|
choices: [],
|
|
|
|
is_choice_field: false,
|
2019-05-27 10:59:55 +02:00
|
|
|
is_external_account_field: false,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
can_modify: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_default_external_accounts: page_params.realm_default_external_accounts,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "meal",
|
|
|
|
hint: "lunch",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: CHOICE_NAME,
|
2018-06-11 22:32:11 +02:00
|
|
|
choices: [
|
2020-07-15 01:29:15 +02:00
|
|
|
{order: 0, value: "0", text: "lunch"},
|
|
|
|
{order: 1, value: "1", text: "dinner"},
|
2018-06-11 22:32:11 +02:00
|
|
|
],
|
|
|
|
is_choice_field: true,
|
2019-05-27 10:59:55 +02:00
|
|
|
is_external_account_field: false,
|
|
|
|
},
|
|
|
|
can_modify: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_default_external_accounts: page_params.realm_default_external_accounts,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 20,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "github profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: EXTERNAL_ACCOUNT_NAME,
|
|
|
|
choices: [],
|
|
|
|
is_choice_field: false,
|
|
|
|
is_external_account_field: true,
|
|
|
|
},
|
|
|
|
can_modify: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_default_external_accounts: page_params.realm_default_external_accounts,
|
2019-05-27 10:59:55 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
profile_field: {
|
|
|
|
id: 21,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "zulip profile",
|
|
|
|
hint: "username only",
|
2019-05-27 10:59:55 +02:00
|
|
|
type: EXTERNAL_ACCOUNT_NAME,
|
|
|
|
choices: [],
|
|
|
|
is_choice_field: false,
|
|
|
|
is_external_account_field: true,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
can_modify: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
realm_default_external_accounts: page_params.realm_default_external_accounts,
|
2018-06-11 22:32:11 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
test_populate({
|
2020-07-20 22:18:43 +02:00
|
|
|
fields_data,
|
|
|
|
expected_template_data,
|
2018-06-11 22:32:11 +02:00
|
|
|
is_admin: true,
|
|
|
|
});
|
|
|
|
});
|