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, 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-06 17:37:51 +01:00
|
|
|
const channel = {};
|
2021-02-28 00:39:51 +01:00
|
|
|
|
|
|
|
rewiremock("../../static/js/channel").with(channel);
|
2021-02-10 04:53:22 +01:00
|
|
|
const alert_words = zrequire("alert_words");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
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");
|
|
|
|
val_item.text(i18n.t("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());
|
|
|
|
});
|