diff --git a/.github/workflows/js-unittest.yml b/.github/workflows/js-unittest.yml index 5501fef..07438f8 100644 --- a/.github/workflows/js-unittest.yml +++ b/.github/workflows/js-unittest.yml @@ -16,7 +16,9 @@ jobs: - name: Install apt updates run: sudo apt -y update; sudo apt -y upgrade; - name: Install prerequisites - run: sudo apt install -y nodejs npm; + uses: actions/setup-node@v3 + with: + node-version: 18 - name: Checkout uses: actions/checkout@v3 - name: Install dependencies diff --git a/package.json b/package.json index b608888..85da852 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "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", "lintHTML": "htmlhint '**/*.html'", @@ -12,6 +13,7 @@ "eslint": "^8.27.0", "htmlhint": "1.1.4", "mocha": "10", + "mock-local-storage": "^1.1.23", "stylelint": "14.14.1", "stylelint-config-standard": "^29.0.0" } diff --git a/static/reset.css b/static/reset.css index 1517822..bb75762 100644 --- a/static/reset.css +++ b/static/reset.css @@ -58,25 +58,25 @@ table { } input, select { - vertical-align :middle; + vertical-align: middle; } img, fieldset, object { - border:none; + border: none; } *, *:after, *:before { - box-sizing:border-box; + box-sizing: border-box; } button, label { - cursor:pointer; + cursor: pointer; } html, body { - height:100%; + height: 100%; } form { - border + border: solid; } \ No newline at end of file diff --git a/testenv/testenv.js b/testenv/testenv.js deleted file mode 100644 index e62cfc3..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 efa5739..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 7b953de..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