mirror of
https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
synced 2024-12-26 17:09:09 +00:00
move localStorage util to localStorage.js,
update main,js to rquire localStroage, add simple unit tests to localStroage.test.js, create symlink of testenv.js to source/assets/scripts, update package.json with proper recursive call Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
parent
9bbb2e48f7
commit
1718c7cedb
@ -2,7 +2,7 @@
|
||||
"name": "food-journal",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"test": "mocha --recursive **/*.test.js",
|
||||
"test": "mocha --recursive './{,!(node_modules)/**}/*.test.js'",
|
||||
"lint": "eslint **/*.js",
|
||||
"fix-style": "eslint --fix **/*.js"
|
||||
},
|
||||
|
21
source/assets/scripts/localStorage.js
Normal file
21
source/assets/scripts/localStorage.js
Normal file
@ -0,0 +1,21 @@
|
||||
module.exports = {getReviewsFromStorage, saveReviewsToStorage};
|
||||
|
||||
/**
|
||||
* @returns {Array<Object>} An array of reviews found in localStorage
|
||||
*/
|
||||
function getReviewsFromStorage() {
|
||||
let result = JSON.parse(localStorage.getItem('reviews'))
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
return new Array(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes in an array of reviews, converts it to a string, and then
|
||||
* saves that string to 'reviews' in localStorage
|
||||
* @param {Array<Object>} reviews An array of reviews
|
||||
*/
|
||||
function saveReviewsToStorage(reviews) {
|
||||
localStorage.setItem('reviews', JSON.stringify(reviews));
|
||||
}
|
27
source/assets/scripts/localStorage.test.js
Normal file
27
source/assets/scripts/localStorage.test.js
Normal file
@ -0,0 +1,27 @@
|
||||
const {getReviewsFromStorage, saveReviewsToStorage} = require("./localStorage");
|
||||
const {environment} = require("./testenv");
|
||||
const assert = require("assert");
|
||||
const {describe, it, beforeEach} = require("mocha");
|
||||
|
||||
beforeEach(() => {
|
||||
window = environment();
|
||||
localStorage = window.localStorage;
|
||||
});
|
||||
|
||||
describe("test app localStorage interaction", () => {
|
||||
it("get after init", () => {
|
||||
assert.deepEqual(getReviewsFromStorage(), []);
|
||||
});
|
||||
it("store one then get", () => {
|
||||
reviews = [{
|
||||
"imgSrc": "sample src",
|
||||
"imgAlt": "sample alt",
|
||||
"mealName": "sample name",
|
||||
"restaurant": "sample restaurant",
|
||||
"rating": 5
|
||||
}];
|
||||
|
||||
saveReviewsToStorage(reviews);
|
||||
assert.deepEqual(getReviewsFromStorage(), reviews);
|
||||
})
|
||||
});
|
@ -1,4 +1,5 @@
|
||||
// main.js
|
||||
const {getReviewsFromStorage, saveReviewsToStorage} = require('./localStorage');
|
||||
|
||||
// Run the init() function when the page has loaded
|
||||
window.addEventListener('DOMContentLoaded', init);
|
||||
@ -12,17 +13,6 @@ function init() {
|
||||
initFormHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Array<Object>} An array of reviews found in localStorage
|
||||
*/
|
||||
function getReviewsFromStorage() {
|
||||
let result = JSON.parse(localStorage.getItem('reviews'))
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
return new Array(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Array<Object>} reviews An array of reviews
|
||||
*/
|
||||
@ -36,15 +26,6 @@ function addReviewsToDocument(reviews) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes in an array of reviews, converts it to a string, and then
|
||||
* saves that string to 'reviews' in localStorage
|
||||
* @param {Array<Object>} reviews An array of reviews
|
||||
*/
|
||||
function saveReviewsToStorage(reviews) {
|
||||
localStorage.setItem('reviews', JSON.stringify(reviews));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the necesarry event handlers to <form> and the clear storage
|
||||
* <button>.
|
||||
|
28
source/assets/scripts/testenv.js
Normal file
28
source/assets/scripts/testenv.js
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user