diff --git a/frontend_tests/node_tests/message_fetch.js b/frontend_tests/node_tests/message_fetch.js index ed217368be..b06f4f1ea1 100644 --- a/frontend_tests/node_tests/message_fetch.js +++ b/frontend_tests/node_tests/message_fetch.js @@ -16,9 +16,8 @@ zrequire('people'); set_global('recent_topics', { process_messages: noop, }); -set_global('page_params', { - pointer: 444, -}); +// Still required for page_params.initial_pointer +set_global('page_params', {}); set_global('ui_report', { hide_error: noop, }); @@ -311,18 +310,36 @@ run_test('loading_newer', () => { can_call_again: true, }); - message_fetch.maybe_load_newer_messages({ - msg_list: msg_list, - show_loading: noop, - hide_loading: noop, - }); + // The msg_list is empty and we are calling frontfill, which should + // raise fatal error. + if (opts.empty_msg_list) { + assert.throws( + () => { + message_fetch.maybe_load_newer_messages({ + msg_list: msg_list, + show_loading: noop, + hide_loading: noop, + }); + }, + { + name: "Error", + message: "There are no message available to frontfill.", + } + ); + } else { + message_fetch.maybe_load_newer_messages({ + msg_list: msg_list, + show_loading: noop, + hide_loading: noop, + }); - test_dup_new_fetch(msg_list); + test_dup_new_fetch(msg_list); - test_fetch_success({ - fetch: fetch, - response: data.resp, - }); + test_fetch_success({ + fetch: fetch, + response: data.resp, + }); + } } (function test_narrow() { @@ -345,6 +362,18 @@ run_test('loading_newer', () => { test_happy_path({ msg_list: msg_list, data: data, + empty_msg_list: true, + }); + + msg_list.append_to_view = () => {}; + // Instead of using 444 as page_param.pointer, we + // should have a message with that id in the message_list. + msg_list.append(message_range(444, 445), false); + + test_happy_path({ + msg_list: msg_list, + data: data, + empty_msg_list: false, }); assert.equal(msg_list.data.fetch_status.can_load_newer_messages(), true); @@ -402,6 +431,16 @@ run_test('loading_newer', () => { test_happy_path({ msg_list: msg_list, data: data[0], + empty_msg_list: true, + }); + + message_list.all.append_to_view = () => {}; + message_list.all.append(message_range(444, 445), false); + + test_happy_path({ + msg_list: msg_list, + data: data[0], + empty_msg_list: false, }); assert.equal(msg_list.data.fetch_status.can_load_newer_messages(), true); diff --git a/static/js/message_fetch.js b/static/js/message_fetch.js index 60c345bccd..b2ff38e282 100644 --- a/static/js/message_fetch.js +++ b/static/js/message_fetch.js @@ -252,18 +252,19 @@ exports.load_messages_for_narrow = function (opts) { }; exports.get_backfill_anchor = function (msg_list) { - let oldest_message_id; - if (msg_list === home_msg_list) { msg_list = message_list.all; } - if (msg_list.first() === undefined) { - oldest_message_id = page_params.pointer; - } else { - oldest_message_id = msg_list.first().id; + const oldest_msg = msg_list.first(); + if (oldest_msg) { + return oldest_msg.id; } - return oldest_message_id; + + // msg_list is empty, which is an impossible + // case, raise a fatal error. + blueslip.fatal("There are no message available to backfill."); + return; }; exports.get_frontfill_anchor = function (msg_list) { @@ -277,7 +278,13 @@ exports.get_frontfill_anchor = function (msg_list) { return last_msg.id; } - return page_params.pointer; + // Although it is impossible that we reach here since we + // are already checking `msg_list.fetch_status.can_load_newer_messages` + // and user cannot be scrolling down on an empty message_list to + // fetch more data, and if user is, then the available data is wrong + // and we raise a fatal error. + blueslip.fatal("There are no message available to frontfill."); + return; }; exports.maybe_load_older_messages = function (opts) { @@ -315,7 +322,7 @@ exports.do_backfill = function (opts) { }; exports.maybe_load_newer_messages = function (opts) { - // This function gets called when you scroll to the top + // This function gets called when you scroll to the bottom // of your window, and you want to get messages newer // than what the browsers originally fetched. const msg_list = opts.msg_list;