mercredi 29 novembre 2017

Testing Redux Thunk Action Creator

I've got a redux action creator that utilizes redux-thunk to do some logic to determine what to dispatch to the store. Its not promise-based, like an HTTP request would be, so I am having some issues with how to test it properly. Ill need a test for when the value meets the condition and for when it doesn't. Since the action creator does not return a promise, I cannot run a .then() in my test. What is the best way to test something like this?

Action Creator

export const handleSelection = (value, cacheKey) => {
return dispatch => {
    if (value === "removeFiles") {
        dispatch(getRemoveFileMetrics(cacheKey));
    }
    dispatch({ type: HANDLE_SELECTION, value });
};

};

Actions

export const getRemoveFileMetrics = cacheKey => {
return dispatch => {
    dispatch({ type: IS_FETCHING_DELETE_METRICS });
    return axios
        .get(`../GetRemoveFileMetrics`, { params: { cacheKey } })
        .then(response => {
            dispatch({ type: GET_REMOVE_FILE_METRICS, payload: response.data });
        })
        .catch(err => console.log(err));
};

};

Jest

it("should dispatch HANDLE_SELECTION when selecting operation", () => {
    const store = mockStore({});
    const value = "switchVersion";
    const expectedAction = [{
        type: MOA.HANDLE_SELECTION,
        value,
    }];
    return store.dispatch(MOA.handleSelection(value)).then(() => {
        const returnedActions = store.getActions();
        expect(returnedActions).toEqual(expectedAction);
    });
});

Aucun commentaire:

Enregistrer un commentaire