The Day TDD Started Working for Me

I've always agreed that Test Driven Development was a good idea and I should be doing it. But it's hard to do on top of learning new tools. And it's hard to do for GUIs. So it never quite happened.

However, after I worked with React and Redux a bit, and after doing a couple hello-world tests with Jest, it all fit together and actually made coding easier and more fun.

These were the parts that came together to make it work:
  1. Jest: Jest is included with create-react-app and is stupid-easy to start using.
  2. The Redux reducer, or functions used in the reducer, have to be pure functions and are therefore stupid-easy to test.
  3. I pulled some functions out of Redux Container mapStateToProps. These are also pure functions.
I also decided that the outputs from mapStateToProps had to have the same structure, and even use most of the same names, as the container component and all of it's child components. In other words, you could dump the data coming out of mapStateToProps and it would basically be the UI in text/outline form. No computation, restructuring, lookup, etc. were allowed.

So now the flow of events and data into the Redux store is all testable pure functions. And the flow of data almost to the UI is testable pure functions.

When all of my tests are green, anything that's broken in the browser is probably a simple typo in my JSX or something equally easy.

And now I have the advantages of TDD and can, for example, reorganize something in the shape of my Redux state and follow the change through the code by making all the newly-red tests green. That's easy. I no longer have to try to keep the Entire Structure of Everything in my head to make it all work again. Wheeee! :-)

Comments

Popular posts from this blog

Callback, Promise, Observable, and Doughnuts

Learn grid and grid-template-areas FIRST!!!