How to write high level test for saga? (Subscribe to end of side-effect handling)
See original GitHub issueCurrent examples of testing sagas are pretty low level. It gives really fine grained control over exactly what a saga does.
https://redux-saga.github.io/redux-saga/docs/advanced/Testing.html
However, I’d like to be able to test my redux setup on a much higher level. Basically how an application would actually consume it
- Create a store with some initial state
- Dispatch
actions
to it, expectingsagas
to do any async work / side effects - Let reducers transform state in response to actions dispatched directly / by sagas
- Wait until the handling of the actions has finished
- Use
selectors
to get the final state and confirm it matches expectations
However, I don’t know how to accomplish step 4 with redux saga.
With thunk this is super easy.
function incrementAsync() {
return dispatch => {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Yay! Can invoke sync or async actions with `dispatch`
dispatch(increment());
resolve()
}, 1000);
});
};
}
// Then in test setup
describe('redux store', () => {
it('increments async by 1', async () => {
const store = configureStore({counter: 1})
await store.dispatch(incrementAsync(1))
const counter = selectCounter(store.getState())
expect(counter).toBe(2)
})
})
However, it’s not clear to me how to dispatch an action and then wait until after redux-saga
has finished handling side-effects for it. Any ideas?
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Managing side-effects with redux-saga - YouTube
Practical examples of how redux- saga can help you reduce application complexity by moving side-effects management to a dedicated place.
Read more >Testing Redux-Sagas with a plan.
Here you will run the saga to the end, mocking select and call effects and asserting the put effects you are interested in....
Read more >Testing Sagas
There are two main ways to test Sagas: testing the saga generator function step-by-step or running the full saga and asserting the side...
Read more >The best way to test Redux Sagas
If we reorder the side effects, we need to fix all of our expect(gen.next(foo).value) assertions, to make sure we're passing the right return ......
Read more >Saga Middleware for Redux to Handle Side Effects
When developing front-ends, handling asynchronous behavior is always bit of a challenge. Yassine Elouafi's redux-saga provides one solution ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can also run the worker Saga directly
Read a bit more docs, maybe something like this?