From 1718c7cedb6ca887c5d2157c8bb9c654ee3563d8 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Thu, 10 Nov 2022 01:35:41 +0000 Subject: [PATCH 1/3] 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 --- package.json | 2 +- source/assets/scripts/localStorage.js | 21 ++++++++++++++++ source/assets/scripts/localStorage.test.js | 27 +++++++++++++++++++++ source/assets/scripts/main.js | 21 +--------------- source/assets/scripts/testenv.js | 28 ++++++++++++++++++++++ 5 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 source/assets/scripts/localStorage.js create mode 100644 source/assets/scripts/localStorage.test.js create mode 100644 source/assets/scripts/testenv.js diff --git a/package.json b/package.json index c21fbe9..198cef2 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/source/assets/scripts/localStorage.js b/source/assets/scripts/localStorage.js new file mode 100644 index 0000000..e2308b6 --- /dev/null +++ b/source/assets/scripts/localStorage.js @@ -0,0 +1,21 @@ +module.exports = {getReviewsFromStorage, saveReviewsToStorage}; + +/** + * @returns {Array} 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} reviews An array of reviews + */ +function saveReviewsToStorage(reviews) { + localStorage.setItem('reviews', JSON.stringify(reviews)); +} \ No newline at end of file diff --git a/source/assets/scripts/localStorage.test.js b/source/assets/scripts/localStorage.test.js new file mode 100644 index 0000000..425a6d3 --- /dev/null +++ b/source/assets/scripts/localStorage.test.js @@ -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); + }) +}); \ No newline at end of file diff --git a/source/assets/scripts/main.js b/source/assets/scripts/main.js index 3073fb6..58a4809 100644 --- a/source/assets/scripts/main.js +++ b/source/assets/scripts/main.js @@ -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} 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} 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} reviews An array of reviews - */ -function saveReviewsToStorage(reviews) { - localStorage.setItem('reviews', JSON.stringify(reviews)); -} - /** * Adds the necesarry event handlers to
and the clear storage * diff --git a/source/assets/scripts/ReviewCard.js b/source/assets/scripts/ReviewCard.js index 1b66a88..7f5806f 100644 --- a/source/assets/scripts/ReviewCard.js +++ b/source/assets/scripts/ReviewCard.js @@ -102,6 +102,7 @@ class ReviewCard extends HTMLElement { * "mealName": "string", * "restaurant": "string", * "rating": number + * "tags": string array * } */ set data(data) { @@ -138,10 +139,21 @@ class ReviewCard extends HTMLElement { div.append(span1); div.append(img2); + //added tags + let tagContainer = document.createElement('div') + tagContainer.setAttribute('class', 'tag-container'); + for (let i = 0; i < data['tags'].length; i++) { + let newTag = document.createElement('p'); + newTag.setAttribute('class','tag'); + newTag.innerHTML = data['tags'][i] + tagContainer.append(newTag) + } + articleEl.append(img1) articleEl.append(pMeal) articleEl.append(pRestaurant) articleEl.append(div) + articleEl.append(tagContainer) } diff --git a/source/assets/scripts/localStorage.js b/source/assets/scripts/localStorage.js index e2308b6..7495993 100644 --- a/source/assets/scripts/localStorage.js +++ b/source/assets/scripts/localStorage.js @@ -1,9 +1,10 @@ -module.exports = {getReviewsFromStorage, saveReviewsToStorage}; +// +//module.exports = {getReviewsFromStorage, saveReviewsToStorage}; /** * @returns {Array} An array of reviews found in localStorage */ -function getReviewsFromStorage() { +export function getReviewsFromStorage() { let result = JSON.parse(localStorage.getItem('reviews')) if (result) { return result; @@ -16,6 +17,6 @@ function getReviewsFromStorage() { * saves that string to 'reviews' in localStorage * @param {Array} reviews An array of reviews */ -function saveReviewsToStorage(reviews) { +export function saveReviewsToStorage(reviews) { localStorage.setItem('reviews', JSON.stringify(reviews)); } \ No newline at end of file diff --git a/source/assets/scripts/main.js b/source/assets/scripts/main.js index 58a4809..28f9d83 100644 --- a/source/assets/scripts/main.js +++ b/source/assets/scripts/main.js @@ -1,5 +1,5 @@ // main.js -const {getReviewsFromStorage, saveReviewsToStorage} = require('./localStorage'); +import {getReviewsFromStorage, saveReviewsToStorage} from './localStorage.js'; // Run the init() function when the page has loaded window.addEventListener('DOMContentLoaded', init); @@ -31,14 +31,16 @@ function addReviewsToDocument(reviews) { *