Testing
Last updated
Last updated
Redux boilerplate does not only accumulate in production code, but also in unit tests. To help you reduce it, Preboiled offers two reducer test helpers - getInitialState
and reduceActions
- which are described in the following sections.
Note that in this guide we assume you use as your testing framework. However, Preboiled doesn't depend on Jest in any way, and it should be easy to translate the examples to your testing framework of choice.
When testing reducers, you'll usually want to check if the initial state looks as expected.
makes such tests a bit more readable. It simply returns the initial state of a reducer by calling it with an undefined
state, just as in the snippet above.
It is often important to test a reducer's state after a sequence of multiple actions were dispatched, rather than just a single one. Loading flags for asynchronous actions are a common example: you'll want to ensure that the flag is set to true
when the initiating action is dispatched, but also that it's set back to false
if the corresponding success or failure action is dispatche afterwards.
Using this helper, we can condense the tests above to:
To make tests like these easier to write, Redux Preboiled offers the helper. It takes a reducer and a sequence of actions, and returns the state after all actions have been processed. It also automatically gets the reducer's initial state and passes it together with the first action in the sequence.
If you want to start the reduction from a specific starting state, you can use the helper instead.
The current set of testing helpers is still very small. If there are any other helpers you'd like to see, feel free to .
This guide concludes our tour through Redux Preboiled. For more examples, looking at the repository's directory. Reference documentation on all helpers can be found in the .