fix locality issue in mocha unit tests,

move sample helper functions for localStorage unit testing

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2022-11-08 01:33:35 +00:00
parent 0f622d16de
commit 3043c6e157
2 changed files with 21 additions and 24 deletions

View File

@ -1,27 +1,13 @@
const {environment} = require("./testenv.js");
var assert = require('assert');
var window;
var {saveToLocal, getFromLocal, removeFromLocal, clearLocal} = require('./testenv_helpers');
function saveToLocal (k, v) {
window.localStorage.setItem(k, v);
}
function getFromLocal (k) {
return window.localStorage.getItem(k);
}
function removeFromLocal (k) {
window.localStorage.removeItem(k);
}
function clearLocal () {
window.localStorage.clear();
}
beforeEach(() => {
window = environment();
});
describe("test localStorage mock", () => {
it("test save and fetch", () => {
window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
@ -37,8 +23,6 @@ describe("test localStorage mock", () => {
});
it("test window locality", () => {
window = environment();
assert.equal(getFromLocal("testkey1"), null);
assert.equal(getFromLocal("testkey2"), null);
assert.equal(getFromLocal("testkey3"), null);
@ -46,8 +30,6 @@ describe("test localStorage mock", () => {
});
it("test delete and fetch", () => {
window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
@ -83,8 +65,6 @@ describe("test localStorage mock", () => {
});
it("test clear and fetch", () => {
window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");

View File

@ -0,0 +1,17 @@
module.exports = {saveToLocal, getFromLocal, removeFromLocal, clearLocal};
function saveToLocal (k, v) {
window.localStorage.setItem(k, v);
}
function getFromLocal (k) {
return window.localStorage.getItem(k);
}
function removeFromLocal (k) {
window.localStorage.removeItem(k);
}
function clearLocal () {
window.localStorage.clear();
}