Skip to content

Commit

Permalink
add unit tests for add and remove task functions
Browse files Browse the repository at this point in the history
  • Loading branch information
amiraabouhadid committed Feb 23, 2022
1 parent 9b4d659 commit 60c9add
Show file tree
Hide file tree
Showing 9 changed files with 973 additions and 223 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
coverage
Empty file removed __mocks__/tasks.js
Empty file.
3 changes: 3 additions & 0 deletions foo-bar-baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const foo = "foo";
export const bar = () => "bar";
export default () => "baz";
53 changes: 53 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { getTasks } from "./src/modules/getTasks";
import Home from "./src/modules/Home";
class LocalStorageMock {
constructor() {
this.store = {};
}

clear() {
this.store = {};
}

getItem(key) {
return this.store[key] || null;
}

setItem(key, value) {
this.store[key] = String(value);
}

removeItem(key) {
delete this.store[key];
}
}

global.localStorage = new LocalStorageMock();
let window;
beforeEach(() => {
localStorage.clear();
});

describe("add and delete task", () => {
test("adds task", () => {
console.log("starting first test...");
let tasks;
const e = { target: { value: "buy food" } };

Home.addTask(e);
tasks = getTasks();
expect(tasks).toHaveLength(1);
});
test("deletes task", () => {
console.log("starting second test...");
let initialTasks = [
{ complete: "true", description: "buy batteries", index: "0" },
{ complete: "false", description: "buy batteries", index: "1" },
];
localStorage.setItem("tasks", JSON.stringify(initialTasks));

Home.deleteTask(initialTasks[0]);
let tasks = JSON.parse(localStorage.getItem("tasks"));
expect(tasks).toHaveLength(1);
});
});
Loading

0 comments on commit 60c9add

Please sign in to comment.