-
Notifications
You must be signed in to change notification settings - Fork 0
/
dva-test.js
118 lines (102 loc) · 2.81 KB
/
dva-test.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import expect from 'expect';
import React from 'react';
import dva from '../src/index';
function delay(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
describe('dva', () => {
it('basic', (done) => {
const app = dva();
app.model({
namespace: 'loading',
state: false,
reducers: {
show() { return true; },
hide() { return false; },
},
});
const nsAction = namespace => action => `${namespace}/${action}`;
const ADD = 'add';
const ADD_DELAY = 'addDelay';
const countAction = nsAction('count');
const loadingAction = nsAction('loading');
app.model({
namespace: 'count',
state: 0,
subscriptions: {
setup({ dispatch }) {
dispatch({ type: ADD });
},
},
reducers: {
[ADD](state, { payload }) {
return state + payload || 1;
},
},
effects: {
*[ADD_DELAY]({ payload }, { call, put }) {
yield put({ type: loadingAction('show') });
yield call(delay, 100);
yield put({ type: ADD, payload });
yield put({ type: loadingAction('hide') });
},
},
});
app.router(({ history }) => <div />);
app.start();
expect(app._store.getState().count).toEqual(1);
expect(app._store.getState().loading).toEqual(false);
app._store.dispatch({ type: countAction(ADD_DELAY), payload: 2 });
expect(app._store.getState().loading).toEqual(true);
setTimeout(() => {
expect(app._store.getState().count).toEqual(3);
expect(app._store.getState().loading).toEqual(false);
done();
}, 500);
});
it('opts.initialState', () => {
const app = dva({
initialState: { count: 1 },
});
app.model({
namespace: 'count',
state: 0
});
app.router(({ history }) => <div />);
app.start();
expect(app._store.getState().count).toEqual(1);
});
it('opts.onAction', () => {
let count;
const countMiddleware = ({ dispatch, getState }) => next => action => {
count = count + 1;
};
const app = dva({
onAction: countMiddleware,
});
app.router(({ history }) => <div />);
app.start();
count = 0;
app._store.dispatch({ type: 'test' });
expect(count).toEqual(1);
});
it('opts.onAction with array', () => {
let count;
const countMiddleware = ({ dispatch, getState }) => next => action => {
count = count + 1;
next(action);
};
const count2Middleware = ({ dispatch, getState }) => next => action => {
count = count + 2;
next(action);
};
const app = dva({
onAction: [countMiddleware, count2Middleware],
});
app.router(({ history }) => <div />);
app.start();
count = 0;
app._store.dispatch({ type: 'test' });
expect(count).toEqual(3);
});
});