0

making a request in my action. Pulling from api that need to return values from mock redux store. I have it start that request when redux mock store dispatch action, but i can't seem the values correctly when store.getActions().

I getting error store.getActions() is returning the state empty from the mock store .

For example:

simpleActions.js

export const fetchTodos = () => {
    return async (dispatch) => {
        const response = await axios.get('https://myweb.com/example.json');
        dispatch({ type: types.FETCH_TODOS_REQUEST , payload: response.data });
       }
}

simpleAction.test.js

const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)

describe('async actions', () => {
  afterEach(() => {
    fetchMock.restore()
  })

  it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', () => {
    fetchMock.getOnce('/todos', {
      body: { todos: ['do something'] },
      headers: { 'content-type': 'application/json' }
    })

    const expectedActions = [
      { type: types.FETCH_TODOS_REQUEST },
      { type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something'] } }
    ]
    const store = mockStore({ todos: [] })

    const dispatchStored = store.dispatch(actions.fetchTodos());

    return dispatchStored.then(() => {

      **const values = store.getActions();**
      This values is returning the state empty array from the mock store.
      expect(store.getActions()).toEqual(expectedActions)
    })
  })
})

How to call or mock async/await function that need to return values instead of the state empty array from the mock store. Any helps would be appreciated.

amstriker
  • 339
  • 7
  • 19
  • Where is your `FETCH_TODOS_SUCCESS` action creator? – Lin Du Oct 14 '19 at 05:36
  • @slideshowp2, I used `FETCH_TODOS_REQUEST` is returning to success for the moment . So, `FETCH_TODOS_REQUEST` is equal to `FETCH_TODOS_SUCCESS`. – amstriker Oct 15 '19 at 05:06
  • Your `fetchTodos` action creator should contain `FETCH_TODOS_REQUEST` action and `FETCH_TODOS_SUCCESS` action when I see your `expectedActions` variable. Can you provide completed code of `simpleActions.js` file? – Lin Du Oct 15 '19 at 05:09

0 Answers0