forked from makeomatic/redux-thunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (80 loc) · 2.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import chai from 'chai';
import thunkMiddleware from '../src/index';
describe('thunk middleware', () => {
const doDispatch = () => {};
const doGetState = () => {};
const type = 'FSA';
const nextHandler = thunkMiddleware({ dispatch: doDispatch, getState: doGetState });
it('must return a function to handle next', () => {
chai.assert.isFunction(nextHandler);
chai.assert.strictEqual(nextHandler.length, 1);
});
describe('handle next', () => {
it('must return a function to handle action', () => {
const actionHandler = nextHandler();
chai.assert.isFunction(actionHandler);
chai.assert.strictEqual(actionHandler.length, 1);
});
describe('handle action', () => {
it('must run the given action function with dispatch and getState', done => {
const actionHandler = nextHandler();
actionHandler((dispatch, getState) => {
chai.assert.strictEqual(dispatch, doDispatch);
chai.assert.strictEqual(getState, doGetState);
done();
});
});
it('must run the given FSA action with dispatch and getState', done => {
const actionObj = {};
const actionHandler = nextHandler(action => {
chai.assert.strictEqual(action.type, type);
chai.assert.strictEqual(action.payload, actionObj);
done();
});
actionHandler({
type,
payload: (dispatch, getState) => {
chai.assert.strictEqual(dispatch, doDispatch);
chai.assert.strictEqual(getState, doGetState);
return actionObj;
},
});
});
it('must pass action to next if not a function', done => {
const actionObj = {};
const actionHandler = nextHandler(action => {
chai.assert.strictEqual(action, actionObj);
done();
});
actionHandler(actionObj);
});
it('must return the return value of next if not a function', () => {
const expected = 'redux';
const actionHandler = nextHandler(() => expected);
const outcome = actionHandler();
chai.assert.strictEqual(outcome, expected);
});
it('must return value as expected if a function', () => {
const expected = 'rocks';
const actionHandler = nextHandler();
const outcome = actionHandler(() => expected);
chai.assert.strictEqual(outcome, expected);
});
it('must be invoked synchronously if a function', () => {
const actionHandler = nextHandler();
let mutated = 0;
actionHandler(() => mutated++);
chai.assert.strictEqual(mutated, 1);
});
});
});
describe('handle errors', () => {
it('must throw if argument is non-object', done => {
try {
thunkMiddleware();
} catch (err) {
done();
}
});
});
});