Files
zulip/frontend_tests/node_tests/settings_muted_topics.js
Abhijeet Prasad Bodas fc53d2c66a topic muting: Clean up live update code.
Previously, we used to do a kind of "local echo"
whenever the user muted/unmuted a topic. Meaning,
we used to do most of the UI update work before making
the API call to mute the topic, instead of after
receiving the `muted_topics` event. This behavior
has been so since the beginning of time
(b4b6fa14d3) and isn't ideal because:
1. If the request fails on the backend, the UI
could end up in an incorrect state.
2. Adds code complexity.
3. Makes it difficult to catch bugs related to
live-update (like the one fixed by
f725711ff2).
4. Isn't consistent with other parts of the
codebase, which do the UI update while handling
events.

This commit makes it so that all the UI update
work is done only after recieving the `muted_topics`
event.

The only possible issue with this strategy could
be users sending another duplicate request in the small
time span before receiving the event. But that isn't
a big problem, because all requests involved here are
idempotent, and the worst that can happen is a HTTP 400.
2021-04-27 17:53:06 -07:00

85 lines
2.5 KiB
JavaScript

"use strict";
const {strict: assert} = require("assert");
const {mock_cjs, mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
mock_cjs("jquery", $);
const muting_ui = mock_esm("../../static/js/muting_ui");
const settings_muted_topics = zrequire("settings_muted_topics");
const stream_data = zrequire("stream_data");
const muting = zrequire("muting");
const noop = () => {};
const frontend = {
stream_id: 101,
name: "frontend",
};
stream_data.add_sub(frontend);
run_test("settings", (override) => {
muting.add_muted_topic(frontend.stream_id, "js", 1577836800);
let populate_list_called = false;
override(settings_muted_topics, "populate_list", () => {
const opts = muting.get_muted_topics();
assert.deepEqual(opts, [
{
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(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) => {
if (opts === "data-stream-id") {
topic_data_called += 1;
return frontend.stream_id;
}
if (opts === "data-topic") {
topic_data_called += 1;
return "js";
}
throw new Error(`Unknown attribute ${opts}`);
};
let unmute_topic_called = false;
muting_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(unmute_topic_called);
assert.equal(topic_data_called, 2);
});