add sorting and filtering localStorage helper functions,

add new unit tests for new helper functions

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2022-11-26 15:37:48 -08:00
parent 4c649e7770
commit 9ff58a1c2c
3 changed files with 219 additions and 6 deletions

View File

@ -76,3 +76,21 @@ export function getAllReviewsFromStorage() {
} }
return reviews; return reviews;
} }
/**
* Returns the top n reviews by ID. If there are less than n reviews, returns the most possible.
* @param {number} n number of reviews to return
* @returns {Object} list of n reviews that are the top rated
*/
export function getTopReviewsFromStorage(n) {
}
/**
* Returns all reviews which contain the same tag specified.
* @param {string} tag to filter by
* @returns {Object} list of reviews that all contain the specified tag
*/
export function getReviewsByTag(tag) {
}

View File

@ -1,8 +1,8 @@
import {strict as assert} from "node:assert"; import {strict as assert} from "node:assert";
import {describe, it, before, after} from "mocha"; import {describe, it, before, after} from "mocha";
import {newReviewToStorage, getReviewFromStorage, updateReviewToStorage, deleteReviewFromStorage, getAllReviewsFromStorage} from "./localStorage.js"; import {newReviewToStorage, getReviewFromStorage, updateReviewToStorage, deleteReviewFromStorage, getAllReviewsFromStorage, getTopReviewsFromStorage, getReviewsByTag} from "./localStorage.js";
describe("test app localStorage interaction", () => { describe("test CRUD localStorage interaction", () => {
before(() => { before(() => {
localStorage.clear(); localStorage.clear();
@ -47,9 +47,7 @@ describe("test app localStorage interaction", () => {
"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`] "tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`]
}; };
newReviewToStorage(new_review); new_review.reviewID = newReviewToStorage(new_review);
new_review.reviewID = i;
reviews.push(new_review); reviews.push(new_review);
assert.deepEqual(getAllReviewsFromStorage(), reviews); assert.deepEqual(getAllReviewsFromStorage(), reviews);
@ -99,5 +97,202 @@ describe("test app localStorage interaction", () => {
} }
}).timeout(5000); }).timeout(5000);
it("test localStorage state after all deletes", () => {
assert.deepEqual(getAllReviewsFromStorage(), []);
});
after(() => {});
});
describe("test sort/filter localStorage interaction", () => {
before(() => {
localStorage.clear();
});
it("add sample data for sort and filter", () => {
for(let i = 0; i < 100; i++){
let new_review = {
"imgSrc": `sample src ${i}`,
"mealName": `sample name ${i}`,
"restaurant": `sample restaurant ${i}`,
"rating": i,
"tags": [`tag ${i%3}`, `tag ${i < 50}`, "tag x"]
};
}
});
it("test getTopReviewsFromStorage end behavior after create", () =>{
for(let i = 0; i <= 100; i++){
let top_reviews = getTopReviewsFromStorage(i);
for(let j = 0; j < i; j++){
assert.strictEqual(top_reviews[j].rating, 99 - j);
assert.strictEqual(top_reviews[j].reviewID, 99 - j);
}
}
});
it("test getReviewsByTag end behavior after create", () => {
let specific_tagged_reviews = [];
specific_tagged_reviews = getReviewsByTag("tag 0");
assert.strictEqual(specific_tagged_reviews.length, 34)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 0"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 3, 0);
}
specific_tagged_reviews = getReviewsByTag("tag 1");
assert.strictEqual(specific_tagged_reviews.length, 33)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 1"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 3, 1);
}
specific_tagged_reviews = getReviewsByTag("tag 2");
assert.strictEqual(specific_tagged_reviews.length, 33)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 2"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 3, 2);
}
specific_tagged_reviews = getReviewsByTag("tag true");
assert.strictEqual(specific_tagged_reviews.length, 50)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag true"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID < 50, true);
}
specific_tagged_reviews = getReviewsByTag("tag false");
assert.strictEqual(specific_tagged_reviews.length, 50);
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag false"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID >= 50, true);
}
specific_tagged_reviews = getReviewsByTag("tag x");
assert.strictEqual(specific_tagged_reviews.length, 100);
specific_tagged_reviews = getReviewsByTag("tag y");
assert.deepEqual(specific_tagged_reviews, []);
});
it("update sample data for sort and filter", () => {
for(let i = 0; i < 100; i++){
let new_review = {
"imgSrc": `sample src ${i}`,
"mealName": `sample name ${i}`,
"restaurant": `sample restaurant ${i}`,
"rating": 99-i,
"tags": [`tag ${i%4}`, `tag ${i < 37}`, "tag y"]
};
updateReviewToStorage(i, new_review);
}
});
it("test getTopReviewsFromStorage end behavior after create", () =>{
for(let i = 0; i <= 100; i++){
let top_reviews = getTopReviewsFromStorage(i);
for(let j = 0; j < i; j++){
assert.strictEqual(top_reviews[j].rating, 99 - j);
assert.strictEqual(top_reviews[j].reviewID, j);
}
}
});
it("test getReviewsByTag end behavior after update", () => {
let specific_tagged_reviews = [];
specific_tagged_reviews = getReviewsByTag("tag 0");
assert.strictEqual(specific_tagged_reviews.length, 25)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 0"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 4, 0);
}
specific_tagged_reviews = getReviewsByTag("tag 1");
assert.strictEqual(specific_tagged_reviews.length, 25)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 1"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 4, 1);
}
specific_tagged_reviews = getReviewsByTag("tag 2");
assert.strictEqual(specific_tagged_reviews.length, 25)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 2"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 4, 2);
}
specific_tagged_reviews = getReviewsByTag("tag 3");
assert.strictEqual(specific_tagged_reviews.length, 25)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag 3"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID % 4, 3);
}
specific_tagged_reviews = getReviewsByTag("tag true");
assert.strictEqual(specific_tagged_reviews.length, 37)
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag true"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID < 37, true);
}
specific_tagged_reviews = getReviewsByTag("tag false");
assert.strictEqual(specific_tagged_reviews.length, 63);
for(let i = 0; i < specific_tagged_reviews.length; i++){
assert.strictEqual(specific_tagged_reviews[i].tags.includes("tag false"), true);
assert.strictEqual(specific_tagged_reviews[i].reviewID >= 37, true);
}
specific_tagged_reviews = getReviewsByTag("tag x");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag y");
assert.strictEqual(specific_tagged_reviews.length, 100);
});
it("delete all sample data for sort and filter", () => {
for(let i = 0; i < 100; i++){
deleteReviewFromStorage(i);
}
});
it("test getTopReviewsFromStorage end behavior after delete", () =>{
for(let i = 0; i <= 100; i++){
let top_reviews = getTopReviewsFromStorage(i);
assert.deepEqual(top_reviews, []);
}
});
it("test getReviewsByTag end behavior after delete", () => {
let specific_tagged_reviews = [];
specific_tagged_reviews = getReviewsByTag("tag 0");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag 1");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag 2");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag 3");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag true");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag false");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag x");
assert.deepEqual(specific_tagged_reviews, []);
specific_tagged_reviews = getReviewsByTag("tag y");
assert.deepEqual(specific_tagged_reviews, []);
});
after(() => {}); after(() => {});
}); });

View File

@ -27,7 +27,7 @@ describe("test App end to end", async () => {
} }
catch (error) { catch (error) {
await console.log("❌ failed to connect to localhost webserver on port 8080"); await console.log("❌ failed to connect to localhost webserver on port 8080");
await exit(1); //await exit(1);
} }
}); });