Unit Testing: back to basic

I was recently playing with a simple React component and writing Unit Tests for that. The component was very simple like adding an user with name and age.

No alt text provided for this image

I was writing the test later because, initially unit testing was not the focus.

By looking at the component you will see that there are four methods and addUserHandler is the most interesting method where we have some input validation logic. So, naturally, you will try to test that with different combinations of enteredUsername and enteredAge to satisfy all the combinations. I found it difficult to check React's state and solutions available over internet were bit tricky. Using React Testing Library there was no straight forward solution for that. However, in the first run I wrote some of them but, accessing state mandated it to be converted from functional to class based component, which I did not want. So, was I doing something wrong?

Yes! What I was trying to do is asserting the internal implementations of a component, which I realized again when the state variable name change forced changing of the unit tests. Since, effectively we are not changing any component behavior, hence test should not be touched while refactoring.

So, what should we test? Go back to the basic. React component is a function which returns something. So, test that something and testing will be like a breeze.

No alt text provided for this image

Note that we are testing what you see in the screen. So, you check the component with some inputs and how it gets rendered. You can check the negative test cases also in the same way.

So, the conclusion: Think what you should test, before you write the unit tests.

The full source code is available here: https://github.com/onlydevelop/react-sample01




There is a good reason behind using pure functions. You push mutations to the fringe, it then becomes easier to know where exactly the mutations to state lies. It also ensures that most of your implementation consists of pure functions which are basically an expression mapping a set of inputs to a set of possible outputs. This makes it easy and straight forward to test.

I love unit testing... My problem is I don't enjoy refactoring tests or fixing broken tests :)

To view or add a comment, sign in

More articles by Dipanjan Bhowmik

  • Cheating in Tests

    Story time! There were two brave princes who once hid in a small abandoned cottage and waiting for demons to come in…

    1 Comment
  • Why there are two ’n’s in your name?

    Interesting question or dumb? I can bet if you just pause before the immediate response and allow yourself to think…

  • Story of my inspiration

    Like many others, one of my favorite book is Refactoring to Patterns by Martin Fowler. He did an excellent advocacy of…

  • ~Working on Legacy Application

    In the beginning of my career, I was always interested in working on something new, fresh so called "Green Field"…

    2 Comments

Others also viewed

Explore content categories