Modularize to Facilitate Testing

The complex getPrice in the spike turned out to be only a few lines of code in stockfetch.js, but a significant part of implementation that was in the spike is not done yet. Let’s continue to modularize, making each piece of code cohesive, narrow, and focused on a single responsibility.

Design the processResponse and processError Functions

Let’s turn our attention to the two new functions called from within getPrice. We can think through the tests for these functions now a lot better than when we focused on getPrice. Let’s add a few more tests for these functions:

  • …
  • ✓ getPrice calls get on http with valid URL
  • ✓ getPrice calls get with a response handler as argument
  • ✓ getPrice registers handler for failure ...

Get Test-Driving JavaScript Applications 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.