Filter the Todos List Between Completed, Not Completed, and All

Story: As a user, I want to be able to filter All, Active, and Completed todos.

MainSection component

Should render correctly

It should include a completed radio-button filter

Write the test.

diff --git a/tests/todomvc/components/MainSection.spec.js b/tests/todomvc/components/MainSection.spec.js
index 9ffc1de..259b510 100644
--- a/tests/todomvc/components/MainSection.spec.js
+++ b/tests/todomvc/components/MainSection.spec.js
@@ -59,6 +59,18 @@ describe('MainSection component', () => {
       const footer = component.children('Footer');

       expect(footer).to.have.length(1);
+    });
+
+    it('Should include a completed radio-button filter', () => {
+      const {component} = setup();
+      const radio_buttons = component.children('input');
+
+      expect(radio_buttons).to.have.length(3);
+      radio_buttons.map(radio_button => expect(radio_button.props().type).to.equal('radio'));
+      radio_buttons.map(radio_button => expect(radio_button.props().name).to.equal('complete_status'));
+      expect(radio_buttons.nodes[0].props.value).to.equal('show_all');
+      expect(radio_buttons.nodes[1].props.value).to.equal('show_completed');
+      expect(radio_buttons.nodes[2].props.value).to.equal('show_not_completed')
     })
   })
 });
(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.

Should behave correctly

It should show the filtered list of Todos

We will set a state of five todos, two 'completed' and three 'not completed'. Testing will be performed in four phases:

  • Initial render should display all five todos with both completed and not completed status.

  • After the simulation of change of 'show only completed' radio button, render should display only two completed todos.

  • After the simulation of change of 'show only not completed' radio button, render should display only three not completed todos.

  • After the simulation of change of 'show all' radio button, render should display all five todos.

Write the test.

Notes:

  • with .simulate() method from Enzyme package we are providing a second (optional) parameter, a mock event object that will be merged with the event object passed to the handlers. This is the value of the radio button we are trying to produce. We do this because .simulate() doesn't create the event object in our case.

  • In the first and the fourth phase of the testing we want to assert that todos list contains both completed and not completed todos. We are basing our assertion on the facts that:

    • OR(T1, T2, ..., Tn) == true if-and-only-if at least one of (T1, ..., Tn) is true

    • AND(T1, T2, ..., Tn) == false if-and-only-if at least one of (T1, ..., Tn) is false

Run the test and watch it fail.

Write the code to make the test pass.

Run the test and watch it pass.

Commit changes.

Run It!

Now let's see what this looks like.

Open http://localhost:4000arrow-up-right in your web browser.

You should immediately see the three-radiobutton set being set to show all todos.

Create a couple of todos and set some of them to be completed

Set the visibility filter to completed only. Check what you see in the todos list.

Set the visibility filter to not completed only. Check what you see in the todos list.

Last updated

Was this helpful?