0

I would like to mock a value returned for an external saga using redux-saga-test-plan.

export function* callApi(endpoint, method, params) {
  let { accessToken } = yield select(authSelector);
  const { apiUrl } = yield select(envSelector);
  if (!accessToken) {
    accessToken = yield fetchAccessToken();
  }
  const response = yield call(api, `${apiUrl}${endpoint}`, method, params, {
    Authorization: accessToken,
  });
  return response;
}

// saga to be tested
function* saga1(params) {
    const response = yield callApi(params); // <--- mock this value
    ...
}
Faiz Maricar
  • 51
  • 1
  • 1
  • 6

1 Answers1

0

you don't need mock saga, instead of that use call from saga.

const response = yield call(callApi, params);

in your test file.

const generator = saga1();
expect(generator().next().value).toEqual(call(callApi, params));
Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37