2

I have this in my react component

console.log(await store.dispatch(getAndSaveImages()));

and my test is:

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

    it("should call redux store...", () => {
        const store = mockStore({ auth: { isAuth: true } })
        store.dispatch = jest.fn(() => Promise.resolve("its working"));

        component = mount(<Provider store={store}><User history={mockedHistory}/></Provider>);

    });

The problem is that it doesnt mock the dispatch function.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • The `mockStore` doesn't actually dispatch any actions, but simply creates an array of the dispatched actions. In other words, it doesn't do anything really with the actions and doesn't update any Redux store. What are you trying to test? – Drew Reese Jan 31 '21 at 22:18
  • I'm trying to test that it actually calls the getAndSaveImages() function. Being able to access that array would be awesome. do you know how I can access the array? store.getActions() returns [] @DrewReese – behdad khameneli Jan 31 '21 at 22:24
  • README's and official documentation are generally great places to start: https://github.com/reduxjs/redux-mock-store, and failing that you can also just look at the source code in the repo to see what it is doing. It's literally `store.getActions()` and you get back the array of "dispatched" actions. See the `Asynchronous action` section as well. – Drew Reese Jan 31 '21 at 22:29

0 Answers0