2021-03-22 16:09:12 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {make_stub} = require("./lib/stub");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
2021-03-22 16:09:12 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#bogus";
|
2021-03-22 16:09:12 +01:00
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
const browser_history = zrequire("browser_history");
|
2021-03-22 16:09:12 +01:00
|
|
|
|
|
|
|
function test(label, f) {
|
2022-01-08 10:27:06 +01:00
|
|
|
run_test(label, (...args) => {
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#bogus";
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.clear_for_testing();
|
2022-01-08 10:27:06 +01:00
|
|
|
f(...args);
|
2021-03-22 16:09:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("basics", () => {
|
2021-06-17 18:42:31 +02:00
|
|
|
const hash1 = "#settings/profile";
|
2023-04-07 14:03:34 +02:00
|
|
|
const hash2 = "#narrow/is/dm";
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.go_to_location(hash1);
|
2021-10-14 00:23:21 +02:00
|
|
|
assert.equal(window.location.hash, hash1);
|
2021-03-22 16:09:12 +01:00
|
|
|
|
|
|
|
browser_history.update(hash2);
|
2021-10-14 00:23:21 +02:00
|
|
|
assert.equal(window.location.hash, hash2);
|
2021-03-22 16:09:12 +01:00
|
|
|
assert.equal(browser_history.old_hash(), hash1);
|
|
|
|
|
|
|
|
const was_internal_change = browser_history.save_old_hash();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(was_internal_change);
|
2021-03-22 16:09:12 +01:00
|
|
|
assert.equal(browser_history.old_hash(), hash2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("update with same hash", () => {
|
|
|
|
const hash = "#keyboard-shortcuts";
|
|
|
|
|
|
|
|
browser_history.update(hash);
|
2021-10-14 00:23:21 +02:00
|
|
|
assert.equal(window.location.hash, hash);
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.update(hash);
|
2021-10-14 00:23:21 +02:00
|
|
|
assert.equal(window.location.hash, hash);
|
2021-03-22 16:09:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("error for bad hashes", () => {
|
|
|
|
const hash = "bogus";
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "programming error: prefix hashes with #");
|
2021-03-22 16:09:12 +01:00
|
|
|
browser_history.update(hash);
|
|
|
|
});
|
2021-03-16 07:22:28 +01:00
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
test("update internal hash if required", ({override_rewire}) => {
|
2021-03-16 07:22:28 +01:00
|
|
|
const hash = "#test/hash";
|
|
|
|
const stub = make_stub();
|
2022-01-08 10:27:06 +01:00
|
|
|
override_rewire(browser_history, "update", stub.f);
|
2021-03-16 07:22:28 +01:00
|
|
|
browser_history.update_hash_internally_if_required(hash);
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
|
2021-10-14 00:23:21 +02:00
|
|
|
window.location.hash = "#test/hash";
|
2021-03-16 07:22:28 +01:00
|
|
|
// update with same hash
|
|
|
|
browser_history.update_hash_internally_if_required(hash);
|
|
|
|
// but no update was made since the
|
|
|
|
// hash was already updated.
|
|
|
|
// Evident by no increase in number of
|
|
|
|
// calls to stub.
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
});
|
2021-05-19 11:17:33 +02:00
|
|
|
|
2022-01-29 00:54:13 +01:00
|
|
|
test("web-public view hash restore", () => {
|
2021-10-14 00:23:21 +02:00
|
|
|
browser_history.update("#");
|
|
|
|
assert.equal(window.location.hash, "");
|
2023-04-07 14:03:34 +02:00
|
|
|
const new_hash = "#narrow/is/dm";
|
2021-05-19 11:17:33 +02:00
|
|
|
browser_history.update(new_hash);
|
2021-10-14 00:23:21 +02:00
|
|
|
assert.equal(window.location.hash, new_hash);
|
2021-05-19 11:17:33 +02:00
|
|
|
browser_history.return_to_web_public_hash();
|
2021-10-14 00:23:21 +02:00
|
|
|
assert.equal(window.location.hash, "");
|
2021-05-19 11:17:33 +02:00
|
|
|
});
|