diff --git a/frontend_tests/node_tests/unread.js b/frontend_tests/node_tests/unread.js index e6f9095308..0ccc9c8e72 100644 --- a/frontend_tests/node_tests/unread.js +++ b/frontend_tests/node_tests/unread.js @@ -145,6 +145,26 @@ var zero_counts = { }; unread.update_unread_topics(other_message, event); + // Update a message that was never marked as unread. + var sticky_message = { + id: 17, + type: 'stream', + stream_id: stream_id, + subject: 'sticky', + }; + + unread.process_loaded_messages([sticky_message]); + count = unread.num_unread_for_topic(stream_id, 'sticky'); + assert.equal(count, 1); + + unread.mark_as_read(sticky_message.id); + count = unread.num_unread_for_topic(stream_id, 'sticky'); + assert.equal(count, 0); + + unread.update_unread_topics(sticky_message, {subject: 'sticky'}); + count = unread.num_unread_for_topic(stream_id, 'sticky'); + assert.equal(count, 0); + // cleanup unread.mark_as_read(message.id); count = unread.num_unread_for_topic(stream_id, 'dinner'); @@ -153,6 +173,9 @@ var zero_counts = { unread.mark_as_read(other_message.id); count = unread.num_unread_for_topic(stream_id, 'snack'); assert.equal(count, 0); + + // test coverage + unread.update_unread_topics(sticky_message, {}); }()); stream_data.get_stream_id = function () { diff --git a/static/js/unread.js b/static/js/unread.js index 36f3823fc4..c670948159 100644 --- a/static/js/unread.js +++ b/static/js/unread.js @@ -164,11 +164,6 @@ exports.unread_topic_counter = (function () { bucketer.clear(); }; - self.update = function (msg_id, stream_id, new_topic) { - self.del(msg_id); - self.add(stream_id, new_topic, msg_id); - }; - self.add = function (stream_id, topic, msg_id) { bucketer.add({ bucket_key: stream_id, @@ -297,13 +292,23 @@ exports.id_flagged_as_unread = function (message_id) { }; exports.update_unread_topics = function (msg, event) { - if (event.subject !== undefined) { - exports.unread_topic_counter.update( - msg.id, - msg.stream_id, - event.subject - ); + if (event.subject === undefined) { + return; } + + if (!unread_messages.has(msg.id)) { + return; + } + + exports.unread_topic_counter.del( + msg.id + ); + + exports.unread_topic_counter.add( + msg.stream_id, + event.subject, + msg.id + ); }; exports.process_loaded_messages = function (messages) {