redux-mock-store icon indicating copy to clipboard operation
redux-mock-store copied to clipboard

replaceReducer() does nothing.

Open nhooey opened this issue 7 years ago • 5 comments

When replaceReducer(someReducerFunction) is run, nothing is set in the mock store. Also, mock stores don't actually have a reducer to begin with.

nhooey avatar Mar 26 '18 03:03 nhooey

@nhooey You can pass a function to the mockStore instead of object. It will receive all the dispatched actions, thus you can dynamically return new state from the dispatched actions and treat it as a reducer. Hope this will help. reference: https://github.com/arnaudbenard/redux-mock-store/pull/109

metreniuk avatar Apr 28 '18 19:04 metreniuk

I'm throwing an error when replaceReducer is called on my TypeScript fork, @jedmao/redux-mock-store.

jednano avatar Jul 12 '19 06:07 jednano

Why is there a refusal to allow this library to use a reducer as is?

baileytincher avatar Oct 18 '20 20:10 baileytincher

Any progress on that?

zgrybus avatar Nov 25 '20 10:11 zgrybus

all replaceReducer does is throw an error if you do not pass a function... https://github.com/reduxjs/redux-mock-store/blob/master/src/index.js#L66 that is it! this is a little confusing. one might expect it to do something. might be better to just remove it

You can pass a function to the mockStore instead of object. It will receive all the dispatched actions, thus you can dynamically return new state from the dispatched actions and treat it as a reducer. Hope this will help.

🤔 ...i found it useful to create a mini-redux like this.

    const initialState = { slice: [{}] }
    let currentState = initialState;

    const mockStore = configureStore([middleware])(actions => {
      const action = actions[actions.length - 1];
      currentState = { ...currentState, slice: sliceReducer(currentState.slice, action) };
      return currentState;
    });

turbobeast avatar Jun 15 '21 12:06 turbobeast