Taking the previous example further, let's say that after each save, we want to dispatch some action to notify the Store that the fetch has succeeded (we'll omit the failure case for the moment).
We could pass the Store's
dispatch function to the Generator. Then the
Generator could invoke it after receiving the fetch response:
However, this solution has the same drawbacks as invoking functions directly from inside the Generator (as discussed in the previous section). If we want to test that
the dispatch after receiving the AJAX response, we'll need again to mock the
The library provides, for this purpose, another function
put which creates the dispatch
Now, we can test the Generator easily as in the previous section
Note how we pass the fake response to the Generator via its
next method. Outside the
middleware environment, we have total control over the Generator, we can simulate a
real environment by mocking results and resuming the Generator with them. Mocking
data is a lot easier than mocking functions and spying calls.