Permalink
Browse files

feat(createEpicMiddleware): warn about reusing middleware

Warn about #389
  • Loading branch information...
maxkostow authored and jayphelps committed Mar 2, 2018
1 parent 90f1928 commit e72661a8a01cae3d82e4d669fa27174a9029dbbf
Showing with 26 additions and 0 deletions.
  1. +4 −0 src/createEpicMiddleware.js
  2. +6 −0 src/utils/console.js
  3. +16 −0 test/createEpicMiddleware-spec.js
@@ -29,6 +29,10 @@ export function createEpicMiddleware(rootEpic, options = defaultOptions) {
let store;

const epicMiddleware = _store => {
if (process.env.NODE_ENV !== 'production' && store) {
// https://github.com/redux-observable/redux-observable/issues/389
require('./utils/console').warn('this middleware is already associated with a store. createEpicMiddleware should be called for every store.\n\n See https://goo.gl/2GQ7Da');
}
store = _store;

return next => {
@@ -8,3 +8,9 @@ export const deprecate = (typeof console === 'object' && typeof console.warn ===
}
}
: () => {};

export const warn = (typeof console === 'object' && typeof console.warn === 'function')
? msg => {
console.warn(`redux-observable | WARNING: ${msg}`);
}
: () => {};
@@ -29,6 +29,22 @@ describe('createEpicMiddleware', () => {
store.dispatch({ type: 'FIRST_ACTION_TO_TRIGGER_MIDDLEWARE' });
});

it('should warn about reusing the epicMiddleware', () => {
sinon.spy(console, 'warn');
const reducer = (state = [], action) => state.concat(action);
const epic = (action$, store) => action$
.ofType('PING')
::map(() => store.dispatch({ type: 'PONG' }))
::ignoreElements();

const middleware = createEpicMiddleware(epic);
createStore(reducer, applyMiddleware(middleware));
createStore(reducer, applyMiddleware(middleware));
expect(console.warn.callCount).to.equal(1);
expect(console.warn.getCall(0).args[0]).to.equal('redux-observable | WARNING: this middleware is already associated with a store. createEpicMiddleware should be called for every store.\n\n See https://goo.gl/2GQ7Da');
console.warn.restore();
});

it('should warn about improper use of dispatch function', () => {
sinon.spy(console, 'warn');
const reducer = (state = [], action) => state.concat(action);

0 comments on commit e72661a

Please sign in to comment.