Skip to content
2 changes: 1 addition & 1 deletion Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>My ToDo List</h1>
</div>
</li>
</template>

<button id="delete-completed-tasks">Delete completed tasks</button>
</div>
</body>
</html>
8 changes: 7 additions & 1 deletion Sprint-3/todo-list/script.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ window.addEventListener("load", () => {
Todos.addTask(todos, "Do the shopping", true);

render();
const deleteButton = document.getElementById("delete-completed-tasks");
deleteButton.addEventListener("click", () => {
Todos.deleteCompleted(todos);
render();
});
});


Expand Down Expand Up @@ -73,4 +78,5 @@ function createListItem(todo, index) {
});

return li;
}
}

9 changes: 8 additions & 1 deletion Sprint-3/todo-list/todos.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export function toggleCompletedOnTask(todos, taskIndex) {
if (todos[taskIndex]) {
todos[taskIndex].completed = !todos[taskIndex].completed;
}
}
}
export function deleteCompleted(todoList) {
for (let i = todoList.length - 1; i >= 0; i--) {
if (todoList[i].completed) {
todoList.splice(i, 1);
}
}
}
24 changes: 24 additions & 0 deletions Sprint-3/todo-list/todos.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,27 @@ describe("toggleCompletedOnTask()", () => {
});
});

describe("deleteCompleted()",()=>{
test("remove all completed tasks",()=>{
const todoList=createMockTodos()
Todos.deleteCompleted(todoList)
expect(todoList).toHaveLength(2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can help to make your tests more in-depth so that you catch more bugs in your code.
Try validating more fields and testing one more scenario

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expect(todoList).toEqual([
{ task: "Task 2 description", completed: false },
{ task: "Task 4 description", completed: false },
]);
})
});
test("does nothing when there are no completed tasks", () => {
const todoList = [
{ task: "Task 1 description", completed: false },
{ task: "Task 2 description", completed: false },
];

Todos.deleteCompleted(todoList);

expect(todoList).toEqual([
{ task: "Task 1 description", completed: false },
{ task: "Task 2 description", completed: false },
]);
});
Loading