All files withDevTools.js

10% Statements 1/10
0% Branches 0/3
0% Functions 0/2
10% Lines 1/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                    4x                                        
import { INIT, DEVTOOLS } from "./constants";
import { setDevTools } from "./createDevTools";
 
/**
 *
 * @param {Function} reducer a function which takes state and action to return a next state
 * @param {Object} options options to configure Redux Dev Tools
 * @return {Function} an enhanced reducer, which will send actions and next states to Redux
 * Dev Tools. By default it connects in development and production NODE_ENVs
 */
export const withDevTools = (reducer, options = {}) => {
  const {
    envs = ["development", "production"],
    init = `${DEVTOOLS}/${INIT}`,
    ...rest
  } = options;
 
  const envFlag = envs.includes(process.env.NODE_ENV);
  const extension = setDevTools(envFlag);
 
  const devTools = extension.connect({ ...rest });
  devTools.send(init, reducer(undefined, {}));
  return (state, action) => {
    const nextState = reducer(state, action);
    devTools.send(action.type, nextState);
    return nextState;
  };
};
 
export default withDevTools;