1+ // LICENSE : MIT
2+ "use strict" ;
3+ const assert = require ( "power-assert" ) ;
4+ import applyMiddleware from "../../src/redux/apply-middleware" ;
5+ import Dispatcher from "../../src/redux/Dispatcher" ;
6+ import timestamp from "../../src/redux/timestamp" ;
7+ import createLogger from "../../src/redux/logger" ;
8+ describe ( "middleware" , function ( ) {
9+ it ( "could apply logger middleware" , function ( ) {
10+ const dispatcher = new Dispatcher ( ) ;
11+ const logs = [ ] ;
12+ const con = {
13+ log ( message ) {
14+ logs . push ( message ) ;
15+ }
16+ } ;
17+ const middleware = createLogger ( {
18+ logger : con
19+ } ) ;
20+ // state
21+ const state = { } ;
22+ const middlewareAPI = {
23+ getState ( ) {
24+ return state ;
25+ } ,
26+ dispatch ( action ) {
27+ dispatcher . dispatch ( action ) ;
28+ }
29+ } ;
30+ const dispatchWithMiddleware = applyMiddleware ( middleware ) ( middlewareAPI ) ;
31+ const action = { type : "FOO" } ;
32+ // then
33+ dispatcher . onDispatch ( payload => {
34+ if ( payload . type === "FOO" ) {
35+ state . isUpdated = true ;
36+ }
37+ } ) ;
38+ // when
39+ dispatchWithMiddleware ( action ) ;
40+ assert . deepEqual ( logs , [ action , state ] ) ;
41+ } ) ;
42+ it ( "could apply timestamp middleware" , function ( done ) {
43+ const dispatcher = new Dispatcher ( ) ;
44+ const dispatchWithMiddleware = applyMiddleware ( timestamp ) ( dispatcher ) ;
45+ const action = { type : "FOO" } ;
46+ // then
47+ dispatcher . onDispatch ( payload => {
48+ assert ( typeof payload . timeStamp === "number" ) ;
49+ done ( ) ;
50+ } ) ;
51+ // when
52+ dispatchWithMiddleware ( action ) ;
53+ } ) ;
54+ } ) ;
0 commit comments