Strange behavior when testing sagas after `call()`.
See original GitHub issueHi,
I’m trying to test some simple sagas following the examples in this repository but the tests are giving me strange results. First of all, the code:
sagas/index.js
export function* loadApp() {
yield put(showLoading());
const { users, expenses, categories } = yield call(fetchAll);
yield put(receiveUsers(users));
yield put(receiveExpenses(expenses));
yield put(receiveCategories(categories));
yield put(finishLoading());
}
test/sagas/index.js
describe.skip('#loadApp()', function () {
const loadAppGenerator = loadApp();
const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
const expenses = [{ id: 1 }, { id: 2 }, { id: 3 }];
const categories = [{ id: 1 }, { id: 2 }, { id: 3 }];
it('should start yielding SHOW_LOADING action', function () {
const expected = put(ApplicationActions.showLoading());
const result = loadAppGenerator.next().value;
expect(result).to.deep.equal(expected);
});
it('should yield a call to api.fetchAll()', function () {
const expected = call(fetchAll);
const result = loadAppGenerator.next().value;
expect(result).to.deep.equal(expected);
});
it('should yield action RECEIVE_USERS', function () {
const expected = put(UsersActions.receiveUsers(users));
const result = loadAppGenerator.next(users).value;
expect(result).to.deep.equal(expected);
});
it('should yield action RECEIVE_EXPENSES', function () {
const expected = put(ExpensesActions.receiveExpenses(expenses));
const result = loadAppGenerator.next(expenses).value;
expect(result).to.deep.equal(expected);
});
it('should yield action RECEIVE_CATEGORIES', function () {
const expected = put(CategoriesActions.receiveCategories(categories));
const result = loadAppGenerator.next(categories).value;
expect(result).to.deep.equal(expected);
});
it('should finish yielding FINISH_LOADING action', function () {
const expected = put(ApplicationActions.finishLoading());
const result = loadAppGenerator.next().value;
expect(result).to.deep.equal(expected);
});
});
In the above example, the first two calls to next()
returns a valid state and the tests are passing ok. But, in the third test (when testing RECEIVE_USERS action), when I call loadAppGenerator.next(users).value
, it doesn’t pass the users
to the action creator. Instead of returning the expected:
PUT: {
type: RECEIVE_USERS,
payload: { users: [{id:1}, {id:2}, {id:3}] }
}
I’m getting the users as undefined:
PUT: {
type: RECEIVE_USERS,
payload: { users: undefined }
}
In other cases is like the next()
call was a bit advanced. I have more examples in expenses-api-frontend
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
React Redux Saga: weird behaviour when calling API
I'm using sagas in the React/Redux project I'm working on. It's a form to create and edit content. The form is working fine...
Read more >Testing Sagas - Redux-Saga
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 >Testing Redux-Sagas with a plan. - Medium
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 >Redux-saga and Typescript, doing it right.
Where inferred generator types fall short ... Inspecting the type of the bar value turns out to be quite weird. Indeed, it is...
Read more >Efficient Test Cases for Redux-Saga - Innominds
Thus, the native approach for testing a saga is to call the generator ... it("test redux-saga using iterator methods", async() => { const ......
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
Yes. Anytime you use the ‘return value’ of yield (e.g via assignament
val = yield something
, in an expression likeif(yield soemthing)
), you have to pass the mocked result to the nextnext
call@kumar155 Please share ur problem on something like codesandbox.io so we can help you better.