mirror of
https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
synced 2024-12-28 01:39:09 +00:00
update unit tests to match new localStorage implementations
This commit is contained in:
parent
6493fbd171
commit
5e83338668
@ -1,49 +1,108 @@
|
|||||||
import {strict as assert} from "node:assert";
|
import {strict as assert} from "node:assert";
|
||||||
import {describe, it, beforeEach} from "mocha";
|
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", () => {
|
describe("test app localStorage interaction", () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
before(() => {
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
|
localStorage.setItem("activeIDS", JSON.stringify([]));
|
||||||
|
localStorage.setItem("nextID", JSON.stringify(0));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("get after init", () => {
|
it("test localStorage state after init", () => {
|
||||||
assert.deepEqual(getReviewsFromStorage(), []);
|
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",
|
"imgSrc": "sample src",
|
||||||
"imgAlt": "sample alt",
|
"imgAlt": "sample alt",
|
||||||
"mealName": "sample name",
|
"mealName": "sample name",
|
||||||
"restaurant": "sample restaurant",
|
"restaurant": "sample restaurant",
|
||||||
"rating": 5,
|
"rating": 5,
|
||||||
"tags": ["tag 1", "tag 2", "tag 3"]
|
"tags": ["tag 1", "tag 2", "tag 3"]
|
||||||
}];
|
};
|
||||||
|
|
||||||
saveReviewsToStorage(reviews);
|
newReviewToStorage(review);
|
||||||
assert.deepEqual(getReviewsFromStorage(), reviews);
|
|
||||||
|
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 = 1; i < 1000; i++){
|
||||||
|
ids.push(i);
|
||||||
|
let new_review = {
|
||||||
|
"imgSrc": `sample src ${i}`,
|
||||||
|
"imgAlt": `sample alt ${i}`,
|
||||||
|
"mealName": `sample name ${i}`,
|
||||||
|
"restaurant": `sample restaurant ${i}`,
|
||||||
|
"rating": i,
|
||||||
|
"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`]
|
||||||
|
}
|
||||||
|
|
||||||
|
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(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++){
|
for(let i = 0; i < 1000; i++){
|
||||||
reviews = getReviewsFromStorage();
|
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.push(
|
reviews[i] = new_review;
|
||||||
{
|
|
||||||
"imgSrc": `sample src ${i}`,
|
updateReviewToStorage(i, new_review);
|
||||||
"imgAlt": `sample alt ${i}`,
|
|
||||||
"mealName": `sample name ${i}`,
|
assert.deepEqual(getAllReviewsFromStorage(), reviews);
|
||||||
"restaurant": `sample restaurant ${i}`,
|
assert.deepEqual(getReviewFromStorage(i), new_review);
|
||||||
"rating": i,
|
assert.deepEqual(JSON.parse(localStorage.getItem("activeIDS")), ids);
|
||||||
"tags": [`tag ${3*i}`, `tag ${3*i + 1}`, `tag ${3*i + 2}`]
|
assert.strictEqual(JSON.parse(localStorage.getItem("nextID")), 1000);
|
||||||
}
|
|
||||||
);
|
|
||||||
saveReviewsToStorage(reviews);
|
|
||||||
assert.deepEqual(getReviewsFromStorage(), reviews);
|
|
||||||
}
|
}
|
||||||
}).timeout(10000);
|
}).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