-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
45 lines (39 loc) · 1.17 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
const expect = require('expect');
const createMockStore = require('redux-logic-test').default.createMockStore;
const createLogic = require('redux-logic').default.createLogic;
const fooLogic = createLogic({
type: 'FOO',
process(deps, dispatch, done) {
const API = deps.API;
API.get()
.then(results => {
dispatch({ type: 'FOO_SUCCESS', payload: results });
done();
});
}
});
const api = {
get() { return Promise.resolve(42); }
};
const logic = [fooLogic]; // array of logic to use/test
const injectedDeps = { // include what is needed for logic
API: api // could include mocked API for easy testing
};
const initialState = {}; // optionally set
const reducer = (state /* ,action */) => { return state; }; // optional
const store = createMockStore({
initialState,
reducer,
logic,
injectedDeps
});
store.dispatch({ type: 'FOO' }); // kick off fetching
store.dispatch({ type: 'BAR' }); // other dispatches
store.whenComplete(() => { // runs this fn when all logic is complete
console.log('actions', store.actions);
expect(store.actions).toEqual([
{ type: 'FOO' },
{ type: 'BAR' },
{ type: 'FOO_SUCCESS', payload: 42 }
]);
});