Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test Thunk actions with Jest?

I'm new to unit testing Redux-Thunk async actions using Jest.

Here is my code:

export const functionA = (a, b) => (dispatch) => {
    dispatch({ type: CONSTANT_A, payload: a });
    dispatch({ type: CONSTANT_B, payload: b });
} 

How can I test this function using Jest?

like image 939
Voi Mập Avatar asked Jul 13 '17 13:07

Voi Mập


1 Answers

You have an example in the Redux docs: http://redux.js.org/docs/recipes/WritingTests.html#async-action-creators

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
const middlewares = [thunk]
const mockStore = configureMockStore(middleware)

describe('async actions', () => {
    
  it('should dispatch actions of ConstantA and ConstantB', () => {
    const expectedActions = [
      {type: CONSTANT_A, payload: 'a'},
      {type: CONSTANT_B, payload: 'b'} 
    ]

    const store = mockStore({ yourInitialState })
    store.dispatch(actions.functionA('a', 'b'))

    expect(store.getActions()).toEqual(expectedActions)
  })
})
like image 199
luanped Avatar answered Sep 21 '22 18:09

luanped