From 3043c6e157cb4151003f81a36ed16656b87e9bee Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 8 Nov 2022 01:33:35 +0000 Subject: [PATCH] fix locality issue in mocha unit tests, move sample helper functions for localStorage unit testing Signed-off-by: Arthur Lu --- testenv/testenv.test.js | 28 ++++------------------------ testenv/testenv_helpers.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 24 deletions(-) create mode 100644 testenv/testenv_helpers.js diff --git a/testenv/testenv.test.js b/testenv/testenv.test.js index afa68bc..d274fb8 100644 --- a/testenv/testenv.test.js +++ b/testenv/testenv.test.js @@ -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"); diff --git a/testenv/testenv_helpers.js b/testenv/testenv_helpers.js new file mode 100644 index 0000000..469adc5 --- /dev/null +++ b/testenv/testenv_helpers.js @@ -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(); +} \ No newline at end of file