Expand file tree Collapse file tree 2 files changed +25
-6
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,13 @@ import promiseMiddleware from '../';
2
2
import { spy } from 'sinon' ;
3
3
4
4
function noop ( ) { }
5
+ const GIVE_ME_META = 'GIVE_ME_META' ;
6
+ function metaMiddleware ( ) {
7
+ return next => action =>
8
+ action . type === GIVE_ME_META
9
+ ? next ( { ...action , meta : 'here you go' } )
10
+ : next ( action ) ;
11
+ }
5
12
6
13
describe ( 'promiseMiddleware' , ( ) => {
7
14
let baseDispatch ;
@@ -11,7 +18,10 @@ describe('promiseMiddleware', () => {
11
18
12
19
beforeEach ( ( ) => {
13
20
baseDispatch = spy ( ) ;
14
- dispatch = promiseMiddleware ( baseDispatch ) ;
21
+ dispatch = function d ( action ) {
22
+ const methods = { dispatch : d , getState : noop } ;
23
+ return metaMiddleware ( ) ( promiseMiddleware ( methods ) ( baseDispatch ) ) ( action ) ;
24
+ } ;
15
25
foobar = { foo : 'bar' } ;
16
26
err = new Error ( ) ;
17
27
} ) ;
@@ -61,4 +71,13 @@ describe('promiseMiddleware', () => {
61
71
payload : foobar
62
72
} ) ;
63
73
} ) ;
74
+
75
+ it ( 'starts async dispatches from beginning of middleware chain' , async ( ) => {
76
+ await dispatch ( Promise . resolve ( { type : GIVE_ME_META } ) ) ;
77
+ dispatch ( { type : GIVE_ME_META } ) ;
78
+ expect ( baseDispatch . args . map ( args => args [ 0 ] . meta ) ) . to . eql ( [
79
+ 'here you go' ,
80
+ 'here you go'
81
+ ] ) ;
82
+ } ) ;
64
83
} ) ;
Original file line number Diff line number Diff line change @@ -4,18 +4,18 @@ function isPromise(val) {
4
4
return val && typeof val . then === 'function' ;
5
5
}
6
6
7
- export default function promiseMiddleware ( next ) {
8
- return action => {
7
+ export default function promiseMiddleware ( { dispatch } ) {
8
+ return next => action => {
9
9
if ( ! isFSA ( action ) ) {
10
10
return isPromise ( action )
11
- ? action . then ( next )
11
+ ? action . then ( dispatch )
12
12
: next ( action ) ;
13
13
}
14
14
15
15
return isPromise ( action . payload )
16
16
? action . payload . then (
17
- result => next ( { ...action , payload : result } ) ,
18
- error => next ( { ...action , payload : error , error : true } )
17
+ result => dispatch ( { ...action , payload : result } ) ,
18
+ error => dispatch ( { ...action , payload : error , error : true } )
19
19
)
20
20
: next ( action ) ;
21
21
} ;
0 commit comments