diff --git a/frontend_tests/node_tests/activity.js b/frontend_tests/node_tests/activity.js index 55ba7e78f4..155cfcfbe4 100644 --- a/frontend_tests/node_tests/activity.js +++ b/frontend_tests/node_tests/activity.js @@ -119,6 +119,17 @@ presence.presence_info = presence_info; assert.equal(presence.get_status(zoe.user_id), "unknown"); }()); +(function test_reload_defaults() { + var warned; + + blueslip.warn = function (msg) { + assert.equal(msg, 'get_filter_text() is called before initialization'); + warned = true; + }; + assert.equal(activity.get_filter_text(), ''); + assert(warned); +}()); + (function test_sort_users() { var user_ids = [alice.user_id, fred.user_id, jill.user_id]; diff --git a/static/js/activity.js b/static/js/activity.js index 55b31c1e77..f0e3137cb8 100644 --- a/static/js/activity.js +++ b/static/js/activity.js @@ -244,21 +244,13 @@ function focus_lost() { } function filter_user_ids(user_ids) { - var user_list = meta.$user_list_filter; - if (user_list.length === 0) { - // We may have received an activity ping response after - // initiating a reload, in which case the user list may no - // longer be available. - // Return user list: useful for testing user list performance fix + var filter_text = exports.get_filter_text(); + + if (filter_text === '') { return user_ids; } - var search_term = user_list.expectOne().val().trim(); - if (search_term === '') { - return user_ids; - } - - var search_terms = search_term.toLowerCase().split(","); + var search_terms = filter_text.toLowerCase().split(","); search_terms = _.map(search_terms, function (s) { return s.trim(); }); @@ -355,7 +347,7 @@ exports.build_user_sidebar = function () { var user_ids; - if (meta.$user_list_filter.val().length > 0) { + if (exports.get_filter_text()) { // If there's a filter, select from all users, not just those // recently active. user_ids = filter_and_sort(people.get_realm_persons().map(function (person) { @@ -615,6 +607,21 @@ exports.set_user_list_filter_handlers = function () { .on('blur', update_clear_search_button); }; +exports.get_filter_text = function () { + if (!meta.$user_list_filter) { + // This may be overly defensive, but there may be + // situations where get called before everything is + // fully intialized. The empty string is a fine + // default here. + blueslip.warn('get_filter_text() is called before initialization'); + return ''; + } + + var user_filter = meta.$user_list_filter.expectOne().val().trim(); + + return user_filter; +}; + return exports; }());