Skip to content

Commit 2a30b7d

Browse files
committed
feat(redux): add action logger
1 parent c7bde51 commit 2a30b7d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"node-fetch": "^1.3.2",
6060
"npm-run-all": "^1.2.8",
6161
"power-assert": "^1.0.0",
62+
"redux": "^3.5.2",
6263
"remark": "^4.1.1",
6364
"stemming-x-keywords": "^1.0.3",
6465
"textlint": "^6.1.0",

src/redux/action-logger.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
const defaultOptions = {
4+
// default: logger use console API
5+
logger: console
6+
};
7+
/**
8+
* create logger middleware
9+
* @param {{logger: *}} options
10+
* @returns {Function} middleware function
11+
*/
12+
export default function createLogger(options = defaultOptions) {
13+
const logger = options.logger || defaultOptions.logger;
14+
return store => next => action => {
15+
logger.log(action);
16+
return next(action);
17+
};
18+
}

test/redux/action-logger-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
const assert = require("power-assert");
4+
import {createStore, applyMiddleware} from "redux";
5+
import createLogger from "../../src/redux/action-logger";
6+
const initialState = {};
7+
const reducer = (state = initialState, action) => state;
8+
9+
describe("logger-test", function () {
10+
let store, dispatch, logs;
11+
beforeEach(() => {
12+
logs = [];
13+
const con = {
14+
log(message){
15+
logs.push(message);
16+
}
17+
};
18+
const middleware = createLogger({
19+
logger: con
20+
});
21+
store = applyMiddleware(middleware)(createStore)(reducer);
22+
dispatch = store.dispatch;
23+
});
24+
afterEach(() => {
25+
logs.length = 0;
26+
});
27+
it("should output log", function () {
28+
const action = {type: "FOO"};
29+
dispatch(action);
30+
assert.deepEqual(logs, [action]);
31+
});
32+
});

0 commit comments

Comments
 (0)