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:
YashRE42 2019-07-20 18:15:56 +05:30 committed by Tim Abbott
parent 8cf15fe01d
commit 9f5fca5579
5 changed files with 62 additions and 14 deletions

View File

@ -1,11 +1,14 @@
set_global('i18n', global.stub_i18n);
zrequire('muting'); zrequire('muting');
zrequire('people'); zrequire('people');
zrequire('stream_data'); zrequire('stream_data');
zrequire('util'); zrequire('util');
zrequire('unread'); zrequire('unread');
zrequire('settings_notifications');
set_global('blueslip', {});
set_global('page_params', {}); set_global('page_params', {});
set_global('blueslip', {});
set_global('narrow_state', {}); set_global('narrow_state', {});
set_global('current_msg_list', {}); set_global('current_msg_list', {});
set_global('home_msg_list', {}); set_global('home_msg_list', {});
@ -36,14 +39,29 @@ var zero_counts = {
pm_count: new Dict(), 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', () => { run_test('empty_counts_while_narrowed', () => {
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.deepEqual(counts, zero_counts); assert.deepEqual(counts, zero_counts);
test_notifiable_count(counts.home_unread_messages, 0);
}); });
run_test('empty_counts_while_home', () => { run_test('empty_counts_while_home', () => {
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.deepEqual(counts, zero_counts); assert.deepEqual(counts, zero_counts);
test_notifiable_count(counts.home_unread_messages, 0);
}); });
run_test('changing_topics', () => { run_test('changing_topics', () => {
@ -205,6 +223,7 @@ run_test('muting', () => {
assert.equal(counts.home_unread_messages, 1); assert.equal(counts.home_unread_messages, 1);
assert.equal(unread.num_unread_for_stream(stream_id), 1); assert.equal(unread.num_unread_for_stream(stream_id), 1);
assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), [message.id]); 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'); muting.add_muted_topic(social.stream_id, 'test_muting');
counts = unread.get_counts(); counts = unread.get_counts();
@ -212,6 +231,7 @@ run_test('muting', () => {
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
assert.equal(unread.num_unread_for_stream(stream_id), 0); assert.equal(unread.num_unread_for_stream(stream_id), 0);
assert.deepEqual(unread.get_msg_ids_for_stream(stream_id), []); 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) // we still find the message id here (muting is ignored)
assert.deepEqual(unread.get_all_msg_ids(), [message.id]); assert.deepEqual(unread.get_all_msg_ids(), [message.id]);
@ -322,19 +342,23 @@ run_test('home_messages', () => {
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
test_notifiable_count(counts.home_unread_messages, 0);
unread.process_loaded_messages([message]); unread.process_loaded_messages([message]);
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 1); assert.equal(counts.home_unread_messages, 1);
assert.equal(counts.stream_count.get(stream_id), 1); assert.equal(counts.stream_count.get(stream_id), 1);
test_notifiable_count(counts.home_unread_messages, 0);
unread.mark_as_read(message.id); unread.mark_as_read(message.id);
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
test_notifiable_count(counts.home_unread_messages, 0);
unread.process_loaded_messages([message]); unread.process_loaded_messages([message]);
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 1); assert.equal(counts.home_unread_messages, 1);
test_notifiable_count(counts.home_unread_messages, 0);
// Now unsubscribe all our streams. // Now unsubscribe all our streams.
stream_data.is_subscribed = function () { stream_data.is_subscribed = function () {
@ -342,6 +366,7 @@ run_test('home_messages', () => {
}; };
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 0); 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); unread.mark_as_read(message.id);
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.home_unread_messages, 0); assert.equal(counts.home_unread_messages, 0);
test_notifiable_count(counts.home_unread_messages, 0);
}); });
run_test('private_messages', () => { run_test('private_messages', () => {
@ -386,10 +412,12 @@ run_test('private_messages', () => {
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.private_message_count, 1); assert.equal(counts.private_message_count, 1);
assert.equal(counts.pm_count.get('999'), 1); assert.equal(counts.pm_count.get('999'), 1);
test_notifiable_count(counts.home_unread_messages, 1);
unread.mark_as_read(message.id); unread.mark_as_read(message.id);
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.private_message_count, 0); assert.equal(counts.private_message_count, 0);
assert.equal(counts.pm_count.get('999'), 0); assert.equal(counts.pm_count.get('999'), 0);
test_notifiable_count(counts.home_unread_messages, 0);
}); });
run_test('private_messages', () => { run_test('private_messages', () => {
@ -443,6 +471,7 @@ run_test('private_messages', () => {
assert.deepEqual(unread.get_all_msg_ids(), []); assert.deepEqual(unread.get_all_msg_ids(), []);
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.private_message_count, 0); 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(); var counts = unread.get_counts();
assert.equal(counts.mentioned_message_count, 0); assert.equal(counts.mentioned_message_count, 0);
assert.deepEqual(unread.get_msg_ids_for_mentions(), []); assert.deepEqual(unread.get_msg_ids_for_mentions(), []);
test_notifiable_count(counts.home_unread_messages, 0);
var message = { var message = {
id: 15, id: 15,
@ -466,9 +496,11 @@ run_test('mentions', () => {
assert.equal(counts.mentioned_message_count, 1); assert.equal(counts.mentioned_message_count, 1);
assert.deepEqual(unread.get_msg_ids_for_mentions(), [message.id]); assert.deepEqual(unread.get_msg_ids_for_mentions(), [message.id]);
assert.deepEqual(unread.get_all_msg_ids(), [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); unread.mark_as_read(message.id);
counts = unread.get_counts(); counts = unread.get_counts();
assert.equal(counts.mentioned_message_count, 0); assert.equal(counts.mentioned_message_count, 0);
test_notifiable_count(counts.home_unread_messages, 0);
}); });
run_test('starring', () => { run_test('starring', () => {
@ -492,6 +524,7 @@ run_test('declare_bankruptcy', () => {
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.deepEqual(counts, zero_counts); assert.deepEqual(counts, zero_counts);
test_notifiable_count(counts.home_unread_messages, 0);
}); });
run_test('message_unread', () => { run_test('message_unread', () => {
@ -588,5 +621,6 @@ run_test('errors', () => {
unread.mark_as_read(message.id); unread.mark_as_read(message.id);
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.private_message_count, 0); assert.equal(counts.private_message_count, 0);
test_notifiable_count(counts.home_unread_messages, 0);
}); });

View File

@ -130,16 +130,8 @@ exports.permission_state = function () {
var new_message_count = 0; var new_message_count = 0;
exports.update_title_count = function (res) { exports.update_title_count = function (count) {
var only_show_notifiable = page_params.desktop_icon_count_display === new_message_count = count;
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.redraw_title(); exports.redraw_title();
}; };

View File

@ -83,8 +83,8 @@ function change_notification_setting(setting, setting_data, status_element) {
function update_desktop_icon_count_display() { function update_desktop_icon_count_display() {
$("#desktop_icon_count_display").val(page_params.desktop_icon_count_display); $("#desktop_icon_count_display").val(page_params.desktop_icon_count_display);
var res = unread.get_counts(); var count = unread.get_notifiable_count();
notifications.update_title_count(res); notifications.update_title_count(count);
} }
exports.set_enable_digest_emails_visibility = function () { exports.set_enable_digest_emails_visibility = function () {

View File

@ -546,6 +546,27 @@ exports.get_counts = function () {
return res; 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) { exports.num_unread_for_stream = function (stream_id) {
return exports.unread_topic_counter.get_stream_count(stream_id); return exports.unread_topic_counter.get_stream_count(stream_id);
}; };

View File

@ -56,7 +56,8 @@ exports.update_unread_counts = function () {
stream_list.update_dom_with_unread_counts(res); stream_list.update_dom_with_unread_counts(res);
pm_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_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"), exports.set_count_toggle_button($("#streamlist-toggle-unreadcount"),
res.home_unread_messages); res.home_unread_messages);