Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing To Do list: part 2 #8

Merged
merged 3 commits into from Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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