Skip to content

Commit

Permalink
feat: error if createLogger is passed to applyMiddleware (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
arbesfeld authored and imevro committed Mar 2, 2017
1 parent 55a8546 commit 19adacf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
"mocha": "3.1.2",
"nyc": "9.0.1",
"open-url": "2.0.2",
"redux": "^3.6.0",
"rimraf": "2.5.4",
"sinon": "^1.17.7",
"webpack": "1.13.3"
},
"dependencies": {
Expand Down
30 changes: 30 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,40 @@ import { expect } from 'chai';

import { repeat } from 'helpers';

import { applyMiddleware, createStore } from 'redux';

import sinon from 'sinon';

import createLogger from '../src';

context(`Helpers`, () => {
describe(`'repeat'`, () => {
it(`should repeat a string the number of indicated times`, () => {
expect(repeat(`teacher`, 3)).to.equal(`teacherteacherteacher`);
});
});
});

context('createLogger', () => {
describe('initialization', () => {
beforeEach(() => {
sinon.spy(console, 'error');
});

afterEach(() => {
console.error.restore();
});

it('should log an error if the function is passed to applyMiddleware', () => {
const store = createStore(() => ({}), applyMiddleware(createLogger));
store.dispatch({ type: 'foo' });
sinon.assert.calledOnce(console.error);
});

it('should not log an error if the correct function is passed', () => {
const store = createStore(() => ({}), applyMiddleware(createLogger()));
store.dispatch({ type: 'foo' });
sinon.assert.notCalled(console.error);
});
});
});
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ function createLogger(options = {}) {
console.error(`Option 'transformer' is deprecated, use 'stateTransformer' instead!`); // eslint-disable-line no-console
}

// Detect if 'createLogger' was passed directly to 'applyMiddleware'.
if (options.getState && options.dispatch) {
// eslint-disable-next-line no-console
console.error(`redux-logger not installed. Make sure to pass logger instance as middleware:
import createLogger from 'redux-logger';
const logger = createLogger();
const store = createStore(
reducer,
applyMiddleware(logger)
);`);

return () => next => action => next(action);
}

const logBuffer = [];

return ({ getState }) => (next) => (action) => {
Expand Down

0 comments on commit 19adacf

Please sign in to comment.