From 8b2ae89bd261be31a1cb75bbe68ae189b2f33313 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 11 Nov 2022 02:39:40 +0000 Subject: [PATCH] switch localStorage mock to mock-local-storage, fix unit tests to work with ES6 import syntax, remove testenv module --- package.json | 6 +- source/assets/scripts/localStorage.js | 3 - source/assets/scripts/localStorage.test.js | 13 ++-- source/assets/scripts/testenv.js | 28 -------- testenv/testenv.js | 28 -------- testenv/testenv.test.js | 81 ---------------------- testenv/testenv_helpers.js | 17 ----- 7 files changed, 10 insertions(+), 166 deletions(-) delete mode 100644 source/assets/scripts/testenv.js delete mode 100644 testenv/testenv.js delete mode 100644 testenv/testenv.test.js delete mode 100644 testenv/testenv_helpers.js diff --git a/package.json b/package.json index 0d02693..c1b3cec 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { "name": "food-journal", "version": "1.0.0", + "type": "module", "scripts": { - "test": "mocha --recursive './{,!(node_modules)/**}/*.test.js'", + "test": "mocha --recursive --require mock-local-storage './{,!(node_modules)/**}/*.test.js'", "lint": "eslint **/*.js", "fix-style": "eslint --fix '**/*.js'" }, "devDependencies": { "eslint": "^8.27.0", - "mocha": "10" + "mocha": "10", + "mock-local-storage": "^1.1.23" } } diff --git a/source/assets/scripts/localStorage.js b/source/assets/scripts/localStorage.js index 7495993..29a72ce 100644 --- a/source/assets/scripts/localStorage.js +++ b/source/assets/scripts/localStorage.js @@ -1,6 +1,3 @@ -// -//module.exports = {getReviewsFromStorage, saveReviewsToStorage}; - /** * @returns {Array} An array of reviews found in localStorage */ diff --git a/source/assets/scripts/localStorage.test.js b/source/assets/scripts/localStorage.test.js index ae9554b..ed11bb5 100644 --- a/source/assets/scripts/localStorage.test.js +++ b/source/assets/scripts/localStorage.test.js @@ -1,11 +1,9 @@ -const {getReviewsFromStorage, saveReviewsToStorage} = require("./localStorage"); -const {environment} = require("./testenv"); -const assert = require("assert"); -const {describe, it, beforeEach} = require("mocha"); +import {strict as assert} from "node:assert" +import {describe, it, beforeEach} from "mocha"; +import {saveReviewsToStorage, getReviewsFromStorage} from "./localStorage.js"; beforeEach(() => { - window = environment(); - localStorage = window.localStorage; + localStorage.clear(); }); describe("test app localStorage interaction", () => { @@ -26,7 +24,8 @@ describe("test app localStorage interaction", () => { assert.deepEqual(getReviewsFromStorage(), reviews); }); it("repeated store one more and get", () => { - saveReviewsToStorage(reviews); + let reviews = []; + assert.deepEqual(getReviewsFromStorage(), reviews); for(let i = 0; i < 1000; i++){ diff --git a/source/assets/scripts/testenv.js b/source/assets/scripts/testenv.js deleted file mode 100644 index 01b6e4c..0000000 --- a/source/assets/scripts/testenv.js +++ /dev/null @@ -1,28 +0,0 @@ -module.exports = {environment}; - -function environment () { - const localStorageMock = (function () { - let store = {}; - return { - getItem(key) { - return store[key]; - }, - setItem(key, value) { - store[key] = value; - }, - clear() { - store = {}; - }, - removeItem(key) { - delete store[key]; - }, - getAll() { - return store; - }, - }; - })(); - - let window = {}; - Object.defineProperty(window, "localStorage", { value: localStorageMock }); - return window; -} \ No newline at end of file diff --git a/testenv/testenv.js b/testenv/testenv.js deleted file mode 100644 index 01b6e4c..0000000 --- a/testenv/testenv.js +++ /dev/null @@ -1,28 +0,0 @@ -module.exports = {environment}; - -function environment () { - const localStorageMock = (function () { - let store = {}; - return { - getItem(key) { - return store[key]; - }, - setItem(key, value) { - store[key] = value; - }, - clear() { - store = {}; - }, - removeItem(key) { - delete store[key]; - }, - getAll() { - return store; - }, - }; - })(); - - let window = {}; - Object.defineProperty(window, "localStorage", { value: localStorageMock }); - return window; -} \ No newline at end of file diff --git a/testenv/testenv.test.js b/testenv/testenv.test.js deleted file mode 100644 index 91bf30b..0000000 --- a/testenv/testenv.test.js +++ /dev/null @@ -1,81 +0,0 @@ -const {environment} = require("./testenv.js"); -var assert = require("assert"); -var {describe, it, beforeEach} = require("mocha"); -var {saveToLocal, getFromLocal, removeFromLocal, clearLocal} = require("./testenv_helpers"); - -beforeEach(() => { - window = environment(); -}); - -describe("test localStorage mock", () => { - it("test save and fetch", () => { - saveToLocal("testkey1", "testvalue1"); - saveToLocal("testkey2", "testvalue2"); - saveToLocal("testkey3", "testvalue3"); - saveToLocal("testkey4", "testvalue4"); - - assert.equal(getFromLocal("testkey1"), "testvalue1"); - assert.equal(getFromLocal("testkey2"), "testvalue2"); - assert.equal(getFromLocal("testkey3"), "testvalue3"); - assert.equal(getFromLocal("testkey4"), "testvalue4"); - - saveToLocal("testkey6", "testvalue5"); - assert.equal(getFromLocal("testkey6"), "testvalue5"); - }); - - it("test window locality", () => { - assert.equal(getFromLocal("testkey1"), null); - assert.equal(getFromLocal("testkey2"), null); - assert.equal(getFromLocal("testkey3"), null); - assert.equal(getFromLocal("testkey4"), null); - }); - - it("test delete and fetch", () => { - saveToLocal("testkey1", "testvalue1"); - saveToLocal("testkey2", "testvalue2"); - saveToLocal("testkey3", "testvalue3"); - saveToLocal("testkey4", "testvalue4"); - - removeFromLocal("testkey3"); - - assert.equal(getFromLocal("testkey1"), "testvalue1"); - assert.equal(getFromLocal("testkey2"), "testvalue2"); - assert.equal(getFromLocal("testkey3"), null); - assert.equal(getFromLocal("testkey4"), "testvalue4"); - - removeFromLocal("testkey1"); - - assert.equal(getFromLocal("testkey1"), null); - assert.equal(getFromLocal("testkey2"), "testvalue2"); - assert.equal(getFromLocal("testkey3"), null); - assert.equal(getFromLocal("testkey4"), "testvalue4"); - - removeFromLocal("testkey4"); - - assert.equal(getFromLocal("testkey1"), null); - assert.equal(getFromLocal("testkey2"), "testvalue2"); - assert.equal(getFromLocal("testkey3"), null); - assert.equal(getFromLocal("testkey4"), null); - - removeFromLocal("testkey2"); - - assert.equal(getFromLocal("testkey1"), null); - assert.equal(getFromLocal("testkey2"), null); - assert.equal(getFromLocal("testkey3"), null); - assert.equal(getFromLocal("testkey4"), null); - }); - - it("test clear and fetch", () => { - saveToLocal("testkey1", "testvalue1"); - saveToLocal("testkey2", "testvalue2"); - saveToLocal("testkey3", "testvalue3"); - saveToLocal("testkey4", "testvalue4"); - - clearLocal(); - - assert.equal(getFromLocal("testkey1"), null); - assert.equal(getFromLocal("testkey2"), null); - assert.equal(getFromLocal("testkey3"), null); - assert.equal(getFromLocal("testkey4"), null); - }); -}); \ No newline at end of file diff --git a/testenv/testenv_helpers.js b/testenv/testenv_helpers.js deleted file mode 100644 index 469adc5..0000000 --- a/testenv/testenv_helpers.js +++ /dev/null @@ -1,17 +0,0 @@ -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