2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("page_params", {
|
2018-07-14 12:46:02 +02:00
|
|
|
search_pills_enabled: false,
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("search");
|
2020-07-08 23:44:01 +02:00
|
|
|
zrequire("message_view_header");
|
2018-07-14 12:46:02 +02:00
|
|
|
|
|
|
|
const noop = () => {};
|
|
|
|
const return_true = () => true;
|
|
|
|
const return_false = () => false;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("$", global.make_zjquery());
|
|
|
|
set_global("narrow_state", {});
|
|
|
|
set_global("search_suggestion", {});
|
|
|
|
set_global("ui_util", {
|
2018-07-14 12:46:02 +02:00
|
|
|
change_tab_to: noop,
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("narrow", {});
|
|
|
|
set_global("Filter", {});
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
global.patch_builtin("setTimeout", (func) => func());
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("update_button_visibility", () => {
|
|
|
|
const search_query = $("#search_query");
|
|
|
|
const search_button = $(".search_button");
|
2018-07-14 12:46:02 +02:00
|
|
|
|
|
|
|
search_query.is = return_false;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_query.val("");
|
2018-07-14 12:46:02 +02:00
|
|
|
narrow_state.active = return_false;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_button.prop("disabled", true);
|
2020-05-31 16:47:55 +02:00
|
|
|
search.update_button_visibility();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(search_button.prop("disabled"));
|
2018-07-14 12:46:02 +02:00
|
|
|
|
|
|
|
search_query.is = return_true;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_query.val("");
|
2018-07-14 12:46:02 +02:00
|
|
|
narrow_state.active = return_false;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_button.prop("disabled", true);
|
2018-07-14 12:46:02 +02:00
|
|
|
search.update_button_visibility();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!search_button.prop("disabled"));
|
2018-07-14 12:46:02 +02:00
|
|
|
|
|
|
|
search_query.is = return_false;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_query.val("Test search term");
|
2018-07-14 12:46:02 +02:00
|
|
|
narrow_state.active = return_false;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_button.prop("disabled", true);
|
2018-07-14 12:46:02 +02:00
|
|
|
search.update_button_visibility();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!search_button.prop("disabled"));
|
2018-07-14 12:46:02 +02:00
|
|
|
|
|
|
|
search_query.is = return_false;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_query.val("");
|
2018-07-14 12:46:02 +02:00
|
|
|
narrow_state.active = return_true;
|
2020-07-15 01:29:15 +02:00
|
|
|
search_button.prop("disabled", true);
|
2018-07-14 12:46:02 +02:00
|
|
|
search.update_button_visibility();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!search_button.prop("disabled"));
|
2018-07-14 12:46:02 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("initialize", () => {
|
|
|
|
const search_query_box = $("#search_query");
|
|
|
|
const searchbox_form = $("#searchbox_form");
|
|
|
|
const search_button = $(".search_button");
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2019-12-25 16:58:11 +01:00
|
|
|
search_suggestion.max_num_of_search_results = 999;
|
2018-07-14 12:46:02 +02:00
|
|
|
search_query_box.typeahead = (opts) => {
|
|
|
|
assert.equal(opts.fixed, true);
|
2019-12-25 16:58:11 +01:00
|
|
|
assert.equal(opts.items, 999);
|
2018-07-14 12:46:02 +02:00
|
|
|
assert.equal(opts.naturalSearch, true);
|
|
|
|
assert.equal(opts.helpOnEmptyStrings, true);
|
|
|
|
assert.equal(opts.matcher(), true);
|
|
|
|
|
|
|
|
{
|
|
|
|
const search_suggestions = {
|
2020-02-12 06:58:20 +01:00
|
|
|
lookup_table: new Map([
|
2020-07-15 00:34:28 +02:00
|
|
|
[
|
|
|
|
"stream:Verona",
|
|
|
|
{
|
|
|
|
description: "Stream <strong>Ver</strong>ona",
|
|
|
|
search_string: "stream:Verona",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"ver",
|
|
|
|
{
|
|
|
|
description: "Search for ver",
|
|
|
|
search_string: "ver",
|
|
|
|
},
|
|
|
|
],
|
2020-02-12 06:58:20 +01:00
|
|
|
]),
|
2020-07-15 01:29:15 +02:00
|
|
|
strings: ["ver", "stream:Verona"],
|
2018-07-14 12:46:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Test source */
|
2020-06-01 15:00:42 +02:00
|
|
|
search_suggestion.get_suggestions = () => search_suggestions;
|
2018-07-14 12:46:02 +02:00
|
|
|
const expected_source_value = search_suggestions.strings;
|
2020-07-15 01:29:15 +02:00
|
|
|
const source = opts.source("ver");
|
2018-07-14 12:46:02 +02:00
|
|
|
assert.equal(source, expected_source_value);
|
|
|
|
|
|
|
|
/* Test highlighter */
|
2020-07-15 01:29:15 +02:00
|
|
|
let expected_value = "Search for ver";
|
2018-07-14 12:46:02 +02:00
|
|
|
assert.equal(opts.highlighter(source[0]), expected_value);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
expected_value = "Stream <strong>Ver</strong>ona";
|
2018-07-14 12:46:02 +02:00
|
|
|
assert.equal(opts.highlighter(source[1]), expected_value);
|
|
|
|
|
|
|
|
/* Test sorter */
|
|
|
|
assert.equal(opts.sorter(search_suggestions.strings), search_suggestions.strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let operators;
|
|
|
|
let is_blurred;
|
2020-07-21 00:23:06 +02:00
|
|
|
search_query_box.on("blur", () => {
|
2018-07-14 12:46:02 +02:00
|
|
|
is_blurred = true;
|
2020-07-21 00:23:06 +02:00
|
|
|
});
|
2018-07-14 12:46:02 +02:00
|
|
|
/* Test updater */
|
|
|
|
const _setup = (search_box_val) => {
|
|
|
|
is_blurred = false;
|
|
|
|
search_query_box.val(search_box_val);
|
|
|
|
Filter.parse = (search_string) => {
|
|
|
|
assert.equal(search_string, search_box_val);
|
|
|
|
return operators;
|
|
|
|
};
|
|
|
|
narrow.activate = (raw_operators, options) => {
|
|
|
|
assert.deepEqual(raw_operators, operators);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.deepEqual(options, {trigger: "search"});
|
2018-07-14 12:46:02 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
operators = [
|
|
|
|
{
|
|
|
|
negated: false,
|
|
|
|
operator: "search",
|
|
|
|
operand: "ver",
|
|
|
|
},
|
|
|
|
];
|
2020-07-15 01:29:15 +02:00
|
|
|
_setup("ver");
|
|
|
|
assert.equal(opts.updater("ver"), "ver");
|
2018-07-14 12:46:02 +02:00
|
|
|
assert(is_blurred);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
operators = [
|
|
|
|
{
|
|
|
|
negated: false,
|
|
|
|
operator: "stream",
|
|
|
|
operand: "Verona",
|
|
|
|
},
|
|
|
|
];
|
2020-07-15 01:29:15 +02:00
|
|
|
_setup("stream:Verona");
|
|
|
|
assert.equal(opts.updater("stream:Verona"), "stream:Verona");
|
2018-07-14 12:46:02 +02:00
|
|
|
assert(is_blurred);
|
|
|
|
|
|
|
|
search.is_using_input_method = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
_setup("stream:Verona");
|
|
|
|
assert.equal(opts.updater("stream:Verona"), "stream:Verona");
|
2018-07-14 12:46:02 +02:00
|
|
|
assert(!is_blurred);
|
2020-07-21 00:23:06 +02:00
|
|
|
|
|
|
|
search_query_box.off("blur");
|
2018-07-14 12:46:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
search.initialize();
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
search_button.prop("disabled", true);
|
|
|
|
search_query_box.trigger("focus");
|
|
|
|
assert(!search_button.prop("disabled"));
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
search_query_box.val("test string");
|
|
|
|
narrow_state.search_string = () => "ver";
|
|
|
|
search_query_box.trigger("blur");
|
|
|
|
assert.equal(search_query_box.val(), "test string");
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
search.is_using_input_method = false;
|
|
|
|
searchbox_form.trigger("compositionend");
|
|
|
|
assert(search.is_using_input_method);
|
|
|
|
|
|
|
|
const keydown = searchbox_form.get_on_handler("keydown");
|
|
|
|
let ev = {
|
|
|
|
type: "keydown",
|
|
|
|
which: 15,
|
2018-07-14 12:46:02 +02:00
|
|
|
};
|
2020-07-21 00:23:06 +02:00
|
|
|
search_query_box.is = return_false;
|
|
|
|
assert.equal(keydown(ev), undefined);
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
ev.which = 13;
|
|
|
|
assert.equal(keydown(ev), undefined);
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_true;
|
|
|
|
assert.equal(keydown(ev), false);
|
2018-07-14 12:46:02 +02:00
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
ev = {
|
|
|
|
type: "keyup",
|
2018-07-14 12:46:02 +02:00
|
|
|
};
|
2020-07-21 00:23:06 +02:00
|
|
|
let operators;
|
|
|
|
let is_blurred;
|
|
|
|
narrow_state.active = return_false;
|
|
|
|
search_query_box.off("blur");
|
|
|
|
search_query_box.on("blur", () => {
|
|
|
|
is_blurred = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const _setup = (search_box_val) => {
|
|
|
|
is_blurred = false;
|
|
|
|
search_button.prop("disabled", false);
|
|
|
|
search_query_box.val(search_box_val);
|
|
|
|
Filter.parse = (search_string) => {
|
|
|
|
assert.equal(search_string, search_box_val);
|
|
|
|
return operators;
|
|
|
|
};
|
|
|
|
narrow.activate = (raw_operators, options) => {
|
|
|
|
assert.deepEqual(raw_operators, operators);
|
|
|
|
assert.deepEqual(options, {trigger: "search"});
|
|
|
|
};
|
2018-07-14 12:46:02 +02:00
|
|
|
};
|
|
|
|
|
2020-07-21 00:23:06 +02:00
|
|
|
operators = [
|
|
|
|
{
|
|
|
|
negated: false,
|
|
|
|
operator: "search",
|
|
|
|
operand: "",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
_setup("");
|
|
|
|
|
|
|
|
ev.which = 15;
|
|
|
|
search_query_box.is = return_false;
|
|
|
|
searchbox_form.trigger(ev);
|
|
|
|
|
|
|
|
assert(!is_blurred);
|
|
|
|
assert(!search_button.prop("disabled"));
|
|
|
|
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_false;
|
|
|
|
searchbox_form.trigger(ev);
|
|
|
|
|
|
|
|
assert(!is_blurred);
|
|
|
|
assert(!search_button.prop("disabled"));
|
|
|
|
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_true;
|
|
|
|
searchbox_form.trigger(ev);
|
|
|
|
assert(is_blurred);
|
|
|
|
|
|
|
|
_setup("ver");
|
|
|
|
search.is_using_input_method = true;
|
|
|
|
searchbox_form.trigger(ev);
|
|
|
|
// No change on enter keyup event when using input tool
|
|
|
|
assert(!is_blurred);
|
|
|
|
assert(!search_button.prop("disabled"));
|
|
|
|
|
|
|
|
_setup("ver");
|
|
|
|
ev.which = 13;
|
|
|
|
search_query_box.is = return_true;
|
|
|
|
searchbox_form.trigger(ev);
|
|
|
|
assert(is_blurred);
|
|
|
|
assert(!search_button.prop("disabled"));
|
2018-07-14 12:46:02 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("initiate_search", () => {
|
2020-04-12 15:21:11 +02:00
|
|
|
// open typeahead and select text when navbar is open
|
|
|
|
// this implicitly expects the code to used the chained
|
|
|
|
// function calls, which is something to keep in mind if
|
|
|
|
// this test ever fails unexpectedly.
|
2020-06-11 00:22:22 +02:00
|
|
|
narrow_state.filter = () => ({is_search: return_true});
|
2020-04-12 15:21:11 +02:00
|
|
|
let typeahead_forced_open = false;
|
|
|
|
let is_searchbox_text_selected = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#search_query").typeahead = (lookup) => {
|
2020-04-12 15:21:11 +02:00
|
|
|
if (lookup === "lookup") {
|
|
|
|
typeahead_forced_open = true;
|
|
|
|
}
|
2020-07-22 04:50:11 +02:00
|
|
|
return $("#search_query");
|
2018-07-14 12:46:02 +02:00
|
|
|
};
|
2020-07-22 04:50:11 +02:00
|
|
|
$("#search_query").on("select", () => {
|
|
|
|
is_searchbox_text_selected = true;
|
|
|
|
});
|
|
|
|
|
2018-07-14 12:46:02 +02:00
|
|
|
search.initiate_search();
|
2020-04-12 15:21:11 +02:00
|
|
|
assert(typeahead_forced_open);
|
|
|
|
assert(is_searchbox_text_selected);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#search_query").val(), "ver");
|
2020-06-11 00:22:22 +02:00
|
|
|
|
|
|
|
// test that we append space for user convenience
|
|
|
|
narrow_state.filter = () => ({is_search: return_false});
|
|
|
|
search.initiate_search();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($("#search_query").val(), "ver ");
|
2018-07-14 12:46:02 +02:00
|
|
|
});
|