Implementing test-driven development
Test-Driven Development (TDD) entails writing unit tests first and then building the code to pass. The TDD approach allows you to think about whether the code is correct for the tests you want to write. The process provides a perspective that focuses on the least amount of code needed to make tests pass. TDD is also known as Red, Green, Refactor. Red represents failing tests, Green represents passing tests, and as the name says, Refactor means refactoring the code while maintaining passing tests. A typical TDD workflow would be the following:
- Write a test.
- Run the test, expecting it to fail.
- Write the minimum amount of code to make the test pass.
- Rerun the test to verify it passes.
- Refactor the code as needed.
- Repeat steps 2 through 5 as needed.
We can use React Testing Library to drive the development of React components using the TDD approach. First, we will use TDD to build the Vote
component we introduced...