lundi 25 novembre 2019

Unit Testing functions to check dispatch to be called

store.js

const store = createStore(state,...) 
export default store;

myfunction.js

import store from './store'

const myFunction = async ()=>{
  store.dispatch(...some actions);
};

export default myFunction;

mytest.js

import store from './store';
import myFunction from './myfunction';

describe("myFunction", ()=>{
  it("should call dispatch" ()=>{

    await myFunction()

    // test something like below
    expect(store.dispatch).toBeCalled();

 })
})

Hi I would like to have a unit test on my helper function in react/redux application. I have created a sample functions above to give you some context.

As you can see, myFunction imports store and uses store.dispatch inside. I would like to check if store.dispatch has been called or not, but I am hard time achieving this. Mocking does not seem to be working because it only mocks the instance that was imported in mytest.js not the one in myFunction.js

I have tried to spy on it like

const spyStore = jest.spyOn(store, "dispatch");

and I am getting error that says dispatch is undefined.

Any clues for this kind of situation?

I would greatly appreciate your input.

Aucun commentaire:

Enregistrer un commentaire