Reducing effort when constructing components
Let’s look at a couple of simple ways to reduce the amount of time and code needed for test suites like the one we’ve just built: first, extracting builder functions, and second, extracting objects to store sensible defaults for our component props.
Extracting test data builders for time and date functions
You’ve already seen how we can extract reusable functions into namespaces of their own, such as the render
, click
, and element
DOM functions. A special case of this is the builder function, which constructs objects that you’ll use in the Arrange and Act phases of your test.
The purpose of these functions is not just to remove duplication but also for simplification and to aid with comprehension.
We already have one candidate in our test suite, which is the following code:
const today = new Date(); today.setHours(9, 0, 0, 0);
We’ll update our test suite so that it uses a builder function...