• import { applyMiddleware } from 'redux'
  • returns `true` if argument is a function
    const isFunction = arg => typeof arg === 'function'
  • The main function of redux-mock-store that creates a mock store for testing. This accepts an array of middleware as an argument.
    export default function configureStore (middlewares = []) {
  •   return function mockStore (getState = {}) {
  • This function is a mock of a redux store, without middlewares. The middlewares will be applied at the end of the function.
        function mockStoreWithoutMiddleware () {
  •       let actions = []
  •       let listeners = []
  •       const self = {
  • You can pass in an object or a function. If it's an object, getState() will return that object. If it's a function, getState() will return the function call. This is effective if you want the store to return different states. For instance, the first call to getState might return one object. The second call to getState might return another.
            getState () {
  •           return isFunction(getState) ? getState() : getState
  •         },
  • Return the log of all actions received
            getActions () {
  •           return actions
  •         },
  •         dispatch (action) {
  • all errors returned by dispatch mimic the same ones returned by redux
              if (typeof action === 'undefined') {
  •             throw new Error(
  •               'Actions may not be an undefined.'
  •             )
  •           }
  •           if (typeof action.type === 'undefined') {
  •             throw new Error(
  •               'Actions may not have an undefined "type" property. ' +
  •               'Have you misspelled a constant? ' +
  •               'Action: ' +
  •               JSON.stringify(action)
  •             )
  •           }
  • When dispatching an action, the action gets saved in a log
              actions.push(action)
  • All subscribed listeners are called
              for (let i = 0; i < listeners.length; i++) {
  •             listeners[i]()
  •           }
  • Return the action that was dispatched. redux returns the same
              return action
  •         },
  • Remove the actions log
            clearActions () {
  •           actions = []
  •         },
  •         subscribe (cb) {
  • Subscribe function calls in redux, an error is thrown if cb is not a function
              if (isFunction(cb)) {
  •             listeners.push(cb)
  •           }
  • Returns a function that can unsubscribe the cb, just like redux
              return () => {
  •             const index = listeners.indexOf(cb)
  •             if (index < 0) {
  •               return
  •             }
  •             listeners.splice(index, 1)
  •           }
  •         },
  • Add placeholder to adhere to redux api
            replaceReducer (nextReducer) {
  •           if (!isFunction(nextReducer)) {
  •             throw new Error('Expected the nextReducer to be a function.')
  •           }
  •         }
  •       }
  •       return self
  •     }
  •     const mockStoreWithMiddleware = applyMiddleware(
  •       ...middlewares
  •     )(mockStoreWithoutMiddleware)
  •     return mockStoreWithMiddleware()
  •   }
  • }