Skip to content

Commit

Permalink
feat: 3.0.0 with breaking change in exports
Browse files Browse the repository at this point in the history
  • Loading branch information
imevro committed Mar 23, 2017
1 parent c329b94 commit 1d12b41
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 50 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import { applyMiddleware, createStore } from 'redux';

// Logger with default options
import { logger } from 'redux-logger'
import logger from 'redux-logger'
const store = createStore(
reducer,
applyMiddleware(logger)
Expand All @@ -40,7 +40,7 @@ const store = createStore(
Or you can create your own logger with custom [options](https://github.com/evgenyrodionov/redux-logger#options):
```javascript
import { applyMiddleware, createStore } from 'redux';
import createLogger from 'redux-logger'
import { createLogger } from 'redux-logger'

const logger = createLogger({
// ...options
Expand Down Expand Up @@ -172,12 +172,14 @@ Filter states diff for certain cases.
## Recipes
### Log only in development
```javascript
import thunk from 'redux-thunk';

const middlewares = [thunk];
const middlewares = [];

if (process.env.NODE_ENV === `development`) {
const { logger } = require(`redux-logger`);
const { createLogger } = require(`redux-logger`);
const logger = createLogger({
// ...options
});

middlewares.push(logger);
}

Expand Down
22 changes: 7 additions & 15 deletions example/src/components/example.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as AuthActions from 'actions/auth';
import { setToken, removeToken, setInfo } from 'actions/auth';

import uuid from 'uuid';

Expand All @@ -10,15 +9,15 @@ const { Grid } = UI;

class Example extends Component {
updateToken() {
this.props.actions.setToken(uuid.v4());
this.props.dispatch(setToken(uuid.v4()));
}

removeToken() {
this.props.actions.removeToken();
this.props.dispatch(removeToken());
}

loadUser() {
this.props.actions.setInfo(1, `theaqua`);
this.props.dispatch(setInfo(1, `evgenyrodionov`));
}

render() {
Expand All @@ -28,7 +27,7 @@ class Example extends Component {

<Grid.Row>
<Grid.Col xs={12}>
<h2>Auth actions</h2>
<h2>Actions</h2>

<Grid.Row>
<Grid.Col sm={3}>
Expand All @@ -41,18 +40,12 @@ class Example extends Component {
<UI.Button kind="primary" block onClick={::this.loadUser}>Load user info</UI.Button>
</Grid.Col>
</Grid.Row>

<ul>
<li><strong>Remove token</strong> produces an action of <code>AUTH_REMOVE_TOKEN</code> type that is not logged because <code>predicate</code> option ignores it.</li>
<li><strong>Load user info</strong> shows the next state in green because logger uses custom <code>colors</code> option.</li>
<li><strong>Update token</strong> is logged with states difference because <code>diff</code> option is turned on. Diff is limited to current action only because of <code>diffPredicate</code> option.</li>
</ul>
</Grid.Col>
</Grid.Row>

<Grid.Row>
<Grid.Col xs={12}>
<h2>State info</h2>
<h2>State</h2>
<pre>{JSON.stringify(this.props.state, null, 2)}</pre>
</Grid.Col>
</Grid.Row>
Expand All @@ -62,6 +55,5 @@ class Example extends Component {
}

const pickState = (state) => ({ state });
const pickActions = (dispatch) => ({ actions: bindActionCreators(AuthActions, dispatch) });

export default connect(pickState, pickActions)(Example);
export default connect(pickState)(Example);
5 changes: 2 additions & 3 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'styles/base';

import React from 'react';
import { render } from 'react-dom';
import createLogger from 'redux-logger';
import { createLogger } from '../../src';

import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { Provider } from 'react-redux';
Expand All @@ -13,8 +13,7 @@ import reducers from 'reducers';
import { AUTH_REMOVE_TOKEN, AUTH_SET_TOKEN } from 'constants/auth';

const logger = createLogger({
predicate: (getState, action) => action.type !== AUTH_REMOVE_TOKEN, // log all actions except AUTH_REMOVE_TOKEN
duration: true,
collapsed: true,
});

const reducer = combineReducers(reducers);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-logger",
"version": "2.10.2",
"version": "3.0.0",
"description": "Logger for Redux",
"main": "lib/index.js",
"scripts": {
Expand Down
45 changes: 32 additions & 13 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
import { expect } from 'chai';

import { repeat } from 'helpers';
import sinon from 'sinon';

import { applyMiddleware, createStore } from 'redux';

import sinon from 'sinon';

import createLogger from '../src';
import { repeat } from 'helpers';
import logger, { createLogger } from '../src';

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

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

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

it('should log an error if the function is passed to applyMiddleware', () => {
it(`should be ok`, () => {
const store = createStore(() => ({}), applyMiddleware(logger));

store.dispatch({ type: `foo` });
sinon.assert.notCalled(console.error);
});
});
});

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

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

it(`should throw error if passed direct to applyMiddleware`, () => {
const store = createStore(() => ({}), applyMiddleware(createLogger));
store.dispatch({ type: 'foo' });

store.dispatch({ type: `foo` });
sinon.assert.calledOnce(console.error);
});

it('should not log an error if the correct function is passed', () => {
it(`should be ok`, () => {
const store = createStore(() => ({}), applyMiddleware(createLogger()));
store.dispatch({ type: 'foo' });

store.dispatch({ type: `foo` });
sinon.assert.notCalled(console.error);
});
});
Expand Down
30 changes: 18 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ function createLogger(options = {}) {

const {
logger,
transformer,
stateTransformer,
errorTransformer,
predicate,
Expand All @@ -42,30 +41,21 @@ function createLogger(options = {}) {
return () => next => action => next(action);
}

if (transformer) {
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] redux-logger not installed. Make sure to pass logger instance as middleware:
// Logger with default options
import { logger } from 'redux-logger'
const store = createStore(
reducer,
applyMiddleware(logger)
)
// Or you can create your own logger with custom options http://bit.ly/redux-logger-options
import createLogger from 'redux-logger'
const logger = createLogger({
// ...options
});
const store = createStore(
reducer,
applyMiddleware(logger)
Expand All @@ -84,6 +74,7 @@ const store = createStore(
}

const logEntry = {};

logBuffer.push(logEntry);

logEntry.started = timer.now();
Expand Down Expand Up @@ -115,11 +106,26 @@ const store = createStore(
};
}

const defaultLogger = createLogger();
const defaultLogger = ({ dispatch, getState } = {}) => {
if (typeof dispatch === `function` || typeof getState === `function`) {
return createLogger()({ dispatch, getState });
} else {
// eslint-disable-next-line no-console
console.error(`
[redux-logger v3] BREAKING CHANGE
[redux-logger v3] Since 3.0.0 redux-logger exports by default logger with default settings.
[redux-logger v3] Change
[redux-logger v3] import createLogger from 'redux-logger'
[redux-logger v3] to
[redux-logger v3] import { createLogger } from 'redux-logger'
`);
}
};

export {
defaults,
createLogger,
defaultLogger as logger,
};

export default createLogger;
export default defaultLogger;

0 comments on commit 1d12b41

Please sign in to comment.