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} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const $ = require("./lib/zjquery");
|
2018-07-17 12:13:19 +02:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const channel = mock_esm("../src/channel");
|
2021-02-28 00:39:51 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const alert_words = zrequire("alert_words");
|
|
|
|
const alert_words_ui = zrequire("alert_words_ui");
|
2018-07-17 12:13:19 +02:00
|
|
|
|
2020-02-26 23:53:46 +01:00
|
|
|
alert_words.initialize({
|
2020-07-15 01:29:15 +02:00
|
|
|
alert_words: ["foo", "bar"],
|
2020-02-26 23:53:46 +01:00
|
|
|
});
|
2022-02-21 18:23:21 +01:00
|
|
|
const noop = () => {};
|
2020-02-26 23:53:46 +01:00
|
|
|
|
2022-02-26 01:58:57 +01:00
|
|
|
run_test("rerender_alert_words_ui", ({mock_template}) => {
|
2022-02-21 18:23:21 +01:00
|
|
|
let list_widget_create_called = false;
|
|
|
|
alert_words_ui.reset();
|
2023-02-22 23:04:10 +01:00
|
|
|
const ListWidget = mock_esm("../src/list_widget", {
|
2022-02-21 18:23:21 +01:00
|
|
|
modifier: noop,
|
2023-06-29 21:59:08 +02:00
|
|
|
create(_container, words, opts) {
|
2022-02-21 18:23:21 +01:00
|
|
|
const alert_words = [];
|
|
|
|
ListWidget.modifier = opts.modifier;
|
|
|
|
for (const word of words) {
|
|
|
|
alert_words.push(opts.modifier(word));
|
|
|
|
}
|
|
|
|
list_widget_create_called = true;
|
|
|
|
return alert_words;
|
|
|
|
},
|
2023-05-03 07:06:19 +02:00
|
|
|
generic_sort_functions: noop,
|
2022-02-21 18:23:21 +01:00
|
|
|
});
|
|
|
|
mock_template("settings/alert_word_settings_item.hbs", false, (args) => {
|
|
|
|
assert.ok(["foo", "bar"].includes(args.alert_word.word));
|
|
|
|
});
|
|
|
|
assert.equal(alert_words_ui.loaded, false);
|
2022-02-26 01:58:57 +01:00
|
|
|
alert_words_ui.rerender_alert_words_ui();
|
2022-02-21 18:23:21 +01:00
|
|
|
assert.equal(list_widget_create_called, false);
|
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
assert.equal(alert_words_ui.loaded, true);
|
|
|
|
assert.equal(list_widget_create_called, true);
|
2018-07-20 18:28:02 +02:00
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test("remove_alert_word", ({override_rewire}) => {
|
2022-02-26 01:58:57 +01:00
|
|
|
override_rewire(alert_words_ui, "rerender_alert_words_ui", () => {});
|
2021-02-21 12:09:04 +01:00
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $word_list = $("#alert-words-table");
|
|
|
|
const remove_func = $word_list.get_on_handler("click", ".remove-alert-word");
|
2018-07-20 18:43:21 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $remove_alert_word = $(".remove-alert-word");
|
|
|
|
const $list_item = $("tr.alert-word-item");
|
|
|
|
const $val_item = $("span.value");
|
|
|
|
$val_item.text($t({defaultMessage: "zot"}));
|
2018-07-20 18:43:21 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$remove_alert_word.set_parents_result("tr", $list_item);
|
|
|
|
$list_item.set_find_results(".value", $val_item);
|
2018-07-20 18:43:21 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2020-07-15 01:29:15 +02:00
|
|
|
currentTarget: ".remove-alert-word",
|
2018-07-20 18:43:21 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let success_func;
|
|
|
|
let fail_func;
|
2018-07-20 18:43:21 +02:00
|
|
|
channel.del = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/users/me/alert_words");
|
2018-07-20 18:43:21 +02:00
|
|
|
assert.deepEqual(opts.data, {alert_words: '["translated: zot"]'});
|
|
|
|
success_func = opts.success;
|
|
|
|
fail_func = opts.error;
|
|
|
|
};
|
|
|
|
|
|
|
|
remove_func(event);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $alert_word_status = $("#alert_word_status");
|
|
|
|
const $alert_word_status_text = $(".alert_word_status_text");
|
|
|
|
$alert_word_status.set_find_results(".alert_word_status_text", $alert_word_status_text);
|
2018-07-20 18:43:21 +02:00
|
|
|
|
|
|
|
// test failure
|
|
|
|
fail_func();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($alert_word_status.hasClass("alert-danger"));
|
|
|
|
assert.equal($alert_word_status_text.text(), "translated: Error removing alert word!");
|
|
|
|
assert.ok($alert_word_status.visible());
|
2018-07-20 18:43:21 +02:00
|
|
|
|
|
|
|
// test success
|
|
|
|
success_func();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($alert_word_status.hasClass("alert-success"));
|
2022-11-20 22:12:05 +01:00
|
|
|
assert.equal(
|
|
|
|
$alert_word_status_text.text(),
|
|
|
|
`translated: Alert word "translated: zot" removed successfully!`,
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($alert_word_status.visible());
|
2018-07-20 18:43:21 +02:00
|
|
|
});
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test("close_status_message", ({override_rewire}) => {
|
2022-02-26 01:58:57 +01:00
|
|
|
override_rewire(alert_words_ui, "rerender_alert_words_ui", () => {});
|
2021-02-21 12:09:04 +01:00
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $alert_word_settings = $("#alert-word-settings");
|
|
|
|
const close = $alert_word_settings.get_on_handler("click", ".close-alert-word-status");
|
2018-07-20 18:52:41 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $alert = $(".alert");
|
|
|
|
const $close_btn = $(".close-alert-word-status");
|
|
|
|
$close_btn.set_parents_result(".alert", $alert);
|
2018-07-20 18:52:41 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$alert.show();
|
2018-07-20 18:52:41 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2022-11-17 23:33:43 +01:00
|
|
|
preventDefault() {},
|
2020-07-15 01:29:15 +02:00
|
|
|
currentTarget: ".close-alert-word-status",
|
2018-07-20 18:52:41 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($alert.visible());
|
2018-07-20 18:52:41 +02:00
|
|
|
close(event);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok(!$alert.visible());
|
2018-07-20 18:52:41 +02:00
|
|
|
});
|