0

I am trying to figure out how it is that I can test my saga that contains a regular ol' javascript function in it.

Here is my 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 that the third const calls (this essentially loops through the activities retrieved from the apis and combines them):

export const attachTimeZoneToActivities= (activities,timezones)=>{
    activities.forEach(function (activity) {
        activity['timezone']=getActivityTimeZone(timezones,activity.start_epoch_ms)
    })
return activities;
}

And finally, my test using redux-saga-test-plan:

it('fetches activities from the 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, the saga immediately goes to failing the api call, I could get it to pass before I added in all the timezone nonsense just fine. Anyone have any tips? Thank you in advanced!

1 Answers1

0

The issue was that I forgot that functions in sagas need to be called using saga effects so for the third const it needed to be called like so:

const activityWithTimezone= yield call(attachTimeZoneToActivities, activities.data,timezone.data);

Everything else works correctly!