vendredi 23 décembre 2016

How to test a redux action which contains setTImeout via Jasmine

I want to test some redux action which contains a setTimeout (currntly it's just a mock). But I want to know how it can be tested via jasmine, because in the official documentation I saw the they recommend to use FETCH API that returns PROMISE and we can use "then". But I want to use standard AJAX with callback.

Is it possible to test it? Thank for any help!

here is my action creators

export const ASYNC_REQUEST = "ASYNC_REQUEST";
export const ASYNC_SUCCESS = "ASYNC_SUCCESS";


export function asyncRequest() {
    return {
        type : ASYNC_REQUEST,
        payload : {
            text : "PENDING...",
            pending : true
        }
    };
}

export function asyncSuccess() {
    return {
        type : ASYNC_SUCCESS,
        payload : {
            text : "SUCCESS",
            pending : false
        }
    };
}


export function someAsyncAction(cb) {
    return (dispatch) => {
        dispatch(asyncRequest());

        setTimeout(() => {
            dispatch(asyncSuccess());
            console.log(cb);
            cb();
        }, 2000);
    };
}

here is my test (it is not working)

import configureStore from "redux-mock-store";
import thunk from "redux-thunk";

const middlewares = [ thunk ];

const mockStore = configureStore(middlewares);


import * as actionsStuff from "../actions";


describe("Async Action test", () => {
    it("should execute all async actions after call syncAction", (done) => {
        const expectedActions = [
            {
                type : actionsStuff.ASYNC_REQUEST,
                payload : {
                    text : "PENDING...",
                    pending : true
                }
            },
            {
                type : actionsStuff.ASYNC_SUCCESS,
                payload : {
                    sdf : "SUCCESS",
                    pensdfsfding : false
                }
            }
        ];

        const store = mockStore({
            "text" : "ASYNC REQUEST IS NOT INITIALIZED",
            "pending" : false
        });


        setTimeout(() => {
            expect(store.getActions()).toEqual(expectedActions);
            done();
        }, 2000);
    });
});

Aucun commentaire:

Enregistrer un commentaire