Test a Single Function
Testing user interfaces adds noise and complexity, so it’s best to test as much as you can without involving the user interface. Putting application logic in functions outside components lets us work more efficiently. In our word counter, we’ve already extracted the countWords function and placed it in a separate module:
| function countWords(text) { |
| return text ? text.match(/\w+/g).length : 0; |
| } |
| |
| export default countWords; |
This module exports the countWords function by default. It’s a good idea to test this logic on its own, as long as you’re testing meaningful functionality instead of implementation details.
Create a new __tests__ directory in the src directory. The directory ...
Get React for Real now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.