hash_util: Extract functions to be reused without window.

These functions can now be used in other libraries without
using window.location.hash in them and directly calling these
functions.
This commit is contained in:
Aman Agrawal
2021-03-23 04:14:54 +00:00
committed by Tim Abbott
parent d8db797798
commit e7129af8ac
3 changed files with 17 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ const ui_report = mock_esm("../../static/js/ui_report", {
ui_report.displayed_error = true;
},
});
set_global("location", {
const location = set_global("location", {
protocol: "https:",
host: "example.com",
pathname: "/",
@@ -87,6 +87,9 @@ run_test("test_get_hash_category", () => {
assert.deepEqual(hash_util.get_hash_category("#settings/display-settings"), "settings");
assert.deepEqual(hash_util.get_hash_category("#drafts"), "drafts");
assert.deepEqual(hash_util.get_hash_category("invites"), "invites");
location.hash = "#settings/your-account";
assert.deepEqual(hash_util.get_current_hash_category(), "settings");
});
run_test("test_get_hash_section", () => {
@@ -97,6 +100,9 @@ run_test("test_get_hash_section", () => {
assert.equal(hash_util.get_hash_section("#drafts"), "");
assert.equal(hash_util.get_hash_section(""), "");
location.hash = "#settings/your-account";
assert.deepEqual(hash_util.get_current_hash_section(), "your-account");
});
run_test("test_parse_narrow", () => {

View File

@@ -23,6 +23,14 @@ export function get_hash_section(hash) {
return parts[1] || "";
}
export function get_current_hash_category() {
return get_hash_category(window.location.hash);
}
export function get_current_hash_section() {
return get_hash_section(window.location.hash);
}
const hashReplacements = new Map([
["%", "."],
["(", ".28"],

View File

@@ -171,9 +171,9 @@ function do_hashchange_overlay(old_hash) {
// background and recent topics seems preferable for that.
recent_topics.show();
}
const base = hash_util.get_hash_category(window.location.hash);
const base = hash_util.get_current_hash_category();
const old_base = hash_util.get_hash_category(old_hash);
const section = hash_util.get_hash_section(window.location.hash);
const section = hash_util.get_current_hash_section();
const coming_from_overlay = hash_util.is_overlay_hash(old_hash || "#");