Delete All Completed Todos From the List

Story: As a user, I want to be able to delete all completed todos from my todos list.

TodoMVC actions

It should create an action to delete all completed todos

Write a test.

diff --git a/tests/todomvc/actions.spec.js b/tests/todomvc/actions.spec.js
index 2ae2dec..6ac3464 100644
--- a/tests/todomvc/actions.spec.js
+++ b/tests/todomvc/actions.spec.js
@@ -45,5 +45,13 @@ describe('TodoMVC actions', () => {
     };

     expect(todomvc.actions.toggleCompleteAllTodos()).to.deep.equal(expectedAction)
+  });
+
+  it('Should create an action to delete all completed todos', () => {
+    const expectedAction = {
+      type: todomvc.types.DELETE_COMPLETED
+    };
+
+    expect(todomvc.actions.deleteCompletedTodos()).to.deep.equal(expectedAction)
   })
 });
(END)

Run the test and watch it fail.

Write the code to make the test pass.

Run the test and watch it pass.

Commit changes.

TodoMVC reducer

It should handle DELETE_COMPLETED todo

Write the test.

Run the test and watch it fail.

Write the code to make the test pass.

Run the test and watch it pass.

Commit changes.

It should render correctly

It should have a 'delete completed' button

Write the test.

Run the test and watch it fail.

Write the code to make the test pass.

Run the test and watch it pass.

Commit changes.

It should behave correctly

It should not show 'delete completed' button when there are no completed todos

We have just created a test to confirm the existence of the 'delete completed' button. Let's replace that unconditional statement with two tests that will confirm that the button will exist if and only if there is at least one completed todo.

Let's start with the case when there are no completed todos.

Write the test.

Run the test and watch it fail.

Write the code to make the test pass.

Run the test and watch it pass.

The change we just introduced in /src/todomvc/components/Footer.js should satisfy our second test case as well. Let's confirm that:

Write the test.

And run the test and watch it pass.

Commit changes.

Should call deleteCompletedTodos() when the delete completed button is clicked

Write the test

Run the test and watch it fail

Write the code to make the test pass.

Run the test and watch it pass.

Let's fix the warning the same way we did before. Write the code to make the warning go away.

Run the test and watch it pass without the warning.

Commit changes.

Run It!

Now let's see what this looks like.

Open http://localhost:4000 in your web browser.

Create a couple of todos.

Change the status to 'completed' to some of the created todos. Observe 'delete completed' button appearing.

Click the button. The completed todos will be deleted and the button itself will disappear.

Last updated

Was this helpful?