mardi 5 février 2019

Testing put effect which isn't direct resuly of yielding

I was wondering how should I test those kind of sagas. I use redux-saga-test. Here is the piece of code.

I commented piece of code which is difficult to understand for me. I guess the problem is that payload for the action isn't result from calling api but instead extracted data from it. On the other hand I don't like the idea to passing storiesListResponse to the reducer. How should I approach to those kind of tests where returned value isn't from yield'ing ?

Saga to test:

export function* fetchStoriesWorker() {
  try {
    yield put({ type: type.FETCH_STORIES_START });
    const storiesListResponse = yield call(api.stories.list);

    yield put({
      type: type.FETCH_STORIES_SUCCESS,
      payload: storiesListResponse.data // payload isn't result of yielding and that's ok
    });
  } catch (error) {
    yield put({ type: type.FETCH_STORIES_FAILURE, payload: error });
  }
}

Test for saga:

  it("should fetch stories if there is no any error", () => {
    const mockData = [1, 2];
    const generator = actions.fetchStoriesWorker();
    const expect = fromGenerator(assert, generator);

    expect.next().put({ type: type.FETCH_STORIES_START });
    expect.next().call(api.stories.list);
    expect
      .next(mockData)
      .put({ type: type.FETCH_STORIES_SUCCESS, payload: mockData }); // throwing AssertionError: expected { Object (@@redux-saga/IO, combinator, ...) } to deeply equal { Object (@@redux-saga/IO, combinator, ...) }
  });

There is no problem with this approach if I start to return as a payload storiesListResponse instead of storiesListResponse.data, however I don't like it.

Aucun commentaire:

Enregistrer un commentaire