mardi 17 décembre 2019

async action called in other async action tests

I have issue with test for async action that contain a call to other async action and I am getting

TypeError: Cannot read property 'then' of undefined

The actions are:

export function fetchSettings() {
    return dispatch => {
        dispatch({
            type: FETCH_SETTINGS,
        });
        return getSettings('settings')
            .then(response => dispatch(fetchSettingsSuccess(response)))
            .catch(error => dispatch(fetchSettingsFailure(error)));
    };
}

function fetchSettingsSuccess(response) {
    return {
        type: FETCH_SETTINGS_SUCCESS,
        response,
    };
}

export function disconnectAccount(account) {
    return dispatch => {
        dispatch({
            type: DISCONNECT_ACCOUNT,
        });
        return deleteAccountConnection(account)
            .then(response => dispatch(disconnectAccountSuccess(response)))
            .catch(error => dispatch(disconnectAccountFailure(error)));
    };
}

function disconnectAccountSuccess(response) {
    return dispatch => {
        dispatch({
            type: DISCONNECT_ACCOUNT_SUCCESS,
            response,
        });
        dispatch(fetchSettings());
    };
}

and tests I wrote are:

import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';

...

describe('action tests', () => {
    it('should dispatch action DISCONNECT_ACCOUNT and then DISCONNECT_ACCOUNT_SUCCESS',
        async () => {
            deleteAccountConnection.mockResolvedValue('settings');
            const expectedActions = [
                {
                    type: DISCONNECT_ACCOUNT,
                },
                {
                    type: DISCONNECT_ACCOUNT_SUCCESS,
                    response: 'settings',
                },
                {
                    type: FETCH_SETTINGS,
                },
                {
                    type: FETCH_SETTINGS_SUCCESS,
                    response: 'settings',
                },
            ];
            const store = mockStore();
            await store.dispatch(disconnectAccount());
            await store.dispatch(fetchSettings());
            expect(store.getActions()).toEqual(expectedActions);
        });

I would have no problem if I would not call a fetchSettings in the action type DISCONNECT_ACCOUNT_SUCCESS, but when I am adding async call to it I am getting an error.

Aucun commentaire:

Enregistrer un commentaire