vendredi 17 juillet 2020

React/Redux Saga testing - error handling

If this is my saga

function* filtersAreaSagas() {
  while (true) {
    const action: IFiltersAreaVerticalsOpenChanged = yield take(
      FiltersAreaActionType.FILTERS_AREA_VERTICALS_OPEN_CHANGED,
    );

    if (!action.payload.open) {
      continue;
    }

    yield put(filtersAreaVerticalsLoadingChanged(true));
    const verticalsAction = yield call(getVerticals);
    yield put(verticalsAction);
    yield put(filtersAreaVerticalsLoadingChanged(false));
  }

and this is my test

    it("should dispatch the verticals loading changed action with false payload", async () => {
      sagas.start(filtersAreaSagas);
      sagas.dispatch(filtersAreaVerticalsOpenChanged(true));
      expect(sagas).toEqual(filtersAreaVerticalsLoadingChanged(false));
    });

Why is only the first action (filtersAreaVerticalsLoadingChanged) being called? getVerticals is try/catch and throwing an error, but i am expecting

yield put(verticalsAction);

to dispatch an action as well as

put(filtersAreaVerticalsLoadingChanged(false));

but neither appears in calledActions on the sagaTester, I am new to react and only reason i can think of is the error, but from what i have read it should still be dispatched? Thank you.

SagaTester {
    +   "actionLookups": Object {
    +     "FILTERS_AREA_VERTICALS_LOADING_CHANGED": Object {
    +       "callback": [Function anonymous],
    +       "count": 1,
    +       "promise": Promise {},
    +       "reject": [Function anonymous],
    +     },
    +     "FILTERS_AREA_VERTICALS_OPEN_CHANGED": Object {
    +       "callback": [Function anonymous],
    +       "count": 1,
    +       "promise": Promise {},
    +       "reject": [Function anonymous],
    +     },
    +   },
    +   "calledActions": Array [
          Object {
            "payload": Object {
    -     "loading": false,
    +         "open": true,
            },
    +       "type": "FILTERS_AREA_VERTICALS_OPEN_CHANGED",
    +     },
    +     Object {
    +       "payload": Object {
    +         "loading": true,
    +       },
            "type": "FILTERS_AREA_VERTICALS_LOADING_CHANGED",
    +     },
    +   ],
    +   "sagaMiddleware": [Function sagaMiddleware],
    +   "store": Object {
    +     "dispatch": [Function anonymous],
    +     "getState": [Function getState],
    +     "replaceReducer": [Function replaceReducer],
    +     "subscribe": [Function subscribe],
    +     Symbol(observable): [Function observable],
    +   },

Aucun commentaire:

Enregistrer un commentaire