@@ -3,12 +3,13 @@ import test from 'tape';
3
3
import { createStore , combineReducers , compose } from 'redux' ;
4
4
import { reduxComponentStateStore } from '../src' ;
5
5
import { addTodo } from './helpers/actionCreators' ;
6
+ import { testMiddleware } from './helpers/middleware' ;
6
7
import * as reducers from './helpers/reducers' ;
7
8
8
9
const space = ' ' ;
9
- const setup = ( ) => {
10
+ const setup = ( { globalMiddlewares = [ ] , localMiddlewares = [ ] } = { } ) => {
10
11
// create redux store
11
- const createFinalStore = compose ( reduxComponentStateStore ) ( createStore ) ;
12
+ const createFinalStore = compose ( reduxComponentStateStore ( ... globalMiddlewares ) ) ( createStore ) ;
12
13
const store = createFinalStore ( combineReducers ( reducers ) ) ;
13
14
14
15
// create redux reducer for component state
@@ -19,6 +20,7 @@ const setup = () => {
19
20
const minimalConfig = {
20
21
key : '' ,
21
22
reducer,
23
+ middlewares : localMiddlewares ,
22
24
} ;
23
25
const completeConfig = Object . assign ( { } , minimalConfig , {
24
26
initialState : {
@@ -27,6 +29,7 @@ const setup = () => {
27
29
text : 're-hydratated todo' ,
28
30
} ] ,
29
31
} ,
32
+ middlewares : localMiddlewares ,
30
33
} ) ;
31
34
return { store, reducer, subscribe, minimalConfig, completeConfig} ;
32
35
} ;
@@ -35,11 +38,19 @@ const setup = () => {
35
38
test ( 'reduxComponentStateStore' , ( { end} ) => end ( ) ) ;
36
39
37
40
test ( `.${ space } should be a function` , ( { ok, equal, end} ) => {
38
- ok ( reduxComponentStateStore , 'should exists' ) ;
41
+ ok ( reduxComponentStateStore ) ;
39
42
equal ( typeof reduxComponentStateStore , 'function' ) ;
40
43
end ( ) ;
41
44
} ) ;
42
45
46
+ test ( `.${ space } should accept 0..n middlewares functions as arguments` , ( { throws, doesNotThrow, end} ) => {
47
+ throws ( ( ) => reduxComponentStateStore ( { } ) ) ;
48
+ throws ( ( ) => reduxComponentStateStore ( [ testMiddleware ] ) ) ;
49
+ doesNotThrow ( ( ) => reduxComponentStateStore ( testMiddleware ( 'fix' ) ) ) ;
50
+ doesNotThrow ( ( ) => reduxComponentStateStore ( testMiddleware ( 'fix' ) , testMiddleware ( 'nix' ) ) ) ;
51
+ end ( ) ;
52
+ } ) ;
53
+
43
54
test ( `.${ space } should expose the public API` , ( { equal, end} ) => {
44
55
const { store } = setup ( ) ;
45
56
const methods = Object . keys ( store ) ;
@@ -56,6 +67,7 @@ test(`.${space}should expose the public API`, ({equal, end}) => {
56
67
57
68
test ( `.${ space } should require a valid configuration object` , ( { throws, doesNotThrow, end} ) => {
58
69
const { subscribe, minimalConfig, completeConfig } = setup ( ) ;
70
+
59
71
throws ( subscribe ) ;
60
72
throws ( ( ) => subscribe ( { } ) ) ;
61
73
throws ( ( ) => subscribe ( { a : 33 , b : 'temp' } ) ) ;
@@ -67,6 +79,14 @@ test(`.${space}should require a valid configuration object`, ({throws, doesNotTh
67
79
completeConfig . key = 'keyB' ;
68
80
doesNotThrow ( ( ) => subscribe ( completeConfig ) ) ;
69
81
82
+ minimalConfig . key = 'keyC' ;
83
+ minimalConfig . middlewares = [ testMiddleware ( 'suffix' ) ] ;
84
+ doesNotThrow ( ( ) => subscribe ( minimalConfig ) ) ;
85
+
86
+ minimalConfig . key = 'keyD' ;
87
+ minimalConfig . middlewares = { } ;
88
+ throws ( ( ) => subscribe ( minimalConfig ) ) ;
89
+
70
90
Reflect . deleteProperty ( completeConfig . initialState , 'todos' ) ;
71
91
throws ( ( ) => subscribe ( completeConfig ) ) ;
72
92
@@ -135,6 +155,62 @@ test(`.${space}${space}should create a new store populated with given initial st
135
155
end ( ) ;
136
156
} ) ;
137
157
158
+ test ( `.${ space } ${ space } should support middlewares shared between all component states reducers` , ( { equal, end} ) => {
159
+ const suffix = ' tested' ;
160
+ const { subscribe, minimalConfig } = setup ( {
161
+ globalMiddlewares : [ testMiddleware ( suffix ) ] ,
162
+ } ) ;
163
+ const text = 'test todo' ;
164
+
165
+ minimalConfig . key = 'keyA' ;
166
+ const subscription = subscribe ( minimalConfig ) ;
167
+ subscription . dispatch ( addTodo ( text ) ) ;
168
+
169
+ const todos = subscription . getState ( ) . todos ;
170
+ equal ( todos . length , 1 ) ;
171
+ equal ( todos [ 0 ] . text , text + suffix ) ;
172
+
173
+ end ( ) ;
174
+ } ) ;
175
+
176
+ test ( `.${ space } ${ space } should support middlewares specific for a component state reducers` , ( { equal, end} ) => {
177
+ const suffix = ' tested' ;
178
+ const { subscribe, minimalConfig } = setup ( {
179
+ localMiddlewares : [ testMiddleware ( suffix ) ] ,
180
+ } ) ;
181
+ const text = 'test todo' ;
182
+
183
+ minimalConfig . key = 'keyA' ;
184
+ const subscription = subscribe ( minimalConfig ) ;
185
+ subscription . dispatch ( addTodo ( text ) ) ;
186
+
187
+ const todos = subscription . getState ( ) . todos ;
188
+ equal ( todos . length , 1 ) ;
189
+ equal ( todos [ 0 ] . text , text + suffix ) ;
190
+
191
+ end ( ) ;
192
+ } ) ;
193
+
194
+ test ( `.${ space } ${ space } should support middlewares both shared and specific for a component state reducers` , ( { equal, end} ) => {
195
+ const suffix = ' tested' ;
196
+ const globalSuffix = ' global' ;
197
+ const { subscribe, minimalConfig } = setup ( {
198
+ globalMiddlewares : [ testMiddleware ( globalSuffix ) ] ,
199
+ localMiddlewares : [ testMiddleware ( suffix ) ] ,
200
+ } ) ;
201
+ const text = 'test todo' ;
202
+
203
+ minimalConfig . key = 'keyA' ;
204
+ const subscription = subscribe ( minimalConfig ) ;
205
+ subscription . dispatch ( addTodo ( text ) ) ;
206
+
207
+ const todos = subscription . getState ( ) . todos ;
208
+ equal ( todos . length , 1 ) ;
209
+ equal ( todos [ 0 ] . text , text + globalSuffix + suffix ) ;
210
+
211
+ end ( ) ;
212
+ } ) ;
213
+
138
214
test ( `.${ space } componentState.unsubscribe()` , ( { end} ) => end ( ) ) ;
139
215
140
216
test ( `.${ space } ${ space } should remove the component store from redux` , ( { notOk, end} ) => {
0 commit comments