diff --git a/frontend_tests/node_tests/search.js b/frontend_tests/node_tests/search.js index b1f2bb0ed4..8cf9428e8c 100644 --- a/frontend_tests/node_tests/search.js +++ b/frontend_tests/node_tests/search.js @@ -20,6 +20,7 @@ set_global('ui_util', { set_global('narrow', {}); search_pill.append_search_string = noop; +search_pill.get_search_string_for_current_filter = noop; global.patch_builtin('setTimeout', func => func()); @@ -139,7 +140,7 @@ run_test('initizalize', () => { assert.deepEqual(options, {trigger: 'search'}); }; search_pill.get_search_string_for_current_filter = () => { - return ''; + return search_box_val; }; }; @@ -150,7 +151,7 @@ run_test('initizalize', () => { }]; _setup('ver'); opts.updater('ver'); - assert(is_blurred); + assert(!is_blurred); assert(is_append_search_string_called); operators = [{ @@ -160,7 +161,7 @@ run_test('initizalize', () => { }]; _setup('stream:Verona'); opts.updater('stream:Verona'); - assert(is_blurred); + assert(!is_blurred); assert(is_append_search_string_called); search.is_using_input_method = true; @@ -206,7 +207,7 @@ run_test('initizalize', () => { assert.deepEqual(options, {trigger: 'search'}); }; search_pill.get_search_string_for_current_filter = () => { - return ''; + return search_box_val; }; }; diff --git a/static/js/search.js b/static/js/search.js index d1121d5534..58cd7f0c9a 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -1,7 +1,7 @@ // Exported for unit testing exports.is_using_input_method = false; -function narrow_or_search_for_term(search_string) { +exports.narrow_or_search_for_term = function (search_string) { const search_query_box = $("#search_query"); if (exports.is_using_input_method) { // Neither narrow nor search when using input tools as @@ -13,14 +13,12 @@ function narrow_or_search_for_term(search_string) { let operators; if (page_params.search_pills_enabled) { - // search_string only contains the suggestion selected - // from the typeahead. base_query stores the query - // corresponding to the existing pills. + // We have to take care to append the new pill before calling this + // function, so that the base_query includes the suggestion selected + // along with query corresponding to the existing pills. const base_query = search_pill.get_search_string_for_current_filter( search_pill_widget.widget); - const base_operators = Filter.parse(base_query); - const suggestion_operator = Filter.parse(search_string); - operators = base_operators.concat(suggestion_operator); + operators = Filter.parse(base_query); } else { operators = Filter.parse(search_string); } @@ -32,9 +30,11 @@ function narrow_or_search_for_term(search_string) { // Narrowing will have already put some operators in the search box, // so leave the current text in. - search_query_box.blur(); + if (!page_params.search_pills_enabled) { + search_query_box.blur(); + } return search_query_box.val(); -} +}; function update_buttons_with_focus(focused) { const search_query_box = $('#search_query'); @@ -90,22 +90,11 @@ exports.initialize = function () { return true; }, updater: function (search_string) { - // Order is important here. narrow_or_search_for_term - // gets a search string from existing pills and obtains - // existing operators. Newly selected suggestion is added - // to those operators. If narrow_or_search_for_term was - // called after append_search_string, the existing search - // pills at the time for calling that function would also - // have the newly selected suggestion, and appending it again - // would cause duplication. - const result = narrow_or_search_for_term(search_string); if (page_params.search_pills_enabled) { search_pill.append_search_string(search_string, search_pill_widget.widget); - $("#search_query").focus(); - } else { - return result; } + return exports.narrow_or_search_for_term(search_string); }, sorter: function (items) { return items; @@ -150,16 +139,8 @@ exports.initialize = function () { // this codepath is that they first all blur the box to // indicate that they've done what they need to do) - let operators = Filter.parse(search_query_box.val()); - if (page_params.search_pills_enabled) { - // Pill is already added during keydown event of input pills. - // Thus we can't call narrow_or_search_for_term as the - // search_query_box has empty value. - const base_query = search_pill.get_search_string_for_current_filter( - search_pill_widget.widget); - operators = Filter.parse(base_query); - } - narrow.activate(operators, {trigger: 'search'}); + // Pill is already added during keydown event of input pills. + exports.narrow_or_search_for_term(search_query_box.val()); search_query_box.blur(); update_buttons_with_focus(false); } diff --git a/static/js/search_pill_widget.js b/static/js/search_pill_widget.js index cd53b28262..1625a29d1b 100644 --- a/static/js/search_pill_widget.js +++ b/static/js/search_pill_widget.js @@ -6,9 +6,7 @@ exports.initialize = function () { exports.widget = search_pill.create_pills(container); exports.widget.onPillRemove(function () { - const base_query = search_pill.get_search_string_for_current_filter(exports.widget); - const operators = Filter.parse(base_query); - narrow.activate(operators, {trigger: 'search'}); + search.narrow_or_search_for_term(); }); };