redux-saga-testing
Version:
A no-brainer way of testing your Sagas
38 lines (30 loc) • 1.28 kB
text/typescript
import sagaHelper from "../helper";
import { call, put } from "redux-saga/effects";
import "jest";
const api = jest.fn();
const someAction = () => ({ type: "SOME_ACTION", payload: "foo" });
function* mySaga() {
yield call(api);
yield put(someAction());
}
describe("When testing a very simple Saga", () => {
const it = sagaHelper(mySaga());
it("should have called the mock API first", result => {
// Here we test that the generator did run the "call" function, with the "api" as an argument.
// The api funtion is NOT called.
expect(result).toEqual(call(api));
// It's very important to understand that the generator ran the 'call' function,
// which only describes what it does, and that the API itself is never called.
// This is what we are testing here: (but you don't need to test that in your own tests)
expect(api).not.toHaveBeenCalled();
});
it("and then trigger an action", result => {
// We then test that on the next step some action is called
// Here, obviously, 'someAction' is called but it doesn't have any effect
// since it only returns an object describing the action
expect(result).toEqual(put(someAction()));
});
it("and then nothing", result => {
expect(result).toBeUndefined();
});
});