mirror of
https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
synced 2024-12-27 09:29:10 +00:00
update unit tests to match new localStorage implementations
This commit is contained in:
parent
6493fbd171
commit
5e83338668
@ -1,39 +1,48 @@
|
||||
import {strict as assert} from "node:assert";
|
||||
import {describe, it, beforeEach} from "mocha";
|
||||
import {saveReviewsToStorage, getReviewsFromStorage} from "./localStorage.js";
|
||||
import {newReviewToStorage, getReviewFromStorage, updateReviewToStorage, deleteReviewFromStorage, getAllReviewsFromStorage} from "./localStorage.js";
|
||||
|
||||
describe("test app localStorage interaction", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
before(() => {
|
||||
localStorage.clear();
|
||||
localStorage.setItem("activeIDS", JSON.stringify([]));
|
||||
localStorage.setItem("nextID", JSON.stringify(0));
|
||||
});
|
||||
|
||||
it("get after init", () => {
|
||||
assert.deepEqual(getReviewsFromStorage(), []);
|
||||
it("test localStorage state after init", () => {
|
||||
assert.deepEqual(getAllReviewsFromStorage(), []);
|
||||
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), []);
|
||||
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), 0);
|
||||
});
|
||||
it("store one then get", () => {
|
||||
let reviews = [{
|
||||
|
||||
it("test localStorage state after adding one review", () => {
|
||||
let review = {
|
||||
"imgSrc": "sample src",
|
||||
"imgAlt": "sample alt",
|
||||
"mealName": "sample name",
|
||||
"restaurant": "sample restaurant",
|
||||
"rating": 5,
|
||||
"tags": ["tag 1", "tag 2", "tag 3"]
|
||||
}];
|
||||
};
|
||||
|
||||
saveReviewsToStorage(reviews);
|
||||
assert.deepEqual(getReviewsFromStorage(), reviews);
|
||||
newReviewToStorage(review);
|
||||
|
||||
review.reviewID = 0;
|
||||
|
||||
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);
|
||||
});
|
||||
it("repeated store one more and get", () => {
|
||||
let reviews = [];
|
||||
|
||||
assert.deepEqual(getReviewsFromStorage(), reviews);
|
||||
it("test localStorage state during adding 999 reviews", () => {
|
||||
let reviews = getAllReviewsFromStorage();
|
||||
let ids = [0];
|
||||
|
||||
for(let i = 0; i < 1000; i++){
|
||||
reviews = getReviewsFromStorage();
|
||||
|
||||
reviews.push(
|
||||
{
|
||||
for(let i = 1; i < 1000; i++){
|
||||
ids.push(i);
|
||||
let new_review = {
|
||||
"imgSrc": `sample src ${i}`,
|
||||
"imgAlt": `sample alt ${i}`,
|
||||
"mealName": `sample name ${i}`,
|
||||
@ -41,9 +50,59 @@ describe("test app localStorage interaction", () => {
|
||||
"rating": i,
|
||||
"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`]
|
||||
}
|
||||
);
|
||||
saveReviewsToStorage(reviews);
|
||||
assert.deepEqual(getReviewsFromStorage(), reviews);
|
||||
|
||||
newReviewToStorage(new_review);
|
||||
|
||||
new_review.reviewID = i;
|
||||
reviews.push(new_review);
|
||||
|
||||
assert.deepEqual(getAllReviewsFromStorage(), reviews);
|
||||
assert.deepEqual(getReviewFromStorage(i), new_review);
|
||||
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), ids);
|
||||
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), (i+1));
|
||||
}
|
||||
}).timeout(10000);
|
||||
}).timeout(5000);
|
||||
|
||||
it("test localStorage state during updating 1000 reviews", () => {
|
||||
let reviews = getAllReviewsFromStorage();
|
||||
let ids = JSON.parse(localStorage.getItem("activeIDS"));
|
||||
|
||||
for(let i = 0; i < 1000; i++){
|
||||
let new_review = {
|
||||
"imgSrc": `updated sample src ${i}`,
|
||||
"imgAlt": `updated sample alt ${i}`,
|
||||
"mealName": `updated sample name ${i}`,
|
||||
"restaurant": `updated sample restaurant ${i}`,
|
||||
"rating": i*2+i,
|
||||
"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`]
|
||||
}
|
||||
new_review.reviewID = i;
|
||||
|
||||
reviews[i] = new_review;
|
||||
|
||||
updateReviewToStorage(i, new_review);
|
||||
|
||||
assert.deepEqual(getAllReviewsFromStorage(), reviews);
|
||||
assert.deepEqual(getReviewFromStorage(i), new_review);
|
||||
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), ids);
|
||||
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"));
|
||||
|
||||
for(let i = 999; i >= 0; i--){
|
||||
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);
|
||||
}
|
||||
}).timeout(5000);
|
||||
|
||||
after(() => {});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user