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, zrequire, set_global} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-05-16 08:23:32 +02:00
|
|
|
const noop = () => {};
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/list_widget", {
|
2020-07-16 22:40:18 +02:00
|
|
|
create: () => ({init: noop}),
|
2021-03-07 13:57:14 +01:00
|
|
|
});
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2021-07-27 14:38:39 +02:00
|
|
|
mock_esm("tippy.js", {
|
2022-11-17 23:33:43 +01:00
|
|
|
default(arg) {
|
2021-07-27 14:38:39 +02:00
|
|
|
arg._tippy = {setContent: noop, placement: noop, destroy: noop};
|
|
|
|
return arg._tippy;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
set_global("document", {});
|
2021-07-27 14:35:58 +02:00
|
|
|
const {DropdownListWidget, MultiSelectDropdownListWidget} = zrequire("dropdown_list_widget");
|
|
|
|
|
|
|
|
// For DropdownListWidget
|
|
|
|
const setup_dropdown_zjquery_data = (name) => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $input_group = $(".input_group");
|
|
|
|
const $reset_button = $(".dropdown_list_reset_button");
|
|
|
|
$input_group.set_find_results(".dropdown_list_reset_button", $reset_button);
|
|
|
|
$(`#${CSS.escape(name)}_widget #${CSS.escape(name)}_name`).closest = () => $input_group;
|
2021-02-03 23:23:32 +01:00
|
|
|
const $widget = $(`#${CSS.escape(name)}_widget #${CSS.escape(name)}_name`);
|
2022-01-25 11:36:19 +01:00
|
|
|
return {$reset_button, $widget};
|
2020-05-19 00:16:34 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basic_functions", () => {
|
2020-05-16 08:23:32 +02:00
|
|
|
let updated_value;
|
|
|
|
const opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
widget_name: "my_setting",
|
|
|
|
data: ["one", "two", "three"].map((x) => ({name: x, value: x})),
|
|
|
|
value: "one",
|
2022-11-17 23:33:43 +01:00
|
|
|
on_update(val) {
|
2020-07-15 00:34:28 +02:00
|
|
|
updated_value = val;
|
|
|
|
},
|
2021-04-13 06:51:54 +02:00
|
|
|
default_text: $t({defaultMessage: "not set"}),
|
2020-05-16 08:23:32 +02:00
|
|
|
render_text: (text) => `rendered: ${text}`,
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const {$reset_button, $widget} = setup_dropdown_zjquery_data(opts.widget_name);
|
2020-05-16 08:23:32 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
const widget = new DropdownListWidget(opts);
|
2022-04-28 00:12:57 +02:00
|
|
|
widget.setup();
|
2020-05-16 08:23:32 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(widget.value(), "one");
|
2020-07-16 23:29:01 +02:00
|
|
|
assert.equal(updated_value, undefined); // We haven't 'updated' the widget yet.
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($reset_button.visible());
|
2020-05-16 08:23:32 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.update("two");
|
|
|
|
assert.equal($widget.text(), "rendered: two");
|
|
|
|
assert.equal(widget.value(), "two");
|
|
|
|
assert.equal(updated_value, "two");
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($reset_button.visible());
|
2020-05-16 08:23:32 +02:00
|
|
|
|
|
|
|
widget.update(null);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($widget.text(), "translated: not set");
|
|
|
|
assert.equal(widget.value(), "");
|
2020-05-16 08:23:32 +02:00
|
|
|
assert.equal(updated_value, null);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$reset_button.visible());
|
2021-02-12 23:49:22 +01:00
|
|
|
|
|
|
|
widget.update("four");
|
|
|
|
assert.equal($widget.text(), "translated: not set");
|
|
|
|
assert.equal(widget.value(), "four");
|
|
|
|
assert.equal(updated_value, "four");
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$reset_button.visible());
|
2020-05-16 08:23:32 +02:00
|
|
|
});
|
2020-05-19 00:16:34 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("no_default_value", () => {
|
2020-05-19 00:16:34 +02:00
|
|
|
const opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
widget_name: "my_setting",
|
|
|
|
data: ["one", "two", "three"].map((x) => ({name: x, value: x})),
|
2021-04-13 06:51:54 +02:00
|
|
|
default_text: $t({defaultMessage: "not set"}),
|
2022-04-09 23:44:38 +02:00
|
|
|
render_text: /* istanbul ignore next */ (text) => `rendered: ${text}`,
|
2020-07-15 01:29:15 +02:00
|
|
|
null_value: "null-value",
|
2020-05-19 00:16:34 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
|
|
|
"dropdown-list-widget: Called without a default value; using null value",
|
|
|
|
);
|
2021-07-27 14:35:58 +02:00
|
|
|
setup_dropdown_zjquery_data(opts.widget_name);
|
2021-07-27 14:05:22 +02:00
|
|
|
const widget = new DropdownListWidget(opts);
|
2022-04-28 00:12:57 +02:00
|
|
|
widget.setup();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(widget.value(), "null-value");
|
2020-05-19 00:16:34 +02:00
|
|
|
});
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
// For MultiSelectDropdownListWidget
|
|
|
|
const setup_multiselect_dropdown_zjquery_data = function (name) {
|
|
|
|
$(`#${CSS.escape(name)}_widget`)[0] = {};
|
|
|
|
return setup_dropdown_zjquery_data(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
run_test("basic MDLW functions", () => {
|
|
|
|
let updated_value;
|
|
|
|
const opts = {
|
|
|
|
widget_name: "my_setting",
|
|
|
|
data: ["one", "two", "three", "four"].map((x) => ({name: x, value: x})),
|
|
|
|
value: ["one"],
|
|
|
|
limit: 2,
|
2022-11-17 23:33:43 +01:00
|
|
|
on_update(val) {
|
2021-07-27 14:35:58 +02:00
|
|
|
updated_value = val;
|
|
|
|
},
|
|
|
|
default_text: $t({defaultMessage: "not set"}),
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const {$reset_button, $widget} = setup_multiselect_dropdown_zjquery_data(opts.widget_name);
|
2021-07-27 14:35:58 +02:00
|
|
|
const widget = new MultiSelectDropdownListWidget(opts);
|
2022-04-28 00:12:57 +02:00
|
|
|
widget.setup();
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
function set_dropdown_variables(widget, value) {
|
|
|
|
widget.data_selected = value;
|
|
|
|
widget.checked_items = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual(widget.value(), ["one"]);
|
|
|
|
assert.equal(updated_value, undefined);
|
|
|
|
assert.equal($widget.text(), "one");
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($reset_button.visible());
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
set_dropdown_variables(widget, ["one", "two"]);
|
|
|
|
widget.update(widget.data_selected);
|
|
|
|
|
|
|
|
assert.equal($widget.text(), "one,two");
|
|
|
|
assert.deepEqual(widget.value(), ["one", "two"]);
|
|
|
|
assert.deepEqual(updated_value, ["one", "two"]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($reset_button.visible());
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
set_dropdown_variables(widget, ["one", "two", "three"]);
|
|
|
|
widget.update(widget.data_selected);
|
|
|
|
|
|
|
|
assert.equal($widget.text(), "translated: 3 selected");
|
|
|
|
assert.deepEqual(widget.value(), ["one", "two", "three"]);
|
|
|
|
assert.deepEqual(updated_value, ["one", "two", "three"]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($reset_button.visible());
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
set_dropdown_variables(widget, null);
|
|
|
|
widget.update(widget.data_selected);
|
|
|
|
|
|
|
|
assert.equal($widget.text(), "translated: not set");
|
|
|
|
assert.equal(widget.value(), null);
|
|
|
|
assert.equal(updated_value, null);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$reset_button.visible());
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
set_dropdown_variables(widget, ["one"]);
|
|
|
|
widget.update(widget.data_selected);
|
|
|
|
|
|
|
|
assert.equal($widget.text(), "one");
|
|
|
|
assert.deepEqual(widget.value(), ["one"]);
|
|
|
|
assert.deepEqual(updated_value, ["one"]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($reset_button.visible());
|
2021-07-27 14:35:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("MDLW no_default_value", () => {
|
|
|
|
const opts = {
|
|
|
|
widget_name: "my_setting",
|
|
|
|
data: ["one", "two", "three", "four"].map((x) => ({name: x, value: x})),
|
|
|
|
limit: 2,
|
|
|
|
null_value: "null-value",
|
|
|
|
default_text: $t({defaultMessage: "not set"}),
|
|
|
|
};
|
|
|
|
|
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
|
|
|
"dropdown-list-widget: Called without a default value; using null value",
|
|
|
|
);
|
|
|
|
|
|
|
|
setup_multiselect_dropdown_zjquery_data(opts.widget_name);
|
|
|
|
const widget = new MultiSelectDropdownListWidget(opts);
|
2022-04-28 00:12:57 +02:00
|
|
|
widget.setup();
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
assert.equal(widget.value(), "null-value");
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("MDLW no_limit_set", () => {
|
|
|
|
const opts = {
|
|
|
|
widget_name: "my_setting",
|
|
|
|
data: ["one", "two", "three", "four"].map((x) => ({name: x, value: x})),
|
|
|
|
value: ["one"],
|
|
|
|
default_text: $t({defaultMessage: "not set"}),
|
|
|
|
};
|
|
|
|
|
|
|
|
blueslip.expect(
|
|
|
|
"warn",
|
|
|
|
"Multiselect dropdown-list-widget: Called without limit value; using 2 as the limit",
|
|
|
|
);
|
|
|
|
|
|
|
|
function set_dropdown_variables(widget, value) {
|
|
|
|
widget.data_selected = value;
|
|
|
|
widget.checked_items = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {$widget} = setup_multiselect_dropdown_zjquery_data(opts.widget_name);
|
|
|
|
const widget = new MultiSelectDropdownListWidget(opts);
|
2022-04-28 00:12:57 +02:00
|
|
|
widget.setup();
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
set_dropdown_variables(widget, ["one", "two", "three"]);
|
|
|
|
widget.update(widget.data_selected);
|
|
|
|
|
|
|
|
// limit is set to 2 (Default value).
|
|
|
|
assert.equal($widget.text(), "translated: 3 selected");
|
|
|
|
|
|
|
|
set_dropdown_variables(widget, ["one"]);
|
|
|
|
widget.update(widget.data_selected);
|
|
|
|
|
|
|
|
assert.equal($widget.text(), "one");
|
|
|
|
});
|