@@ -6,7 +6,7 @@ import thunk from 'redux-thunk'
6
6
import mockMiddleware from './mock/middleware'
7
7
import configureStore from '../src'
8
8
9
- const mockStore = configureStore ( [ thunk ] )
9
+ const mockStore = configureStore ( )
10
10
11
11
describe ( 'redux-mock-store' , ( ) => {
12
12
describe ( 'getState' , ( ) => {
@@ -40,11 +40,21 @@ describe('redux-mock-store', () => {
40
40
} )
41
41
} )
42
42
43
- it ( 'should throw an error when action is undefined' , ( ) => {
43
+ it ( 'should throw an error when the action is undefined' , ( ) => {
44
44
const store = mockStore ( { } )
45
45
46
46
expect ( ( ) => { store . dispatch ( undefined ) } ) . toThrow (
47
- 'Actions may not be an undefined.'
47
+ 'Actions must be plain objects. ' +
48
+ 'Use custom middleware for async actions.'
49
+ )
50
+ } )
51
+
52
+ it ( 'should throw an error when the action is not a plain object' , ( ) => {
53
+ const store = mockStore ( { } )
54
+
55
+ expect ( ( ) => { store . dispatch ( ( ) => { } ) } ) . toThrow (
56
+ 'Actions must be plain objects. ' +
57
+ 'Use custom middleware for async actions.'
48
58
)
49
59
} )
50
60
@@ -70,51 +80,6 @@ describe('redux-mock-store', () => {
70
80
expect ( first ) . toBe ( action )
71
81
} )
72
82
73
- it ( 'handles async actions' , ( done ) => {
74
- function increment ( ) {
75
- return {
76
- type : 'INCREMENT_COUNTER'
77
- }
78
- }
79
-
80
- function incrementAsync ( ) {
81
- return dispatch => {
82
- return Promise . resolve ( )
83
- . then ( ( ) => dispatch ( increment ( ) ) )
84
- }
85
- }
86
-
87
- const store = mockStore ( { } )
88
-
89
- store . dispatch ( incrementAsync ( ) )
90
- . then ( ( ) => {
91
- expect ( store . getActions ( ) [ 0 ] ) . toEqual ( increment ( ) )
92
- done ( )
93
- } )
94
- } )
95
-
96
- it ( 'should call the middleware' , ( ) => {
97
- const spy = sinon . spy ( )
98
- const middlewares = [ mockMiddleware ( spy ) ]
99
- const mockStoreWithMiddleware = configureStore ( middlewares )
100
- const action = { type : 'ADD_ITEM' }
101
-
102
- const store = mockStoreWithMiddleware ( )
103
- store . dispatch ( action )
104
- expect ( spy . called ) . toBe ( true )
105
- } )
106
-
107
- it ( 'should handle when test function throws an error' , ( done ) => {
108
- const store = mockStore ( { } )
109
- const error = { error : 'Something went wrong' }
110
-
111
- store . dispatch ( ( ) => Promise . reject ( error ) )
112
- . catch ( err => {
113
- expect ( err ) . toEqual ( error )
114
- done ( )
115
- } )
116
- } )
117
-
118
83
it ( 'clears the actions' , ( ) => {
119
84
const action = { type : 'ADD_ITEM' }
120
85
const store = mockStore ( { } )
@@ -177,4 +142,52 @@ describe('redux-mock-store', () => {
177
142
expect ( ( ) => store . replaceReducer ( 123 ) )
178
143
. toThrow ( 'Expected the nextReducer to be a function.' )
179
144
} )
145
+
146
+ describe ( 'store with middleware' , ( ) => {
147
+ const mockStoreWithMiddleware = configureStore ( [ thunk ] )
148
+ it ( 'handles async actions' , ( done ) => {
149
+ function increment ( ) {
150
+ return {
151
+ type : 'INCREMENT_COUNTER'
152
+ }
153
+ }
154
+
155
+ function incrementAsync ( ) {
156
+ return dispatch => {
157
+ return Promise . resolve ( )
158
+ . then ( ( ) => dispatch ( increment ( ) ) )
159
+ }
160
+ }
161
+
162
+ const store = mockStoreWithMiddleware ( { } )
163
+
164
+ store . dispatch ( incrementAsync ( ) )
165
+ . then ( ( ) => {
166
+ expect ( store . getActions ( ) [ 0 ] ) . toEqual ( increment ( ) )
167
+ done ( )
168
+ } )
169
+ } )
170
+
171
+ it ( 'should handle when test function throws an error' , ( done ) => {
172
+ const store = mockStoreWithMiddleware ( { } )
173
+ const error = { error : 'Something went wrong' }
174
+
175
+ store . dispatch ( ( ) => Promise . reject ( error ) )
176
+ . catch ( err => {
177
+ expect ( err ) . toEqual ( error )
178
+ done ( )
179
+ } )
180
+ } )
181
+
182
+ it ( 'should call the middleware' , ( ) => {
183
+ const spy = sinon . spy ( )
184
+ const middlewares = [ mockMiddleware ( spy ) ]
185
+ const mockStoreWithCustomMiddleware = configureStore ( middlewares )
186
+ const action = { type : 'ADD_ITEM' }
187
+
188
+ const store = mockStoreWithCustomMiddleware ( )
189
+ store . dispatch ( action )
190
+ expect ( spy . called ) . toBe ( true )
191
+ } )
192
+ } )
180
193
} )
0 commit comments