facebookarchive / redux-react-hook

React Hook for accessing state and dispatch from a Redux store
MIT License
2.16k stars 103 forks source link

testing with jest #56

Closed vladimir-ivanov closed 5 years ago

vladimir-ivanov commented 5 years ago

Are there any plans to provide mocks for the store/ dispatch functions please? Can not find any examples of how to test components which connect to the store via redux-react-hooks

Thanks

ianobermiller commented 5 years ago

We probably won't provide any mocks with the library. My preference is to add an actual provider and use the real store (or a mock store) but with all real redux-react-hook. An alternative is to use Jest to mock out the module itself, which isn't too bad because mapState returns the data you're looking for and dispatch returns a function.

Something like this, if you use create:

jest.mock('redux-react-hook', () => {
  return {
    create() {
      return {dispatch: jest.fn(), mapState: jest.fn()};
    }
  };
});
ianobermiller commented 5 years ago

For an example of testing with a real store, see the tests for redux-react-hook itself: https://github.com/facebookincubator/redux-react-hook/blob/master/src/__tests__/index-test.tsx