Skip to content

Commit 19adacf

Browse files
arbesfeldimevro
authored andcommitted
feat: error if createLogger is passed to applyMiddleware (LogRocket#204)
1 parent 55a8546 commit 19adacf

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
"mocha": "3.1.2",
7272
"nyc": "9.0.1",
7373
"open-url": "2.0.2",
74+
"redux": "^3.6.0",
7475
"rimraf": "2.5.4",
76+
"sinon": "^1.17.7",
7577
"webpack": "1.13.3"
7678
},
7779
"dependencies": {

spec/index.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,40 @@ import { expect } from 'chai';
22

33
import { repeat } from 'helpers';
44

5+
import { applyMiddleware, createStore } from 'redux';
6+
7+
import sinon from 'sinon';
8+
9+
import createLogger from '../src';
10+
511
context(`Helpers`, () => {
612
describe(`'repeat'`, () => {
713
it(`should repeat a string the number of indicated times`, () => {
814
expect(repeat(`teacher`, 3)).to.equal(`teacherteacherteacher`);
915
});
1016
});
1117
});
18+
19+
context('createLogger', () => {
20+
describe('initialization', () => {
21+
beforeEach(() => {
22+
sinon.spy(console, 'error');
23+
});
24+
25+
afterEach(() => {
26+
console.error.restore();
27+
});
28+
29+
it('should log an error if the function is passed to applyMiddleware', () => {
30+
const store = createStore(() => ({}), applyMiddleware(createLogger));
31+
store.dispatch({ type: 'foo' });
32+
sinon.assert.calledOnce(console.error);
33+
});
34+
35+
it('should not log an error if the correct function is passed', () => {
36+
const store = createStore(() => ({}), applyMiddleware(createLogger()));
37+
store.dispatch({ type: 'foo' });
38+
sinon.assert.notCalled(console.error);
39+
});
40+
});
41+
});

src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ function createLogger(options = {}) {
4646
console.error(`Option 'transformer' is deprecated, use 'stateTransformer' instead!`); // eslint-disable-line no-console
4747
}
4848

49+
// Detect if 'createLogger' was passed directly to 'applyMiddleware'.
50+
if (options.getState && options.dispatch) {
51+
// eslint-disable-next-line no-console
52+
console.error(`redux-logger not installed. Make sure to pass logger instance as middleware:
53+
54+
import createLogger from 'redux-logger';
55+
56+
const logger = createLogger();
57+
const store = createStore(
58+
reducer,
59+
applyMiddleware(logger)
60+
);`);
61+
62+
return () => next => action => next(action);
63+
}
64+
4965
const logBuffer = [];
5066

5167
return ({ getState }) => (next) => (action) => {

0 commit comments

Comments
 (0)