admin管理员组文章数量:1186702
How do I use DI in redux-sagas. I have the following saga
export function* loadUsers() {
// Want to use something like userService.loadUsers()
}
As shown in the above code how do I inject userService into the saga, ideally I would like something like this
export function* loadUsers(userService) {
userService.loadUsers()
}
Thanks
How do I use DI in redux-sagas. I have the following saga
export function* loadUsers() {
// Want to use something like userService.loadUsers()
}
As shown in the above code how do I inject userService into the saga, ideally I would like something like this
export function* loadUsers(userService) {
userService.loadUsers()
}
Thanks
Share Improve this question edited Apr 7, 2019 at 13:32 Julien Kode 5,4691 gold badge23 silver badges38 bronze badges asked Mar 21, 2019 at 0:25 tmp devtmp dev 9,18319 gold badges66 silver badges128 bronze badges2 Answers
Reset to default 33How to inject dependencies in redux-sagas ?
How do I use DI in redux-sagas ?
You can do that with the Redux saga API, you have access to getContext
and setContext
methods that allow you to manage the context inside your sagas. Let's see how it works !
Inject your context
When you call createSagaMiddleware
you can pass a context. Later we will see how to get your context in your sagas
Here is how to inject context in redux-saga:
import { createUserService } from "...";
import createSagaMiddleware from "redux-saga";
const userService = createUserService(...);
const sagaMiddleware = createSagaMiddleware({
context: {
userService
}
});
sagaMiddleware.run(rootSaga);
Get your context
You can import getContext
from redux-saga/effects and then call getContext
with the key of the context that you want. So you will get userService
in this case.
import { getContext } from "redux-saga/effects";
export function* loadUsersSagas(action) {
const userService = yield getContext("userService");
const users = yield userService.loadUsers();
...
}
Testing
How we can test a context with redux-saga You can use redux-saga-test-plan. The goal of this library is to test our sagas.
What we want to do ?
- Add mocked context in our tests
- Ensure we try to get the right item in the context
With provide
we give [[getContext("userService"), { loadUsers }]]
that's the mocked context for userService
.
With getContext
we test if we get the right item in the context here: userService
import { getContext } from "redux-saga-test-plan/matchers";
describe("Load users", () => {
it("should load mocked users", () => {
const loadUsers = () => Promise.resolve([johnDoe]);
const action = createAction(...);
return expectSaga(loadUsersSagas, action)
.provide([[getContext("userService"), { loadUsers }]])
.getContext("userService")
.call(...)
.put(...)
.run();
});
});
I hope my answer help you
本文标签: javascriptHow to inject dependencies ( or context ) in reduxsagasStack Overflow
版权声明:本文标题:javascript - How to inject dependencies ( or context ) in redux-sagas - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738314368a2074210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论