Merge pull request #47 from cse110-fa22-group29/fix-es6-compatibility

Fix JS unit test ES6 compatibility
This commit is contained in:
Arthur Lu 2022-11-11 08:58:39 -08:00 committed by GitHub
commit 0c0b49264e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 13 additions and 167 deletions

View File

@ -16,7 +16,9 @@ jobs:
- name: Install apt updates - name: Install apt updates
run: sudo apt -y update; sudo apt -y upgrade; run: sudo apt -y update; sudo apt -y upgrade;
- name: Install prerequisites - name: Install prerequisites
run: sudo apt install -y nodejs npm; uses: actions/setup-node@v3
with:
node-version: 18
- name: Checkout - name: Checkout
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Install dependencies - name: Install dependencies

View File

@ -1,13 +1,15 @@
{ {
"name": "food-journal", "name": "food-journal",
"version": "1.0.0", "version": "1.0.0",
"type": "module",
"scripts": { "scripts": {
"test": "mocha --recursive './{,!(node_modules)/**}/*.test.js'", "test": "mocha --recursive --require mock-local-storage './{,!(node_modules)/**}/*.test.js'",
"lint": "eslint **/*.js", "lint": "eslint **/*.js",
"fix-style": "eslint --fix '**/*.js'" "fix-style": "eslint --fix '**/*.js'"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^8.27.0", "eslint": "^8.27.0",
"mocha": "10" "mocha": "10",
"mock-local-storage": "^1.1.23"
} }
} }

View File

@ -1,6 +1,3 @@
//
//module.exports = {getReviewsFromStorage, saveReviewsToStorage};
/** /**
* @returns {Array<Object>} An array of reviews found in localStorage * @returns {Array<Object>} An array of reviews found in localStorage
*/ */

View File

@ -1,11 +1,9 @@
const {getReviewsFromStorage, saveReviewsToStorage} = require("./localStorage"); import {strict as assert} from "node:assert"
const {environment} = require("./testenv"); import {describe, it, beforeEach} from "mocha";
const assert = require("assert"); import {saveReviewsToStorage, getReviewsFromStorage} from "./localStorage.js";
const {describe, it, beforeEach} = require("mocha");
beforeEach(() => { beforeEach(() => {
window = environment(); localStorage.clear();
localStorage = window.localStorage;
}); });
describe("test app localStorage interaction", () => { describe("test app localStorage interaction", () => {
@ -26,7 +24,8 @@ describe("test app localStorage interaction", () => {
assert.deepEqual(getReviewsFromStorage(), reviews); assert.deepEqual(getReviewsFromStorage(), reviews);
}); });
it("repeated store one more and get", () => { it("repeated store one more and get", () => {
saveReviewsToStorage(reviews); let reviews = [];
assert.deepEqual(getReviewsFromStorage(), reviews); assert.deepEqual(getReviewsFromStorage(), reviews);
for(let i = 0; i < 1000; i++){ for(let i = 0; i < 1000; i++){

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
});
});

View File

@ -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();
}