Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a30b7d

Browse files
committed
feat(redux): add action logger
1 parentc7bde51 commit2a30b7d

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+
constdefaultOptions={
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+
exportdefaultfunctioncreateLogger(options=defaultOptions){
13+
constlogger=options.logger||defaultOptions.logger;
14+
returnstore=>next=>action=>{
15+
logger.log(action);
16+
returnnext(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+
constassert=require("power-assert");
4+
import{createStore,applyMiddleware}from"redux";
5+
importcreateLoggerfrom"../../src/redux/action-logger";
6+
constinitialState={};
7+
constreducer=(state=initialState,action)=>state;
8+
9+
describe("logger-test",function(){
10+
letstore,dispatch,logs;
11+
beforeEach(()=>{
12+
logs=[];
13+
constcon={
14+
log(message){
15+
logs.push(message);
16+
}
17+
};
18+
constmiddleware=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+
constaction={type:"FOO"};
29+
dispatch(action);
30+
assert.deepEqual(logs,[action]);
31+
});
32+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp