Skip to content

goldylucks/redux-middleware-test-helper

Repository files navigation

Redux Middleware Test Helper

Build Status Coverage Status Code Climate npm downloads dependencies peerDependencies devDependencies GPLv3

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 app will only call this.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 [email protected]: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 mode

Test

$ npm test # runs mocha unit tests

Contact

Issues, features (and Prs!) are always welcomed :)

License

The code is available under the GPL v3 license.