mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
When we were preparing the conversion to ES modules in 2019, the primary obstacle was that the Node tests extensively relied on the ability to reach into modules and mutate their CommonJS exports in order to mock things. ES module bindings are not mutable, so in commit173c9cee42we added babel-plugin-rewire-ts as a kludgy transpilation-based workaround for this to unblock the conversion. However, babel-plugin-rewire-ts is slow, buggy, nonstandard, confusing, and unmaintained. It’s incompatible with running our ES modules as native ES modules, and prevents us from taking advantage of modern tools for ES modules. So we want to excise all use of __Rewire__ (and the disallow_rewire, override_rewire helper functions that rely on it) from the tests and remove babel-plugin-rewire-ts. Commits64abdc199eande17ba5260a(#20730) prepared for this by letting us see where __Rewire__ is being used. Now we go through and remove most of the uses that are easy to remove without modifying the production code at all. Signed-off-by: Anders Kaseorg <anders@zulip.com>
85 lines
2.5 KiB
JavaScript
85 lines
2.5 KiB
JavaScript
"use strict";
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
const {mock_esm, zrequire} = require("../zjsunit/namespace");
|
|
const {run_test} = require("../zjsunit/test");
|
|
const $ = require("../zjsunit/zjquery");
|
|
|
|
const list_widget = mock_esm("../../static/js/list_widget");
|
|
const muted_topics_ui = mock_esm("../../static/js/muted_topics_ui");
|
|
|
|
const settings_muted_topics = zrequire("settings_muted_topics");
|
|
const stream_data = zrequire("stream_data");
|
|
const muted_topics = zrequire("muted_topics");
|
|
|
|
const noop = () => {};
|
|
|
|
const frontend = {
|
|
stream_id: 101,
|
|
name: "frontend",
|
|
};
|
|
stream_data.add_sub(frontend);
|
|
|
|
run_test("settings", ({override}) => {
|
|
muted_topics.add_muted_topic(frontend.stream_id, "js", 1577836800);
|
|
let populate_list_called = false;
|
|
override(list_widget, "create", ($container, list) => {
|
|
assert.deepEqual(list, [
|
|
{
|
|
date_muted: 1577836800000,
|
|
date_muted_str: "Jan\u00A001,\u00A02020",
|
|
stream: frontend.name,
|
|
stream_id: frontend.stream_id,
|
|
topic: "js",
|
|
},
|
|
]);
|
|
populate_list_called = true;
|
|
});
|
|
|
|
settings_muted_topics.reset();
|
|
assert.equal(settings_muted_topics.loaded, false);
|
|
|
|
settings_muted_topics.set_up();
|
|
assert.equal(settings_muted_topics.loaded, true);
|
|
assert.ok(populate_list_called);
|
|
|
|
const topic_click_handler = $("body").get_on_handler("click", ".settings-unmute-topic");
|
|
assert.equal(typeof topic_click_handler, "function");
|
|
|
|
const event = {
|
|
stopPropagation: noop,
|
|
};
|
|
|
|
const $topic_fake_this = $.create("fake.settings-unmute-topic");
|
|
const $topic_tr_html = $('tr[data-topic="js"]');
|
|
$topic_fake_this.closest = (opts) => {
|
|
assert.equal(opts, "tr");
|
|
return $topic_tr_html;
|
|
};
|
|
|
|
let topic_data_called = 0;
|
|
$topic_tr_html.attr = (opts) => {
|
|
topic_data_called += 1;
|
|
switch (opts) {
|
|
case "data-stream-id":
|
|
return frontend.stream_id;
|
|
case "data-topic":
|
|
return "js";
|
|
/* istanbul ignore next */
|
|
default:
|
|
throw new Error(`Unknown attribute ${opts}`);
|
|
}
|
|
};
|
|
|
|
let unmute_topic_called = false;
|
|
muted_topics_ui.unmute_topic = (stream_id, topic) => {
|
|
assert.equal(stream_id, frontend.stream_id);
|
|
assert.equal(topic, "js");
|
|
unmute_topic_called = true;
|
|
};
|
|
topic_click_handler.call($topic_fake_this, event);
|
|
assert.ok(unmute_topic_called);
|
|
assert.equal(topic_data_called, 2);
|
|
});
|