Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6ee0b9d

Browse files
committedJul 15, 2015
Merge pull request #8 from acdlite/ide-redux-1
Update for Redux 1.0
2 parents a2d4b1e + 3d96a41 commit 6ee0b9d

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed
 

‎src/__tests__/promiseMiddleware-test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import promiseMiddleware from '../';
22
import { spy } from 'sinon';
33

44
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+
}
512

613
describe('promiseMiddleware', () => {
714
let baseDispatch;
@@ -11,7 +18,10 @@ describe('promiseMiddleware', () => {
1118

1219
beforeEach(() => {
1320
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+
};
1525
foobar = { foo: 'bar' };
1626
err = new Error();
1727
});
@@ -61,4 +71,13 @@ describe('promiseMiddleware', () => {
6171
payload: foobar
6272
});
6373
});
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+
});
6483
});

‎src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ function isPromise(val) {
44
return val && typeof val.then === 'function';
55
}
66

7-
export default function promiseMiddleware(next) {
8-
return action => {
7+
export default function promiseMiddleware({ dispatch }) {
8+
return next => action => {
99
if (!isFSA(action)) {
1010
return isPromise(action)
11-
? action.then(next)
11+
? action.then(dispatch)
1212
: next(action);
1313
}
1414

1515
return isPromise(action.payload)
1616
? 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 })
1919
)
2020
: next(action);
2121
};

0 commit comments

Comments
 (0)
Failed to load comments.