redux-saga“ all + takeEvery”单元测试

时间:2019-10-17 20:30:24

标签: reactjs typescript redux redux-saga

我是新来的人。请帮助我进行redux-saga / effecs的单元测试。 可以说我对单独的功能具有Redux效果

export function* appEffects() {
    yield all([
        takeEvery(Types.CONFIGURE_LANGUAGE, configureLanguage),
        // other effects for this feature (removed to simplify example)
    ]);
}

function* configureLanguage(action: Actions.ConfigureLanguageAction) {
    const currentLanguage: string = 'en'; // simplified example
    yield put({ type: Types.SET_CURRENT_LANGUAGE, payload: currentLanguage });
}

我可以使用以下代码直接测试configureLanguage

import { runSaga } from 'redux-saga';


    async function recordSaga(saga: any, initialAction: any) {
        const dispatched: any = [];
        await runSaga(
            { dispatch: (action: any) => dispatched.push(action) },
            saga,
            initialAction
        ).toPromise;
        return dispatched;
    }

    it('should configure language', async () => {
        const action = appActions.service.configureLanguage('en');
        const dispatched = await recordSaga(
            configureLanguage
            action
        );
        expect(dispatched).toEqual([{ type: Types.SET_CURRENT_LANGUAGE, payload: 'en' }]);
    });

如何从configureLanguage测试appEffects?如果我尝试

const dispatched = await recordSaga(
            appEffects
            action
        );

我收到dispatched === []

1 个答案:

答案 0 :(得分:0)

您没有调用toPromise方法来返回诺言-现在您正在await中使用toPromise方法本身,因此recordSaga在{{1 }}完成,因此runSaga仍然是空的。

dispatched