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 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
$ 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)
$ 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
$ npm test # runs mocha unit tests
Issues, features (and Prs!) are always welcomed :)
The code is available under the GPL v3 license.