0.0.5 • Published 9 years ago
redux-middleware-test-helper v0.0.5
Redux Middleware Test Helper
Why?
In every middleware we want to test the following:
- All action types trigger 
next(action) - If an action type is relevant to this middleware, it should only call ONE RELEVANT middleware method, i.e. 
init appwill only callthis.onInitApp 
I found myself writing the same tests over and over again so I made this package to DRY my UTs a bit.
Here r some working examples of one of my production apps that uses this
Use
$ npm install --save-dev redux-middleware-test-helper# ExampleMiddleware.js
import toMiddlewareTest from 'redux-middleware-test-helper'
class ExampleMiddleware {
  toMiddleware () {
    return store => next => action => {
      if (action.type === 'init app') {
        this.onInitApp()
        next(action)
        return
      }
      if (action.type === 'end game') {
        this.onEndGame()
        next(action)
        return
      }
      next(action)
    }
  }
  onInitApp () {
  }
  onEndGame () {
  }
}
# ExampleMiddleware.spec.js
describe('exampleMiddleware', () => {
  toMiddlewareTest({
    cut: new ExampleMiddleware(),
    methods: [
      { methodName: 'onInitApp', actionType: 'init app' },
      { methodName: 'onEndGame', actionType: 'end game' },
    ],
  })
})$ mocha path/to/ExampleMiddleware.spec.js
exampleMiddleware
  toMiddleware
    ✓ should call only next(action)
    ✓ should call only next(action), onInitApp
    ✓ should call only next(action), onEndGame
    ✓ should call only next(action), onEneTypeOrAnother
    ✓ should call only next(action), onEneTypeOrAnother
5 passing (15ms)Develop
$ git clone git@github.com:goldylucks/redux-middleware-test-helper.git
$ cd redux-middleware-test-helper
$ ./scripts/gitHooks.sh # recommended, runs lint and unit tests on pre-commit 
$ npm install
$ npm run test -- -w # recommended, runs unit tests in watch modeTest
$ npm test # runs mocha unit testsContact
Issues, features (and Prs!) are always welcomed :)
License
The code is available under the GPL v3 license.