node_tests: Make localStorage mock common.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-04-09 17:16:59 -07:00 committed by Steve Howell
parent e2a3fe0930
commit 8aed6321bd
4 changed files with 22 additions and 50 deletions

View File

@ -7,23 +7,8 @@ const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {user_settings} = require("../zjsunit/zpage_params");
const ls_container = new Map();
const noop = () => {};
const localStorage = set_global("localStorage", {
getItem(key) {
return ls_container.get(key);
},
setItem(key, val) {
ls_container.set(key, val);
},
removeItem(key) {
ls_container.delete(key);
},
clear() {
ls_container.clear();
},
});
const setTimeout_delay = 3000;
set_global("setTimeout", (f, delay) => {
assert.equal(delay, setTimeout_delay);
@ -106,7 +91,7 @@ const short_msg = {
function test(label, f) {
run_test(label, ({override, override_rewire, mock_template}) => {
$("#draft_overlay").css = () => {};
localStorage.clear();
window.localStorage.clear();
f({override, override_rewire, mock_template});
});
}

View File

@ -10,22 +10,6 @@ const {page_params} = require("../zjsunit/zpage_params");
page_params.is_spectator = false;
const ls_container = new Map();
const localStorage = set_global("localStorage", {
getItem(key) {
return ls_container.get(key);
},
setItem(key, val) {
ls_container.set(key, val);
},
removeItem(key) {
ls_container.delete(key);
},
clear() {
ls_container.clear();
},
});
// Dependencies
set_global("document", {
hasFocus() {
@ -40,7 +24,7 @@ const util = zrequire("util");
function test(label, f) {
run_test(label, ({override}) => {
localStorage.clear();
window.localStorage.clear();
f({override});
});
}

View File

@ -2,7 +2,7 @@
const {strict: assert} = require("assert");
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
@ -146,22 +146,6 @@ mock_esm("../../static/js/unread", {
},
});
const ls_container = new Map();
set_global("localStorage", {
getItem(key) {
return ls_container.get(key);
},
setItem(key, val) {
ls_container.set(key, val);
},
removeItem(key) {
ls_container.delete(key);
},
clear() {
ls_container.clear();
},
});
const {all_messages_data} = zrequire("all_messages_data");
const people = zrequire("people");
const rt = zrequire("recent_topics_ui");

View File

@ -51,6 +51,23 @@ const window = new Proxy(global, {
},
});
const ls_container = new Map();
const localStorage = {
getItem(key) {
return ls_container.get(key);
},
setItem(key, val) {
ls_container.set(key, val);
},
/* istanbul ignore next */
removeItem(key) {
ls_container.delete(key);
},
clear() {
ls_container.clear();
},
};
// Set up Handlebars
handlebars.hook_require();
@ -90,6 +107,8 @@ try {
window.location.href = "http://zulip.zulipdev.com/#";
namespace.set_global("setTimeout", noop);
namespace.set_global("setInterval", noop);
namespace.set_global("localStorage", localStorage);
ls_container.clear();
_.throttle = immediate;
_.debounce = immediate;
zpage_params.reset();