kurumkan
kurumkan

Reputation: 2725

redux-mock-store getActions returns empty array

This is my action creator:

export function signinUser({login, password}){  

    return function(dispatch){      
        axios.post('/auth/signin', {login, password})
            .then((response)=>{             
                //-update state to indicate user is authenticated
                var {username, userid, token} = response.data;
                dispatch(authUser(token, username, userid));                
                console.log(1)
            })
            .catch(()=>{
                //- show error message
                dispatch(setErrorMessage('Bad Login Info'));                                
                console.log(2)
            });
    }   
}

The test:

 describe('signinUser', ()=>{

    var {signinUser} = actions;
    var arg;

    beforeEach(() => {
        arg = {
            login: 'john',
            password: '123'
        }           
    });

    afterEach(function () {
        nock.cleanAll()
    });

    it('should call setErrorMessage', ()=>{     

        nock('http://localhost:5000/auth/signin').post('')              
            .reply(401)         

        var expectedActions = [
            {
                type: 'SET_ERROR',
                payload: 'Bad Login Info'
            }
        ];

        var store = mockStore({});          
        store.dispatch(signinUser({...arg}));
        expect(store.getActions()).to.equal(expectedActions)
    });    

The output is:

2
AssertionError: expected [] to equal [ Array(1) ]

Which means - the action creator has been called - and the console.log(2) in the catch has been called! But somehow getActions returns an empty array. Please help me figure out why.

Upvotes: 3

Views: 4591

Answers (2)

Anshu Kumar
Anshu Kumar

Reputation: 11

Yes I was also facing same issue. All of my action creator was written in that form and one single action creator was not in that return format. I wasted 2 days for it. Finally I restructured action creator and it is working fine.

Upvotes: 0

kurumkan
kurumkan

Reputation: 2725

Ok the problem was: I didn't return promise in the action creator. Here is an updated version of my action creator:

export function signinUser({login, password}){  
    //you have to return this one
    return function(dispatch){      
        return axios.post('/auth/signin', {login, password})
            .then((response)=>{             
                //-update state to indicate user is authenticated
                var {username, userid, token} = response.data;
                dispatch(authUser(token, username, userid));                
                console.log(1)
            })
            .catch(()=>{
                //- show error message
                dispatch(setErrorMessage('Bad Login Info'));                                
                console.log(2)
            });
    }   
}

Upvotes: 3

Related Questions