2020-07-15 01:29:15 +02:00
|
|
|
set_global("$", global.make_zjquery());
|
|
|
|
const window_stub = $.create("window-stub");
|
|
|
|
set_global("location", {
|
|
|
|
protocol: "http:",
|
|
|
|
host: "example.com",
|
2018-05-31 23:55:28 +02:00
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("to_$", () => window_stub);
|
|
|
|
|
|
|
|
zrequire("people");
|
|
|
|
zrequire("hash_util");
|
|
|
|
zrequire("hashchange");
|
|
|
|
zrequire("stream_data");
|
|
|
|
|
|
|
|
set_global("document", "document-stub");
|
|
|
|
set_global("history", {});
|
|
|
|
|
|
|
|
set_global("admin", {});
|
|
|
|
set_global("drafts", {});
|
|
|
|
set_global("favicon", {});
|
|
|
|
set_global("floating_recipient_bar", {});
|
|
|
|
set_global("info_overlay", {});
|
|
|
|
set_global("message_viewport", {});
|
|
|
|
set_global("narrow", {});
|
|
|
|
set_global("overlays", {});
|
|
|
|
set_global("settings", {});
|
|
|
|
set_global("subs", {});
|
|
|
|
set_global("ui_util", {});
|
|
|
|
|
|
|
|
run_test("operators_round_trip", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators;
|
|
|
|
let hash;
|
|
|
|
let narrow;
|
2014-02-19 22:19:16 +01:00
|
|
|
|
|
|
|
operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "devel"},
|
|
|
|
{operator: "topic", operand: "algol"},
|
2014-02-19 22:19:16 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(hash, "#narrow/stream/devel/topic/algol");
|
2014-02-19 22:19:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
narrow = hash_util.parse_narrow(hash.split("/"));
|
2017-01-05 22:04:33 +01:00
|
|
|
assert.deepEqual(narrow, [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "devel", negated: false},
|
|
|
|
{operator: "topic", operand: "algol", negated: false},
|
2017-01-05 22:04:33 +01:00
|
|
|
]);
|
|
|
|
|
2014-02-19 22:19:16 +01:00
|
|
|
operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "devel"},
|
|
|
|
{operator: "topic", operand: "visual c++", negated: true},
|
2014-02-19 22:19:16 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(hash, "#narrow/stream/devel/-topic/visual.20c.2B.2B");
|
2017-01-05 22:04:33 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
narrow = hash_util.parse_narrow(hash.split("/"));
|
2017-01-05 22:04:33 +01:00
|
|
|
assert.deepEqual(narrow, [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "devel", negated: false},
|
|
|
|
{operator: "topic", operand: "visual c++", negated: true},
|
2017-01-05 22:04:33 +01:00
|
|
|
]);
|
|
|
|
|
2018-02-15 21:02:47 +01:00
|
|
|
// test new encodings, where we have a stream id
|
2019-11-02 00:06:25 +01:00
|
|
|
const florida_stream = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "Florida, USA",
|
2018-02-15 21:02:47 +01:00
|
|
|
stream_id: 987,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(florida_stream);
|
2018-02-15 21:02:47 +01:00
|
|
|
operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "Florida, USA"},
|
2018-02-15 21:02:47 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(hash, "#narrow/stream/987-Florida.2C-USA");
|
|
|
|
narrow = hash_util.parse_narrow(hash.split("/"));
|
2018-02-15 21:02:47 +01:00
|
|
|
assert.deepEqual(narrow, [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "Florida, USA", negated: false},
|
2018-05-15 15:59:50 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("operators_trailing_slash", () => {
|
|
|
|
const hash = "#narrow/stream/devel/topic/algol/";
|
|
|
|
const narrow = hash_util.parse_narrow(hash.split("/"));
|
2018-05-15 15:59:50 +02:00
|
|
|
assert.deepEqual(narrow, [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "stream", operand: "devel", negated: false},
|
|
|
|
{operator: "topic", operand: "algol", negated: false},
|
2018-02-15 21:02:47 +01:00
|
|
|
]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-01-19 03:53:50 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("people_slugs", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators;
|
|
|
|
let hash;
|
2017-01-19 03:53:50 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@example.com",
|
2017-01-19 03:53:50 +01:00
|
|
|
user_id: 42,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Smith",
|
2017-01-19 03:53:50 +01:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2017-01-19 03:53:50 +01:00
|
|
|
operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "sender", operand: "alice@example.com"},
|
2017-01-19 03:53:50 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(hash, "#narrow/sender/42-alice");
|
|
|
|
const narrow = hash_util.parse_narrow(hash.split("/"));
|
2017-01-19 03:53:50 +01:00
|
|
|
assert.deepEqual(narrow, [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "sender", operand: "alice@example.com", negated: false},
|
2017-01-19 03:53:50 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "pm-with", operand: "alice@example.com"},
|
2017-01-19 03:53:50 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(hash, "#narrow/pm-with/42-alice");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
function test_helper() {
|
2019-11-02 00:06:25 +01:00
|
|
|
let events = [];
|
|
|
|
let narrow_terms;
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
function stub(module_name, func_name) {
|
|
|
|
global[module_name][func_name] = () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
events.push(module_name + "." + func_name);
|
2018-04-11 13:14:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
stub("admin", "launch");
|
|
|
|
stub("drafts", "launch");
|
|
|
|
stub("floating_recipient_bar", "update");
|
|
|
|
stub("message_viewport", "stop_auto_scrolling");
|
|
|
|
stub("narrow", "deactivate");
|
|
|
|
stub("overlays", "close_for_hash_change");
|
|
|
|
stub("settings", "launch");
|
|
|
|
stub("subs", "launch");
|
|
|
|
stub("ui_util", "blur_active_element");
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
ui_util.change_tab_to = (hash) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
events.push("change_tab_to " + hash);
|
2018-04-11 13:14:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
narrow.activate = (terms) => {
|
|
|
|
narrow_terms = terms;
|
2020-07-15 01:29:15 +02:00
|
|
|
events.push("narrow.activate");
|
2018-04-11 13:14:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
info_overlay.show = (name) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
events.push("info: " + name);
|
2018-04-11 13:14:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
clear_events: () => {
|
|
|
|
events = [];
|
|
|
|
},
|
|
|
|
assert_events: (expected_events) => {
|
|
|
|
assert.deepEqual(expected_events, events);
|
|
|
|
},
|
2020-07-02 01:41:40 +02:00
|
|
|
get_narrow_terms: () => narrow_terms,
|
2018-04-11 13:14:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("hash_interactions", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = test_helper();
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
hashchange.initialize();
|
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"change_tab_to #home",
|
|
|
|
"narrow.deactivate",
|
|
|
|
"floating_recipient_bar.update",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"change_tab_to #home",
|
|
|
|
"narrow.deactivate",
|
|
|
|
"floating_recipient_bar.update",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#narrow/stream/Denmark";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"change_tab_to #home",
|
|
|
|
"narrow.activate",
|
|
|
|
"floating_recipient_bar.update",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
2019-11-02 00:06:25 +01:00
|
|
|
let terms = helper.get_narrow_terms();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(terms[0].operand, "Denmark");
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#narrow";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"change_tab_to #home",
|
|
|
|
"narrow.activate",
|
|
|
|
"floating_recipient_bar.update",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
terms = helper.get_narrow_terms();
|
|
|
|
assert.equal(terms.length, 0);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#streams/whatever";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"subs.launch",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#keyboard-shortcuts/whatever";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"info: keyboard-shortcuts",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#message-formatting/whatever";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"info: message-formatting",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#search-operators/whatever";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"message_viewport.stop_auto_scrolling",
|
|
|
|
"info: search-operators",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#drafts";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"drafts.launch",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#settings/alert-words";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"settings.launch",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.hash = "#organization/user-list-admin";
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(window).trigger($.Event("hashchange", {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"overlays.close_for_hash_change",
|
|
|
|
"admin.launch",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let called_back;
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
hashchange.exit_overlay(() => {
|
|
|
|
called_back = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"ui_util.blur_active_element",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
assert(called_back);
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("save_narrow", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = test_helper();
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "is", operand: "private"},
|
2018-04-11 13:14:39 +02:00
|
|
|
];
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("warn", "browser does not support pushState");
|
2018-04-11 13:14:39 +02:00
|
|
|
hashchange.save_narrow(operators);
|
2018-12-06 22:59:05 +01:00
|
|
|
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"message_viewport.stop_auto_scrolling",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(window.location.hash, "#narrow/is/private");
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let url_pushed;
|
2018-04-11 13:14:39 +02:00
|
|
|
global.history.pushState = (state, title, url) => {
|
|
|
|
url_pushed = url;
|
|
|
|
};
|
|
|
|
|
|
|
|
operators = [
|
2020-07-15 01:29:15 +02:00
|
|
|
{operator: "is", operand: "starred"},
|
2018-04-11 13:14:39 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
hashchange.save_narrow(operators);
|
|
|
|
helper.assert_events([
|
2020-07-15 01:29:15 +02:00
|
|
|
"message_viewport.stop_auto_scrolling",
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(url_pushed, "http://example.com/#narrow/is/starred");
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|