Skip to content

Commit

Permalink
Merge pull request #8 from omarsalem7/testing-milestone02
Browse files Browse the repository at this point in the history
Testing To Do list: part 2
  • Loading branch information
omarsalem7 committed Jan 20, 2022
2 parents ab987ea + f9498d5 commit 2d053e6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
89 changes: 72 additions & 17 deletions src/__mock__/todos.test.js
Expand Up @@ -4,24 +4,24 @@
import Todos from '../todos';

document.body.innerHTML = `
<div class="container">
<h1 class="title">Today's Todo</h1>
<form>
<input class="input-todo" type="text" placeholder="Add to your List..." autofocus />
<button class="add-btn">
<i class="fas fa-location-arrow"></i>
</button>
</form>
<div class="todos">
<!-- Add Todos Automatically -->
</div>
<div class="clear">
<a class="clear-btn">Clear All</a>
</div>
</div>
`;
<div class="container">
<h1 class="title">Today's Todo</h1>
<form>
<input class="input-todo" type="text" placeholder="Add to your List..." autofocus />
<button class="add-btn">
<i class="fas fa-location-arrow"></i>
</button>
</form>
<div class="todos">
<!-- Add Todos Automatically -->
</div>
<div class="clear">
<a class="clear-btn">Clear All</a>
</div>
</div>
`;

describe('add and remove', () => {
// mock Localstorage
window.localStorage = Storage.prototype;
test('Add task', () => {
const todoList = new Todos();
Expand All @@ -43,4 +43,59 @@ describe('add and remove', () => {
expect(todoList.list).toHaveLength(2);
expect(todoList.list[1].description).toBe('task2');
});

test('remove task', () => {
const todoList = new Todos();
const newTodo = {
id: 'id4d5sa',
description: 'task3',
completed: false,
index: 3,
};
todoList.addTodo(newTodo);
todoList.removeTodo(newTodo.id);
expect(todoList.list[1].description).toBe('task2');
expect(todoList.list).toHaveLength(2);
});
});

describe('Edit test', () => {
test('Editing', () => {
const todoList = new Todos();
const newTodo3 = {
id: 'gfgdgrg',
description: 'task33',
completed: false,
index: 3,
};
todoList.addTodo(newTodo3);
todoList.editTodo(newTodo3.id, 'asd');
expect(todoList.list[2].description).toBe('asd');
expect(todoList.list).toHaveLength(3);
});
});

describe('complete test', () => {
test(' updating an items completed status', () => {
const todoList = new Todos();
const newTodo4 = {
id: 'dasasds5sa',
description: 'task5',
completed: false,
index: 4,
};
todoList.addTodo(newTodo4);
todoList.completeTodo(newTodo4.id, true);
expect(todoList.list[3].completed).toBeTruthy();
expect(todoList.list).toHaveLength(4);
});
});

describe('Clear all completed', () => {
test('clear items completed', () => {
const todoList = new Todos();
todoList.clearCompletedTodos();
expect(todoList.list).toHaveLength(3);
expect(todoList.list[1].completed).toBeFalsy();
});
});
4 changes: 2 additions & 2 deletions src/todos.js
Expand Up @@ -19,13 +19,13 @@ export default class Todos {
}

editTodo(todoId, todoDescription) {
const newData = this.list.map((todo) => {
this.list = this.list.map((todo) => {
if (todo.id === todoId) {
return { ...todo, description: todoDescription };
}
return todo;
});
localStorage.setItem('todos', JSON.stringify(newData));
localStorage.setItem('todos', JSON.stringify(this.list));
}

completeTodo(todoId, status) {
Expand Down

0 comments on commit 2d053e6

Please sign in to comment.