Files
zulip/frontend_tests/node_tests/muting.js
Steve Howell a4c80089f3 page load: Fix two bugs related to muting/unreads.
The first bug fixed here has been around for a long
time--we were redundantly updating unread counts
indirectly via muting_ui.initialize(). The
unread counts also get updated in
unread_ui.initialize(), when we have more valid
state.  (And it's worth noting here that the unread
counts get updated yet again once message fetches
complete.)

The second bug was a very recent regression from
my recent stream name -> stream id cleanup in the
muting system.  We now depend on stream_data to
initialize muting data, so we need to initialize
muting.js slightly later in the process.

These fixes are intertwined, because they were both
somewhat caused by the anti-pattern of having
muting_ui.js initialize unread_ui.js and muting.js,
instead of doing more direct, fine-grained initialization
from ui_init.js.

Essentially we replace this code:

    exports.update_muted_topics = function (muted_topics) {
        muting.set_muted_topics(muted_topics);
        unread_ui.update_unread_counts();
    };

with this:

    exports.initialize = function () {
        exports.set_muted_topics(page_params.muted_topics);
    };

And the modules load like this:

    stream_data
    ...
    muting
    ...
    unread_ui

And we don't need any page-load initialization for muting_ui,
which is mostly used for Settings/Muted topics.
2018-12-15 13:44:30 -08:00

101 lines
2.6 KiB
JavaScript

zrequire('muting');
zrequire('stream_data');
set_global('blueslip', global.make_zblueslip());
set_global('page_params', {});
run_test('edge_cases', () => {
// private messages
assert(!muting.is_topic_muted(undefined, undefined));
// defensive
assert(!muting.is_topic_muted('nonexistent', undefined));
});
var design = {
stream_id: 100,
name: 'design',
};
var devel = {
stream_id: 101,
name: 'devel',
};
var office = {
stream_id: 102,
name: 'office',
};
var social = {
stream_id: 103,
name: 'social',
};
var unknown = {
stream_id: 999,
name: 'whatever',
};
stream_data.add_sub(design.name, design);
stream_data.add_sub(devel.name, devel);
stream_data.add_sub(office.name, office);
stream_data.add_sub(social.name, social);
run_test('basics', () => {
assert(!muting.is_topic_muted(devel.stream_id, 'java'));
muting.add_muted_topic(devel.stream_id, 'java');
assert(muting.is_topic_muted(devel.stream_id, 'java'));
// test idempotentcy
muting.add_muted_topic(devel.stream_id, 'java');
assert(muting.is_topic_muted(devel.stream_id, 'java'));
muting.remove_muted_topic(devel.stream_id, 'java');
assert(!muting.is_topic_muted(devel.stream_id, 'java'));
// test idempotentcy
muting.remove_muted_topic(devel.stream_id, 'java');
assert(!muting.is_topic_muted(devel.stream_id, 'java'));
// test unknown stream is harmless too
muting.remove_muted_topic(unknown.stream_id, 'java');
assert(!muting.is_topic_muted(unknown.stream_id, 'java'));
});
run_test('get_and_set_muted_topics', () => {
assert.deepEqual(muting.get_muted_topics(), []);
muting.add_muted_topic(office.stream_id, 'gossip');
muting.add_muted_topic(devel.stream_id, 'java');
assert.deepEqual(muting.get_muted_topics().sort(), [
[devel.stream_id, 'java'],
[office.stream_id, 'gossip'],
]);
blueslip.set_test_data('warn', 'Unknown stream in set_muted_topics: BOGUS STREAM');
page_params.muted_topics = [
['social', 'breakfast'],
['design', 'typography'],
['BOGUS STREAM', 'whatever'],
];
muting.initialize();
blueslip.clear_test_data();
assert.deepEqual(muting.get_muted_topics().sort(), [
[design.stream_id, 'typography'],
[social.stream_id, 'breakfast'],
]);
});
run_test('case_insensitivity', () => {
muting.set_muted_topics([]);
assert(!muting.is_topic_muted(social.stream_id, 'breakfast'));
muting.set_muted_topics([
['SOCial', 'breakfast'],
]);
assert(muting.is_topic_muted(social.stream_id, 'breakfast'));
assert(muting.is_topic_muted(social.stream_id, 'breakFAST'));
});