Redux Saga:使用redux-saga-test-plan和jest

时间:2017-08-14 18:29:43

标签: redux jestjs redux-saga redux-saga-test-plan

我正在试图弄清楚我是如何测试我的传奇包含一个普通的ol' javascript函数在其中。

这是我的传奇:

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.'));
    }
}

第三个const调用的函数(这实际上循环遍历从apis检索的活动并将它们组合起来):

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

最后,我的测试使用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()
});

无论出于什么原因,这个传奇立即失败了api电话,我可以让它通过,然后我加入所有时区的废话就好了。有人有任何提示吗?先谢谢你了!

1 个答案:

答案 0 :(得分:0)

问题是我忘记了sagas中的函数需要使用saga效果调用,所以对于第三个const,它需要像这样被调用:


query = N1QLQuery('SELECT airportname, city, country FROM `travel-sample` WHERE city=$my_city', my_city="London")

其他一切正常!