Merge pull request #37 from cse110-fa22-group29/js-unit-test

Pull new JS unit tests from js-unit-test to js-unit-test-ci
This commit is contained in:
Arthur Lu 2022-11-07 15:27:51 -08:00 committed by GitHub
commit 1c7b1de7d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 0 deletions

10
package.json Normal file
View File

@ -0,0 +1,10 @@
{
"name": "food-journal",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"dependencies": {
"jest": "24"
}
}

28
testenv/testenv.js Normal file
View File

@ -0,0 +1,28 @@
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;
}

86
testenv/testenv.test.js Normal file
View File

@ -0,0 +1,86 @@
const {environment} = require("./testenv.js");
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();
}
describe("test localStorage mock", () => {
test("test save and fetch", () => {
let window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
saveToLocal("testkey4", "testvalue4");
expect(getFromLocal("testkey1")).toStrictEqual("testvalue1");
expect(getFromLocal("testkey2")).toStrictEqual("testvalue2");
expect(getFromLocal("testkey3")).toStrictEqual("testvalue3");
expect(getFromLocal("testkey4")).toStrictEqual("testvalue4");
saveToLocal("testkey6", "testvalue5");
expect(getFromLocal("testkey6")).toStrictEqual("testvalue5");
});
test("test delete and fetch", () => {
let window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
saveToLocal("testkey4", "testvalue4");
removeFromLocal("testkey3");
expect(getFromLocal("testkey1")).toStrictEqual("testvalue1");
expect(getFromLocal("testkey2")).toStrictEqual("testvalue2");
expect(getFromLocal("testkey3")).toStrictEqual(null);
expect(getFromLocal("testkey4")).toStrictEqual("testvalue4");
removeFromLocal("testkey1");
expect(getFromLocal("testkey1")).toStrictEqual(null);
expect(getFromLocal("testkey2")).toStrictEqual("testvalue2");
expect(getFromLocal("testkey3")).toStrictEqual(null);
expect(getFromLocal("testkey4")).toStrictEqual("testvalue4");
removeFromLocal("testkey4");
expect(getFromLocal("testkey1")).toStrictEqual(null);
expect(getFromLocal("testkey2")).toStrictEqual("testvalue2");
expect(getFromLocal("testkey3")).toStrictEqual(null);
expect(getFromLocal("testkey4")).toStrictEqual(null);
removeFromLocal("testkey2");
expect(getFromLocal("testkey1")).toStrictEqual(null);
expect(getFromLocal("testkey2")).toStrictEqual(null);
expect(getFromLocal("testkey3")).toStrictEqual(null);
expect(getFromLocal("testkey4")).toStrictEqual(null);
});
test("test clear and fetch", () => {
let window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
saveToLocal("testkey4", "testvalue4");
clearLocal();
expect(getFromLocal("testkey1")).toStrictEqual(null);
expect(getFromLocal("testkey2")).toStrictEqual(null);
expect(getFromLocal("testkey3")).toStrictEqual(null);
expect(getFromLocal("testkey4")).toStrictEqual(null);
});
});