mirror of https://github.com/zulip/zulip.git
notifications: Refactor and test notifiable unreads logic.
In this refactor, we extract two functions in unread.js. Which one to use depends on whether res has already been fetched or not. This also adds node tests to maintain coverage of unread.js. Tweaked by tabbott for cleaner variable names and tests.
This commit is contained in:
parent
8cf15fe01d
commit
9f5fca5579
|
@ -1,11 +1,14 @@
|
|||
set_global('i18n', global.stub_i18n);
|
||||
|
||||
zrequire('muting');
|
||||
zrequire('people');
|
||||
zrequire('stream_data');
|
||||
zrequire('util');
|
||||
zrequire('unread');
|
||||
zrequire('settings_notifications');
|
||||
|
||||
set_global('blueslip', {});
|
||||
set_global('page_params', {});
|
||||
set_global('blueslip', {});
|
||||
set_global('narrow_state', {});
|
||||
set_global('current_msg_list', {});
|
||||
set_global('home_msg_list', {});
|
||||
|
@ -36,14 +39,29 @@ var zero_counts = {
|
|||
pm_count: new Dict(),
|
||||
};
|
||||
|
||||
function test_notifiable_count(home_unread_messages, expected_notifiable_count) {
|
||||
set_global('page_params', {
|
||||
desktop_icon_count_display: 1,
|
||||
});
|
||||
var notifiable_counts = unread.get_notifiable_count();
|
||||
assert.deepEqual(notifiable_counts, home_unread_messages);
|
||||
set_global('page_params', {
|
||||
desktop_icon_count_display: 2,
|
||||
});
|
||||
notifiable_counts = unread.get_notifiable_count();
|
||||
assert.deepEqual(notifiable_counts, expected_notifiable_count);
|
||||
}
|
||||
|
||||
run_test('empty_counts_while_narrowed', () => {
|
||||
var counts = unread.get_counts();
|
||||
assert.deepEqual(counts, zero_counts);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
run_test('empty_counts_while_home', () => {
|
||||
var counts = unread.get_counts();
|
||||
assert.deepEqual(counts, zero_counts);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
run_test('changing_topics', () => {
|
||||
|
@ -205,6 +223,7 @@ run_test('muting', () => {
|
|||
assert.equal(counts.home_unread_messages, 1);
|
||||
assert.equal(unread.num_unread_for_stream(stream_id), 1);
|
||||
assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), [message.id]);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
muting.add_muted_topic(social.stream_id, 'test_muting');
|
||||
counts = unread.get_counts();
|
||||
|
@ -212,6 +231,7 @@ run_test('muting', () => {
|
|||
assert.equal(counts.home_unread_messages, 0);
|
||||
assert.equal(unread.num_unread_for_stream(stream_id), 0);
|
||||
assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), []);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
// we still find the message id here (muting is ignored)
|
||||
assert.deepEqual(unread.get_all_msg_ids(), [message.id]);
|
||||
|
@ -322,19 +342,23 @@ run_test('home_messages', () => {
|
|||
|
||||
var counts = unread.get_counts();
|
||||
assert.equal(counts.home_unread_messages, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
unread.process_loaded_messages([message]);
|
||||
|
||||
counts = unread.get_counts();
|
||||
assert.equal(counts.home_unread_messages, 1);
|
||||
assert.equal(counts.stream_count.get(stream_id), 1);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
unread.mark_as_read(message.id);
|
||||
counts = unread.get_counts();
|
||||
assert.equal(counts.home_unread_messages, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
unread.process_loaded_messages([message]);
|
||||
counts = unread.get_counts();
|
||||
assert.equal(counts.home_unread_messages, 1);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
// Now unsubscribe all our streams.
|
||||
stream_data.is_subscribed = function () {
|
||||
|
@ -342,6 +366,7 @@ run_test('home_messages', () => {
|
|||
};
|
||||
counts = unread.get_counts();
|
||||
assert.equal(counts.home_unread_messages, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
});
|
||||
|
||||
|
@ -358,6 +383,7 @@ run_test('phantom_messages', () => {
|
|||
unread.mark_as_read(message.id);
|
||||
var counts = unread.get_counts();
|
||||
assert.equal(counts.home_unread_messages, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
run_test('private_messages', () => {
|
||||
|
@ -386,10 +412,12 @@ run_test('private_messages', () => {
|
|||
counts = unread.get_counts();
|
||||
assert.equal(counts.private_message_count, 1);
|
||||
assert.equal(counts.pm_count.get('999'), 1);
|
||||
test_notifiable_count(counts.home_unread_messages, 1);
|
||||
unread.mark_as_read(message.id);
|
||||
counts = unread.get_counts();
|
||||
assert.equal(counts.private_message_count, 0);
|
||||
assert.equal(counts.pm_count.get('999'), 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
run_test('private_messages', () => {
|
||||
|
@ -443,6 +471,7 @@ run_test('private_messages', () => {
|
|||
assert.deepEqual(unread.get_all_msg_ids(), []);
|
||||
var counts = unread.get_counts();
|
||||
assert.equal(counts.private_message_count, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
|
||||
|
@ -450,6 +479,7 @@ run_test('mentions', () => {
|
|||
var counts = unread.get_counts();
|
||||
assert.equal(counts.mentioned_message_count, 0);
|
||||
assert.deepEqual(unread.get_msg_ids_for_mentions(), []);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
|
||||
var message = {
|
||||
id: 15,
|
||||
|
@ -466,9 +496,11 @@ run_test('mentions', () => {
|
|||
assert.equal(counts.mentioned_message_count, 1);
|
||||
assert.deepEqual(unread.get_msg_ids_for_mentions(), [message.id]);
|
||||
assert.deepEqual(unread.get_all_msg_ids(), [message.id]);
|
||||
test_notifiable_count(counts.home_unread_messages, 1);
|
||||
unread.mark_as_read(message.id);
|
||||
counts = unread.get_counts();
|
||||
assert.equal(counts.mentioned_message_count, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
run_test('starring', () => {
|
||||
|
@ -492,6 +524,7 @@ run_test('declare_bankruptcy', () => {
|
|||
|
||||
var counts = unread.get_counts();
|
||||
assert.deepEqual(counts, zero_counts);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
run_test('message_unread', () => {
|
||||
|
@ -588,5 +621,6 @@ run_test('errors', () => {
|
|||
unread.mark_as_read(message.id);
|
||||
var counts = unread.get_counts();
|
||||
assert.equal(counts.private_message_count, 0);
|
||||
test_notifiable_count(counts.home_unread_messages, 0);
|
||||
});
|
||||
|
||||
|
|
|
@ -130,16 +130,8 @@ exports.permission_state = function () {
|
|||
|
||||
var new_message_count = 0;
|
||||
|
||||
exports.update_title_count = function (res) {
|
||||
var only_show_notifiable = page_params.desktop_icon_count_display ===
|
||||
settings_notifications.desktop_icon_count_display_values.notifiable.code;
|
||||
if (only_show_notifiable) {
|
||||
// DESKTOP_ICON_COUNT_DISPLAY_NOTIFIABLE
|
||||
new_message_count = res.mentioned_message_count + res.private_message_count;
|
||||
} else {
|
||||
// DESKTOP_ICON_COUNT_DISPLAY_MESSAGES
|
||||
new_message_count = res.home_unread_messages;
|
||||
}
|
||||
exports.update_title_count = function (count) {
|
||||
new_message_count = count;
|
||||
exports.redraw_title();
|
||||
};
|
||||
|
||||
|
|
|
@ -83,8 +83,8 @@ function change_notification_setting(setting, setting_data, status_element) {
|
|||
|
||||
function update_desktop_icon_count_display() {
|
||||
$("#desktop_icon_count_display").val(page_params.desktop_icon_count_display);
|
||||
var res = unread.get_counts();
|
||||
notifications.update_title_count(res);
|
||||
var count = unread.get_notifiable_count();
|
||||
notifications.update_title_count(count);
|
||||
}
|
||||
|
||||
exports.set_enable_digest_emails_visibility = function () {
|
||||
|
|
|
@ -546,6 +546,27 @@ exports.get_counts = function () {
|
|||
return res;
|
||||
};
|
||||
|
||||
// Saves us from calling to get_counts() when we can avoid it.
|
||||
exports.calculate_notifiable_count = function (res) {
|
||||
var new_message_count = 0;
|
||||
|
||||
var only_show_notifiable = page_params.desktop_icon_count_display ===
|
||||
settings_notifications.desktop_icon_count_display_values.notifiable.code;
|
||||
if (only_show_notifiable) {
|
||||
// DESKTOP_ICON_COUNT_DISPLAY_NOTIFIABLE
|
||||
new_message_count = res.mentioned_message_count + res.private_message_count;
|
||||
} else {
|
||||
// DESKTOP_ICON_COUNT_DISPLAY_MESSAGES
|
||||
new_message_count = res.home_unread_messages;
|
||||
}
|
||||
return new_message_count;
|
||||
};
|
||||
|
||||
exports.get_notifiable_count = function () {
|
||||
var res = exports.get_counts();
|
||||
return exports.calculate_notifiable_count(res);
|
||||
};
|
||||
|
||||
exports.num_unread_for_stream = function (stream_id) {
|
||||
return exports.unread_topic_counter.get_stream_count(stream_id);
|
||||
};
|
||||
|
|
|
@ -56,7 +56,8 @@ exports.update_unread_counts = function () {
|
|||
stream_list.update_dom_with_unread_counts(res);
|
||||
pm_list.update_dom_with_unread_counts(res);
|
||||
notifications.update_pm_count(res.private_message_count);
|
||||
notifications.update_title_count(res);
|
||||
var notifiable_unread_count = unread.calculate_notifiable_count(res);
|
||||
notifications.update_title_count(notifiable_unread_count);
|
||||
|
||||
exports.set_count_toggle_button($("#streamlist-toggle-unreadcount"),
|
||||
res.home_unread_messages);
|
||||
|
|
Loading…
Reference in New Issue