2021-03-22 16:09:12 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2021-03-16 07:22:28 +01:00
|
|
|
const {make_stub} = require("../zjsunit/stub");
|
2021-03-22 16:09:12 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
|
|
|
|
|
|
|
const browser_history = zrequire("browser_history");
|
|
|
|
|
|
|
|
const location = set_global("location", {
|
|
|
|
hash: "bogus",
|
|
|
|
});
|
|
|
|
|
|
|
|
function test(label, f) {
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test(label, ({override}) => {
|
2021-03-22 16:09:12 +01:00
|
|
|
location.hash = "bogus";
|
|
|
|
browser_history.clear_for_testing();
|
2021-06-16 14:38:37 +02:00
|
|
|
f({override});
|
2021-03-22 16:09:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test("basics", () => {
|
2021-06-17 18:42:31 +02:00
|
|
|
const hash1 = "#settings/profile";
|
2021-03-22 16:09:12 +01:00
|
|
|
const hash2 = "#narrow/is/private";
|
|
|
|
browser_history.go_to_location(hash1);
|
|
|
|
assert.equal(location.hash, hash1);
|
|
|
|
|
|
|
|
browser_history.update(hash2);
|
|
|
|
assert.equal(location.hash, hash2);
|
|
|
|
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);
|
|
|
|
assert.equal(location.hash, hash);
|
|
|
|
browser_history.update(hash);
|
|
|
|
assert.equal(location.hash, hash);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("error for bad hashes", () => {
|
|
|
|
const hash = "bogus";
|
|
|
|
blueslip.expect("error", "programming error: prefix hashes with #: bogus");
|
|
|
|
browser_history.update(hash);
|
|
|
|
});
|
2021-03-16 07:22:28 +01:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("update internal hash if required", ({override}) => {
|
2021-03-16 07:22:28 +01:00
|
|
|
const hash = "#test/hash";
|
|
|
|
const stub = make_stub();
|
|
|
|
override(browser_history, "update", stub.f);
|
|
|
|
browser_history.update_hash_internally_if_required(hash);
|
|
|
|
assert.equal(stub.num_calls, 1);
|
|
|
|
|
|
|
|
location.hash = "#test/hash";
|
|
|
|
// 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
|
|
|
|
|
|
|
test("web public view hash restore", () => {
|
|
|
|
const allowed_hash = "#";
|
|
|
|
browser_history.update(allowed_hash);
|
|
|
|
assert.equal(location.hash, allowed_hash);
|
|
|
|
const new_hash = "#narrow/is/private";
|
|
|
|
browser_history.update(new_hash);
|
|
|
|
assert.equal(location.hash, new_hash);
|
|
|
|
browser_history.return_to_web_public_hash();
|
|
|
|
assert.equal(location.hash, allowed_hash);
|
|
|
|
});
|