2022-12-03 02:02:52 +00:00
|
|
|
import { strict as assert } from "node:assert";
|
|
|
|
import { describe, it, before, after } from "mocha";
|
|
|
|
import {
|
|
|
|
newReviewToStorage,
|
|
|
|
getReviewFromStorage,
|
|
|
|
updateReviewToStorage,
|
|
|
|
deleteReviewFromStorage,
|
|
|
|
getAllReviewsFromStorage,
|
|
|
|
getIDsByTag,
|
|
|
|
getTopIDsFromStorage,
|
|
|
|
} from "./localStorage.js";
|
2022-11-10 01:35:41 +00:00
|
|
|
|
2022-11-26 23:37:48 +00:00
|
|
|
describe("test CRUD localStorage interaction", () => {
|
2022-11-18 07:51:08 +00:00
|
|
|
before(() => {
|
2022-11-17 00:10:31 +00:00
|
|
|
localStorage.clear();
|
|
|
|
});
|
|
|
|
|
2022-11-18 07:51:08 +00:00
|
|
|
it("test localStorage state after init", () => {
|
|
|
|
assert.deepEqual(getAllReviewsFromStorage(), []);
|
|
|
|
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), []);
|
|
|
|
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), 0);
|
2022-11-10 01:35:41 +00:00
|
|
|
});
|
2022-11-18 07:51:08 +00:00
|
|
|
|
|
|
|
it("test localStorage state after adding one review", () => {
|
|
|
|
let review = {
|
2022-12-03 02:02:52 +00:00
|
|
|
imgSrc: "sample src",
|
|
|
|
mealName: "sample name",
|
|
|
|
restaurant: "sample restaurant",
|
|
|
|
rating: 5,
|
|
|
|
tags: ["tag 1", "tag 2", "tag 3"],
|
2022-11-18 07:51:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
newReviewToStorage(review);
|
|
|
|
|
|
|
|
review.reviewID = 0;
|
2022-11-10 01:35:41 +00:00
|
|
|
|
2022-11-18 07:51:08 +00:00
|
|
|
assert.deepEqual(getAllReviewsFromStorage(), [review]);
|
|
|
|
assert.deepEqual(getReviewFromStorage(0), review);
|
|
|
|
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), [0]);
|
|
|
|
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), 1);
|
2022-11-10 22:33:47 +00:00
|
|
|
});
|
2022-11-11 02:39:40 +00:00
|
|
|
|
2022-11-18 07:51:08 +00:00
|
|
|
it("test localStorage state during adding 999 reviews", () => {
|
|
|
|
let reviews = getAllReviewsFromStorage();
|
|
|
|
let ids = [0];
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 1; i < 1000; i++) {
|
2022-11-18 07:51:08 +00:00
|
|
|
ids.push(i);
|
|
|
|
let new_review = {
|
2022-12-03 02:02:52 +00:00
|
|
|
imgSrc: `sample src ${i}`,
|
|
|
|
mealName: `sample name ${i}`,
|
|
|
|
restaurant: `sample restaurant ${i}`,
|
|
|
|
rating: i,
|
|
|
|
tags: [`tag ${3 * i}`, `tag ${3 * i + 1}`, `tag ${3 * i + 2}`],
|
2022-11-18 07:56:07 +00:00
|
|
|
};
|
2022-11-18 07:51:08 +00:00
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
new_review.reviewID = newReviewToStorage(new_review);
|
2022-11-18 07:51:08 +00:00
|
|
|
reviews.push(new_review);
|
|
|
|
|
|
|
|
assert.deepEqual(getAllReviewsFromStorage(), reviews);
|
|
|
|
assert.deepEqual(getReviewFromStorage(i), new_review);
|
|
|
|
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), ids);
|
2022-12-03 02:02:52 +00:00
|
|
|
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), i + 1);
|
2022-11-18 07:51:08 +00:00
|
|
|
}
|
|
|
|
}).timeout(5000);
|
|
|
|
|
|
|
|
it("test localStorage state during updating 1000 reviews", () => {
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < 1000; i++) {
|
2022-12-02 00:25:15 +00:00
|
|
|
let old_review = getReviewFromStorage(i);
|
|
|
|
let id = old_review.reviewID;
|
|
|
|
|
2022-11-18 07:51:08 +00:00
|
|
|
let new_review = {
|
2022-12-03 02:02:52 +00:00
|
|
|
imgSrc: `updated sample src ${id}`,
|
|
|
|
mealName: `updated sample name ${id}`,
|
|
|
|
restaurant: `updated sample restaurant ${id}`,
|
|
|
|
reviewID: id,
|
|
|
|
rating: (id % 5) + 1,
|
|
|
|
tags: [`tag ${3 * id}`, `tag ${3 * id + 1}`, `tag ${3 * id + 2}`],
|
2022-11-18 07:56:07 +00:00
|
|
|
};
|
2022-11-18 07:51:08 +00:00
|
|
|
|
2022-12-02 00:25:15 +00:00
|
|
|
updateReviewToStorage(id, new_review);
|
2022-11-18 07:51:08 +00:00
|
|
|
|
2022-12-02 00:25:15 +00:00
|
|
|
let all_reviews = getAllReviewsFromStorage();
|
|
|
|
let active_ids = JSON.parse(localStorage.getItem("activeIDS"));
|
2022-11-18 07:51:08 +00:00
|
|
|
|
2022-12-02 00:25:15 +00:00
|
|
|
assert.deepEqual(all_reviews[999], new_review);
|
|
|
|
assert.strictEqual(active_ids[999], id);
|
2022-11-18 07:51:08 +00:00
|
|
|
assert.deepEqual(getReviewFromStorage(i), new_review);
|
|
|
|
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), 1000);
|
|
|
|
}
|
|
|
|
}).timeout(5000);
|
|
|
|
|
|
|
|
it("test localStorage state during deleting 1000 reviews", () => {
|
|
|
|
let reviews = getAllReviewsFromStorage();
|
|
|
|
let ids = JSON.parse(localStorage.getItem("activeIDS"));
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 999; i >= 0; i--) {
|
2022-11-18 07:51:08 +00:00
|
|
|
deleteReviewFromStorage(i);
|
|
|
|
ids.pop();
|
|
|
|
reviews.pop();
|
|
|
|
|
|
|
|
assert.deepEqual(getAllReviewsFromStorage(), reviews);
|
|
|
|
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), ids);
|
|
|
|
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), 1000);
|
2022-11-10 22:33:47 +00:00
|
|
|
}
|
2022-11-18 07:51:08 +00:00
|
|
|
}).timeout(5000);
|
|
|
|
|
2022-11-26 23:37:48 +00:00
|
|
|
it("test localStorage state after all deletes", () => {
|
|
|
|
assert.deepEqual(getAllReviewsFromStorage(), []);
|
|
|
|
});
|
|
|
|
|
2022-11-18 07:51:08 +00:00
|
|
|
after(() => {});
|
2022-11-11 20:07:58 +00:00
|
|
|
});
|
2022-11-26 23:37:48 +00:00
|
|
|
|
|
|
|
describe("test sort/filter localStorage interaction", () => {
|
|
|
|
before(() => {
|
|
|
|
localStorage.clear();
|
2022-11-30 22:49:41 +00:00
|
|
|
getAllReviewsFromStorage();
|
2022-11-26 23:37:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("add sample data for sort and filter", () => {
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-11-27 00:06:24 +00:00
|
|
|
let review = {
|
2022-12-03 02:02:52 +00:00
|
|
|
imgSrc: `sample src ${i}`,
|
|
|
|
mealName: `sample name ${i}`,
|
|
|
|
restaurant: `sample restaurant ${i}`,
|
|
|
|
rating: (i % 5) + 1,
|
|
|
|
tags: [`tag ${i % 3}`, `tag ${i < 50}`, "tag x"],
|
2022-11-26 23:37:48 +00:00
|
|
|
};
|
2022-11-27 00:06:24 +00:00
|
|
|
|
|
|
|
newReviewToStorage(review);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
it("test getTopIDsFromStorage end behavior after create", () => {
|
2022-11-30 22:49:41 +00:00
|
|
|
let top_reviews = getTopIDsFromStorage();
|
|
|
|
let prev = Infinity;
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < top_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(top_reviews[i]);
|
|
|
|
assert.strictEqual(review.rating <= prev, true);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
it("test getIDsByTag end behavior after create", () => {
|
2022-11-26 23:37:48 +00:00
|
|
|
let specific_tagged_reviews = [];
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 0");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 34);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 0"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 3, 0);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 1");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 33);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 1"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 3, 1);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 2");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 33);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 2"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 3, 2);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag true");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 50);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag true"), true);
|
|
|
|
assert.strictEqual(review.reviewID < 50, true);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag false");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 50);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag false"), true);
|
|
|
|
assert.strictEqual(review.reviewID >= 50, true);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag x");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 100);
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag y");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("update sample data for sort and filter", () => {
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let old_review = getReviewFromStorage(i);
|
2022-11-26 23:37:48 +00:00
|
|
|
let new_review = {
|
2022-12-03 02:02:52 +00:00
|
|
|
imgSrc: `sample src ${i}`,
|
|
|
|
mealName: `sample name ${i}`,
|
|
|
|
restaurant: `sample restaurant ${i}`,
|
|
|
|
reviewID: old_review.reviewID,
|
|
|
|
rating: (i % 5) + 1,
|
|
|
|
tags: [`tag ${i % 4}`, `tag ${i < 37}`, "tag y"],
|
2022-11-26 23:37:48 +00:00
|
|
|
};
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
updateReviewToStorage(old_review.reviewID, new_review);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
it("test getTopIDsFromStorage end behavior after create", () => {
|
2022-11-30 22:49:41 +00:00
|
|
|
let top_reviews = getTopIDsFromStorage();
|
|
|
|
let prev = Infinity;
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < top_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(top_reviews[i]);
|
|
|
|
assert.strictEqual(review.rating <= prev, true);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
it("test getIDsByTag end behavior after update", () => {
|
2022-11-26 23:37:48 +00:00
|
|
|
let specific_tagged_reviews = [];
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 0");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 25);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 0"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 4, 0);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 1");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 25);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 1"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 4, 1);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 2");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 25);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 2"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 4, 2);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 3");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 25);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag 3"), true);
|
|
|
|
assert.strictEqual(review.reviewID % 4, 3);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag true");
|
2022-11-27 00:06:24 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 37);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag true"), true);
|
|
|
|
assert.strictEqual(review.reviewID < 37, true);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag false");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 63);
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < specific_tagged_reviews.length; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let review = getReviewFromStorage(specific_tagged_reviews[i]);
|
|
|
|
assert.strictEqual(review.tags.includes("tag false"), true);
|
|
|
|
assert.strictEqual(review.reviewID >= 37, true);
|
2022-11-26 23:37:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag x");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag y");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.strictEqual(specific_tagged_reviews.length, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("delete all sample data for sort and filter", () => {
|
2022-12-03 02:02:52 +00:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2022-11-26 23:37:48 +00:00
|
|
|
deleteReviewFromStorage(i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-03 02:02:52 +00:00
|
|
|
it("test getTopIDsFromStorage end behavior after delete", () => {
|
|
|
|
for (let i = 0; i <= 100; i++) {
|
2022-11-30 22:49:41 +00:00
|
|
|
let top_reviews = getTopIDsFromStorage(i);
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(top_reviews, []);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
it("test getIDsByTag end behavior after delete", () => {
|
2022-11-26 23:37:48 +00:00
|
|
|
let specific_tagged_reviews = [];
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 0");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 1");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 2");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag 3");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag true");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag false");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag x");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
|
2022-11-30 22:49:41 +00:00
|
|
|
specific_tagged_reviews = getIDsByTag("tag y");
|
2022-11-26 23:37:48 +00:00
|
|
|
assert.deepEqual(specific_tagged_reviews, []);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {});
|
2022-12-03 02:02:52 +00:00
|
|
|
});
|