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");
|
2021-04-13 06:51:54 +02:00
|
|
|
const {$t} = require("../zjsunit/i18n");
|
2021-03-11 05:43:45 +01:00
|
|
|
const {mock_cjs, mock_esm, 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");
|
2018-07-17 12:13:19 +02:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
2021-03-10 06:10:32 +01:00
|
|
|
const channel = mock_esm("../../static/js/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
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("render_alert_words_ui", () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const word_list = $("#alert_words_list");
|
2019-11-02 00:06:25 +01:00
|
|
|
const appended = [];
|
2018-07-17 12:13:19 +02:00
|
|
|
word_list.append = (rendered) => {
|
|
|
|
appended.push(rendered);
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const alert_word_items = $.create("alert_word_items");
|
|
|
|
word_list.set_find_results(".alert-word-item", alert_word_items);
|
2018-07-17 12:13:19 +02:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
alert_word_items.remove = () => {};
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates((name, args) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(name, "settings/alert_word_settings_item");
|
|
|
|
return "stub-" + args.word;
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2018-07-17 12:13:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const new_alert_word = $("#create_alert_word_name");
|
2018-07-17 12:13:19 +02:00
|
|
|
assert(!new_alert_word.is_focused());
|
|
|
|
|
|
|
|
alert_words_ui.render_alert_words_ui();
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(appended, ["stub-bar", "stub-foo"]);
|
2018-07-17 12:13:19 +02:00
|
|
|
assert(new_alert_word.is_focused());
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("add_alert_word", (override) => {
|
2021-02-21 12:09:04 +01:00
|
|
|
override(alert_words_ui, "render_alert_words_ui", () => {}); // we've already tested this above
|
2018-07-17 20:18:39 +02:00
|
|
|
|
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const create_form = $("#create_alert_word_form");
|
|
|
|
const add_func = create_form.get_on_handler("click", "#create_alert_word_button");
|
2018-07-17 20:18:39 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const new_alert_word = $("#create_alert_word_name");
|
|
|
|
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-17 20:18:39 +02:00
|
|
|
|
|
|
|
// add '' as alert word
|
|
|
|
add_func();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(new_alert_word.val(), "");
|
|
|
|
assert(alert_word_status.hasClass("alert-danger"));
|
2018-07-17 20:18:39 +02:00
|
|
|
assert.equal(alert_word_status_text.text(), "translated: Alert word can't be empty!");
|
|
|
|
assert(alert_word_status.visible());
|
|
|
|
|
|
|
|
// add 'foo' as alert word (existing word)
|
2020-07-15 01:29:15 +02:00
|
|
|
new_alert_word.val("foo");
|
2018-07-17 20:18:39 +02:00
|
|
|
|
|
|
|
add_func();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(alert_word_status.hasClass("alert-danger"));
|
2018-07-17 20:18:39 +02:00
|
|
|
assert.equal(alert_word_status_text.text(), "translated: Alert word already exists!");
|
|
|
|
assert(alert_word_status.visible());
|
|
|
|
|
|
|
|
// add 'zot' as alert word (new word)
|
2020-07-15 01:29:15 +02:00
|
|
|
new_alert_word.val("zot");
|
2018-07-17 20:18:39 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let success_func;
|
|
|
|
let fail_func;
|
2018-07-17 20:18:39 +02:00
|
|
|
channel.post = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/users/me/alert_words");
|
2018-07-17 20:18:39 +02:00
|
|
|
assert.deepEqual(opts.data, {alert_words: '["zot"]'});
|
|
|
|
success_func = opts.success;
|
|
|
|
fail_func = opts.error;
|
|
|
|
};
|
|
|
|
|
|
|
|
add_func();
|
|
|
|
|
|
|
|
// test failure
|
|
|
|
fail_func();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(alert_word_status.hasClass("alert-danger"));
|
2018-07-17 20:18:39 +02:00
|
|
|
assert.equal(alert_word_status_text.text(), "translated: Error adding alert word!");
|
|
|
|
assert(alert_word_status.visible());
|
|
|
|
|
|
|
|
// test success
|
|
|
|
success_func();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(alert_word_status.hasClass("alert-success"));
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(alert_word_status_text.text(), 'translated: Alert word "zot" added successfully!');
|
2018-07-17 20:18:39 +02:00
|
|
|
assert(alert_word_status.visible());
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("add_alert_word_keypress", (override) => {
|
2021-02-21 12:09:04 +01:00
|
|
|
override(alert_words_ui, "render_alert_words_ui", () => {});
|
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const create_form = $("#create_alert_word_form");
|
|
|
|
const keypress_func = create_form.get_on_handler("keypress", "#create_alert_word_name");
|
2018-07-20 18:28:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const new_alert_word = $("#create_alert_word_name");
|
|
|
|
new_alert_word.val("zot");
|
2018-07-20 18:28:02 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2018-07-20 18:28:02 +02:00
|
|
|
preventDefault: () => {},
|
|
|
|
which: 13,
|
2020-07-15 01:29:15 +02:00
|
|
|
target: "#create_alert_word_name",
|
2018-07-20 18:28:02 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let called = false;
|
2018-07-20 18:28:02 +02:00
|
|
|
channel.post = (opts) => {
|
|
|
|
assert.deepEqual(opts.data, {alert_words: '["zot"]'});
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
keypress_func(event);
|
|
|
|
assert(called);
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("remove_alert_word", (override) => {
|
2021-02-21 12:09:04 +01:00
|
|
|
override(alert_words_ui, "render_alert_words_ui", () => {});
|
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const word_list = $("#alert_words_list");
|
|
|
|
const remove_func = word_list.get_on_handler("click", ".remove-alert-word");
|
2018-07-20 18:43:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const remove_alert_word = $(".remove-alert-word");
|
2021-02-02 01:35:51 +01:00
|
|
|
const list_item = $("tr.alert-word-item");
|
2020-07-15 01:29:15 +02:00
|
|
|
const val_item = $("span.value");
|
2021-04-13 06:51:54 +02:00
|
|
|
val_item.text($t({defaultMessage: "zot"}));
|
2018-07-20 18:43:21 +02:00
|
|
|
|
2021-02-02 01:35:51 +01:00
|
|
|
remove_alert_word.set_parents_result("tr", list_item);
|
2020-07-15 01:29:15 +02:00
|
|
|
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);
|
|
|
|
|
2020-07-15 01:29:15 +02: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();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(alert_word_status.hasClass("alert-danger"));
|
2018-07-20 18:43:21 +02:00
|
|
|
assert.equal(alert_word_status_text.text(), "translated: Error removing alert word!");
|
|
|
|
assert(alert_word_status.visible());
|
|
|
|
|
|
|
|
// test success
|
|
|
|
success_func();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(alert_word_status.hasClass("alert-success"));
|
2018-07-20 18:43:21 +02:00
|
|
|
assert.equal(alert_word_status_text.text(), "translated: Alert word removed successfully!");
|
|
|
|
assert(alert_word_status.visible());
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("close_status_message", (override) => {
|
2021-02-21 12:09:04 +01:00
|
|
|
override(alert_words_ui, "render_alert_words_ui", () => {});
|
|
|
|
alert_words_ui.set_up_alert_words();
|
|
|
|
|
2020-07-15 01:29:15 +02: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
|
|
|
|
2020-07-15 01:29:15 +02: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
|
|
|
|
|
|
|
alert.show();
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2018-07-20 18:52:41 +02:00
|
|
|
preventDefault: () => {},
|
2020-07-15 01:29:15 +02:00
|
|
|
currentTarget: ".close-alert-word-status",
|
2018-07-20 18:52:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(alert.visible());
|
|
|
|
close(event);
|
|
|
|
assert(!alert.visible());
|
|
|
|
});
|