-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.model-test.js
100 lines (87 loc) · 1.96 KB
/
app.model-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
import expect from 'expect';
import React from 'react';
import dva from '../src/index';
import dvaM from '../src/mobile';
describe('app.model', () => {
it('namespace: type error', () => {
const app = dva();
expect(_ => {
app.model({});
}).toThrow(/app.model: namespace should be defined/);
expect(_ => {
app.model({
namespace: 'routing',
});
}).toThrow(/app.model: namespace should not be routing/);
const appM = dvaM();
expect(_ => {
appM.model({
namespace: 'routing',
});
}).toNotThrow();
});
it('dynamic model', () => {
let count = 0;
const app = dva();
app.model({
namespace: 'users',
state: [],
reducers: {
'add'(state, { payload }) {
return [...state, payload];
},
},
});
app.router(_ => <div />);
app.start();
// inject model
app.model({
namespace: 'tasks',
state: [],
reducers: {
'add'(state, { payload }) {
return [...state, payload];
},
},
effects: {
*'add'() {
yield 1;
count = count + 1;
},
},
subscriptions: {
setup() {
count = count + 1;
},
},
});
// subscriptions
expect(count).toEqual(1);
// reducers
app._store.dispatch({ type: 'tasks/add', payload: 'foo' });
app._store.dispatch({ type: 'users/add', payload: 'foo' });
const state = app._store.getState();
expect(state.users).toEqual(['foo']);
expect(state.tasks).toEqual(['foo']);
// effects
expect(count).toEqual(2);
});
it('don\'t inject if exists', () => {
const app = dva();
let count = 0;
const model = {
namespace: 'count',
state: 0,
subscriptions: {
setup() {
count += 1;
},
},
};
app.model(model);
app.router(() => 1);
app.start();
app.model(model);
expect(count).toEqual(1);
});
});