jestjs - Redux Saga: Testing plain javascript function inside my saga with redux-saga-test-plan and jest -
i trying figure out how can test saga contains regular ol' javascript function in it.
here saga:
export function* watchgetactivities() { yield takelatest(actiontypes.get_activities, getactivitiessaga); } function* getactivitiessaga() { try { const activities = yield call(api.getactivities); const timezone= yield call(gettimezone); const activitywithtimezone=attachtimezonetoactivities(activities.data,timezone.data); yield put(getactivitessuccess(activitywithtimezone)); } catch (e) { yield put(getactivitiesfailure()); yield put(showmodal(modaltypes.error, 'could not retrieve activities.')); } }
the function third const calls (this loops through activities retrieved apis , combines them):
export const attachtimezonetoactivities= (activities,timezones)=>{ activities.foreach(function (activity) { activity['timezone']=getactivitytimezone(timezones,activity.start_epoch_ms) }) return activities; }
and finally, test using redux-saga-test-plan:
it('fetches activities activities api', () => { const fakeactivity = { data: {foo: 'bar' } }; const faketimezone= { data: {timezone: 'denver' } }; const fakeattachtimezonetoactivities={foo:'bar',timezone: 'denver'}; return expectsaga(watchgetactivities, api) .provide([ [call(api.getactivities), fakeactivity], [call(gettimezone),faketimezone], [matchers.call.fn(attachtimezonetoactivities), fakeattachtimezonetoactivities] [matchers.call.fn(getactivitytimezone), 'denver'] ]) .put(activity.getactivitessuccess({ foo: 'bar',timezone:'denver'})) .dispatch(activity.getactivities()) .silentrun() });
for whatever reason, saga goes failing api call, pass before added in timezone nonsense fine. have tips? thank in advanced!
the issue forgot functions in sagas need called using saga effects third const needed called so:
const activitywithtimezone= yield call(attachtimezonetoactivities, activities.data,timezone.data);
everything else works correctly!
Comments
Post a Comment