-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
loading.test.js
212 lines (197 loc) · 5.39 KB
/
loading.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
beforeEach(() => {
jest.resetModules()
})
const count = {
state: 0,
reducers: {
addOne: s => s + 1
},
effects: {
async timeout() {
await setTimeout(() => {}, 1000)
}
}
}
describe('loading', () => {
test('loading.global should be false for normal dispatched action', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
dispatch.count.addOne()
expect(store.getState().loading.global).toBe(false)
})
test('loading.global should be true for dispatched effect', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
dispatch.count.timeout()
expect(store.getState().loading.global).toBe(true)
})
test('should set loading.models[name] to false', () => {
const {
init
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
expect(store.getState().loading.models.count).toBe(false)
})
test('should change the loading.models', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
dispatch.count.timeout()
expect(store.getState().loading.models.count).toBe(true)
})
test('should set loading.effects[name] to object of effects', () => {
const {
init
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
expect(store.getState().loading.effects.count.timeout).toBe(false)
})
test('should change the loading.effects', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
dispatch.count.timeout()
expect(store.getState().loading.effects.count.timeout).toBe(true)
})
test('should configure the loading name', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin({ name: 'load' })]
})
dispatch.count.addOne()
expect(store.getState().load.global).toBe(false)
})
test('should throw if loading name is not a string', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const createStore = () => init({
models: { count },
plugins: [loadingPlugin({ name: 42 })]
})
expect(createStore).toThrow()
})
test('should block items if not in whitelist', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin({
whitelist: ['some/action'],
})]
})
dispatch.count.timeout()
expect(store.getState().loading.models.count).toBe(false)
})
test('should block items if in blacklist', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const store = init({
models: { count },
plugins: [loadingPlugin({
blacklist: ['count/timeout'],
})]
})
dispatch.count.timeout()
expect(store.getState().loading.models.count).toBe(false)
})
test('should throw if whitelist is not an array', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const createStore = () => init({
models: { count },
plugins: [loadingPlugin({
whitelist: 'some/action',
})]
})
expect(createStore).toThrow()
})
test('should throw if blacklist is not an array', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const createStore = () => init({
models: { count },
plugins: [loadingPlugin({
blacklist: 'some/action',
})]
})
expect(createStore).toThrow()
})
test('should throw if contains both a whitelist & blacklist', () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const createStore = () => init({
models: { count },
plugins: [loadingPlugin({
whitelist: ['some/action'],
blacklist: ['some/action'],
})]
})
expect(createStore).toThrow()
})
test('should handle "hide" if effect throws', async () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const count = {
state: 0,
effects: {
throwError() {
throw new Error('effect error')
}
}
}
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
await dispatch.count.throwError()
expect(store.getState().loading.global).toBe(false)
})
})