implement basic linting with eslint,

fix linting errors in testenv module

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2022-11-08 03:09:10 +00:00
parent 50f0679a76
commit a6015aff3b
4 changed files with 149 additions and 108 deletions

33
.eslintrc.json Normal file
View File

@ -0,0 +1,33 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"no-global-assign": 0
}
}

View File

@ -2,9 +2,16 @@
"name": "food-journal",
"version": "1.0.0",
"scripts": {
"test": "mocha --recursive **/*.test.js"
"test": "mocha --recursive **/*.test.js",
"lint": "eslint **/*.js",
"fix-style": "eslint --fix **/*.js"
},
"dependencies": {
"devDependencies": {
"eslint": "^8.27.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.5.0",
"eslint-plugin-promise": "^6.1.1",
"mocha": "10"
}
}

View File

@ -1,28 +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;
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,80 +1,81 @@
const {environment} = require("./testenv.js");
var assert = require('assert');
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);
});
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);
});
});