1
1
# redux-actions-assertions
2
- Assertions for redux actions testing
2
+ Assertions for redux actions testing.
3
3
4
- This library add assertions for [ redux actions] ( http://redux.js.org/docs/advanced/AsyncActions.html ) testing.
4
+ This library adds assertions for [ redux actions] ( http://redux.js.org/docs/advanced/AsyncActions.html ) testing.
5
5
It use [ redux-mock-store] ( https://github.com/arnaudbenard/redux-mock-store ) to mock redux store.
6
6
7
7
[ ![ build status] ( https://img.shields.io/travis/dmitry-zaets/redux-actions-assertions/master.svg?style=flat-square )] ( https://travis-ci.org/dmitry-zaets/redux-actions-assertions )
8
8
[ ![ npm version] ( https://img.shields.io/npm/v/redux-actions-assertions.svg?style=flat-square )] ( https://www.npmjs.com/package/redux-actions-assertions )
9
9
10
+ ## What it does:
11
+ - [ Simplifies initial setup] ( #simplifies-initial-setup ) ;
12
+ - [ Reduces repetitive code of test methods] ( #reduces-repetitive-code-of-test-methods ) ;
13
+ - [ Allows to avoid re-testing nested action creators] ( #allows-to-avoid-re-testing-nested-action-creators ) ;
14
+
10
15
## Supported Assertion Frameworks/Libraries:
11
16
- [ chai] ( #chai )
12
17
- [ expect] ( #expect )
@@ -15,13 +20,118 @@ It use [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store) to m
15
20
16
21
If you have not found assertion framework/library that you are using - you can use [ pure javascript assertion] ( #javascript ) or create an issue.
17
22
23
+ ### Simplifies initial setup
24
+ It provides singe-time global configuration for middlewares and initial store state.
25
+
26
+ Without:
27
+ ``` javascript
28
+ const middlewares = [thunk];
29
+ const mockStore = configureStore (middlewares);
30
+ const store = mockStore ({ /* initial store object*});
31
+ ```
32
+ With:
33
+ ```javascript
34
+ registerMiddlewares([ thunk ]);
35
+ // to set custom initial state
36
+ registerInitialStoreState(/*object of function*/ );
37
+ // to generate initial state of your application
38
+ registerInitialStoreState (buildInitialStoreState (/* your root reducer*/ ));
39
+ ` ` `
40
+
41
+ ### Reduces repetitive code of test methods
42
+ It reduces boilerplate of test methods and makes testing fluent.
43
+
44
+ Without:
45
+ ` ` ` javascript
46
+ const store = mockStore (/* initial state */ );
47
+ const expectedActions = [
48
+ { type: types .FETCH_TODOS_REQUEST },
49
+ /* All expected triggered action objects */
50
+ ];
51
+ store .dispatch (fetchData ()).then (() => {
52
+ const actions = store .getActions ();
53
+ expect (actions).toEqual (expectedActions);
54
+ }).then (done).catch (done);
55
+ ` ` `
56
+
57
+ With:
58
+ ` ` ` javascript
59
+ const expectedActions = [
60
+ /* All expected triggered action objects or action creator functions*/
61
+ ];
62
+ expect (fetchData ()).toDispatchActions (expectedActions, done);
63
+ ` ` `
64
+
65
+ With using customised store state:
66
+ ` ` ` javascript
67
+ expect (fetchData ()).withState ({/* custom state*/ }).toDispatchActions (expectedActions, done);
68
+ ` ` `
69
+
70
+ ### Allows to avoid re-testing nested action creators
71
+ It allows to test only actions that needs to be tested.
72
+
73
+ **Example:**
74
+ We have two actions (A, B). Each one makes async http requests.
75
+ Action A makes request and if result is successful it triggers Action B.
76
+ Action B is also used as independent action.
77
+ Action B can be tested separately.
78
+ We don't need to test it again in Action A.
79
+
80
+ Actions:
81
+ ` ` ` javascript
82
+ function actionA () {
83
+ return dispatch => {
84
+ dispatch (actionAStart ());
85
+ return api .getA ().then (response => {
86
+ dispatch (actionAFinish (response));
87
+ dispatch (actionB ());
88
+ }).catch (err => {
89
+ dispatch (actionAFailure (err));
90
+ });
91
+ };
92
+ }
93
+
94
+ function actionB () {
95
+ return dispatch => {
96
+ dispatch (actionBStart ());
97
+ return api .getB ().then (response => {
98
+ dispatch (actionBFinish (response));
99
+ }).catch (err => {
100
+ dispatch (actionBFailure (err));
101
+ });
102
+ };
103
+ }
104
+ ` ` `
105
+
106
+ Without:
107
+ ` ` ` javascript
108
+ const expectedActions = [
109
+ { type: action_a_start },
110
+ { type: action_a_success },
111
+ { type: action_b_start }, // retesting of action B
112
+ { type: action_b_success } // retesting of action B];
113
+ const store = mockStore ({ todos: [] });
114
+ store .dispatch (actionA ()).then (() => {
115
+ expect (store .getActions ()).toEqual (expectedActions);
116
+ }).then (done).catch (done);
117
+ ` ` `
118
+
119
+ With:
120
+ ` ` ` javascript
121
+ expect (actionA ()).withState ({ todos: [] }).toDispatch ([
122
+ { type: action_a_start },
123
+ { type: action_a_success },
124
+ actionB () // just executing tested action
125
+ ], done);
126
+ ` ` `
127
+
18
128
## Installation
19
129
20
130
Using [npm](https://www.npmjs.org/):
21
131
22
132
$ npm install --save redux-actions-assertions
23
133
24
- ## Register redux middlewares
134
+ ### Redux middlewares registration
25
135
26
136
` ` ` js
27
137
// using ES6 modules
@@ -36,7 +146,7 @@ registerMiddlewares([
36
146
]);
37
147
` ` `
38
148
39
- ## Register default initial store state
149
+ ### Default initial store state registration
40
150
41
151
**By using state object or function:**
42
152
` ` ` js
@@ -88,7 +198,7 @@ assertions.toDispatchActionsWithState(/**/);
88
198
Asserts that when given ` action` is dispatched it will dispatch ` expectedActions` . ` action` can be plain object (action) or function (action creator). ` expectedActions` can be can be plain object (action) or function (action creator) or array of objects/functions.
89
199
90
200
` ` ` js
91
- toDispatchActions (testActionCreator (), [{type: ' MY_ACTION_START' }], callback);
201
+ toDispatchActions (testActionCreator (), [{ type: ' MY_ACTION_START' }], callback);
92
202
` ` `
93
203
94
204
#### toDispatchActionsWithState
@@ -98,7 +208,7 @@ toDispatchActions(testActionCreator(), [{type: 'MY_ACTION_START'}], callback);
98
208
Same as ` toDispatchActions` + asserts that store initialised with ` state` before ` action` is dispatched.
99
209
100
210
` ` ` js
101
- toDispatchActions ({property: ' value' }, testActionCreator (), [{type: ' MY_ACTION_START' }], callback);
211
+ toDispatchActions ({property: ' value' }, testActionCreator (), [{ type: ' MY_ACTION_START' }], callback);
102
212
` ` `
103
213
104
214
## [chai](https://github.com/chaijs/chai)
@@ -128,14 +238,14 @@ Asserts that when given `action` is dispatched it will dispatch `expectedActions
128
238
129
239
` ` ` js
130
240
expect (myActionCreator ())
131
- .to .dispatch .actions ({type: ' MY_ACTION_START' }, callback);
241
+ .to .dispatch .actions ({ type: ' MY_ACTION_START' }, callback);
132
242
133
243
myActionCreator ()
134
- .should .dispatch .actions ({type: ' MY_ACTION_START' }, callback);
244
+ .should .dispatch .actions ({ type: ' MY_ACTION_START' }, callback);
135
245
136
246
assert .isDispatching (
137
247
myActionCreator (),
138
- {type: ' MY_ACTION_START' },
248
+ { type: ' MY_ACTION_START' },
139
249
callback
140
250
);
141
251
` ` `
@@ -152,15 +262,15 @@ Asserts that store initialised with `state` before `action` is dispatched.
152
262
` ` ` js
153
263
expect (myActionCreator ())
154
264
.with .state ({ property: ' value' })
155
- .to .dispatch .actions ([{type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
265
+ .to .dispatch .actions ([{ type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
156
266
157
267
myActionCreator ()
158
268
.should .with .({ property: ' value' })
159
- .dispatch .actions ([{type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
269
+ .dispatch .actions ([{ type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
160
270
161
271
assert .isDispatchingWithState (
162
272
myActionCreator (),
163
- [{type: ' MY_ACTION_START' }, finishActionCreator ()],
273
+ [{ type: ' MY_ACTION_START' }, finishActionCreator ()],
164
274
{ property: ' value' }
165
275
callback
166
276
);
@@ -190,7 +300,7 @@ Asserts that when given `action` is dispatched it will dispatch `expectedActions
190
300
191
301
` ` ` js
192
302
expect (myActionCreator ())
193
- .toDispatchActions ({type: ' MY_ACTION_START' }, callback);
303
+ .toDispatchActions ({ type: ' MY_ACTION_START' }, callback);
194
304
` ` `
195
305
196
306
#### .withState
@@ -202,7 +312,7 @@ Asserts that store initialised with `state` before `action` is dispatched.
202
312
` ` ` js
203
313
expect (myActionCreator ())
204
314
.withState ({property: ' value' })
205
- .toDispatchActions ([{type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
315
+ .toDispatchActions ([{ type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
206
316
` ` `
207
317
208
318
## [expect.js](https://github.com/Automattic/expect.js)
@@ -230,7 +340,7 @@ Asserts that when given `action` is dispatched it will dispatch `expectedActions
230
340
231
341
` ` ` js
232
342
expect (myActionCreator ())
233
- .to .dispatchActions ({type: ' MY_ACTION_START' }, callback);
343
+ .to .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
234
344
` ` `
235
345
236
346
#### .withState
@@ -241,8 +351,8 @@ Asserts that store initialised with `state` before `action` is dispatched.
241
351
242
352
` ` ` js
243
353
expect (myActionCreator ())
244
- .withState ({property: ' value' })
245
- .to .dispatchActions ([{type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
354
+ .withState ({ property: ' value' })
355
+ .to .dispatchActions ([{ type: ' MY_ACTION_START' }, finishActionCreator ()], callback);
246
356
` ` `
247
357
248
358
## [should](https://github.com/shouldjs/should.js)
@@ -271,10 +381,10 @@ Asserts that when given `action` is dispatched it will dispatch `expectedActions
271
381
272
382
` ` ` js
273
383
should (myActionCreator ())
274
- .dispatchActions ({type: ' MY_ACTION_START' }, callback);
384
+ .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
275
385
276
386
myActionCreator ().should
277
- .dispatchActions ({type: ' MY_ACTION_START' }, callback);
387
+ .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
278
388
` ` `
279
389
280
390
#### .withState or with.state
@@ -289,18 +399,18 @@ Asserts that store initialised with `state` before `action` is dispatched.
289
399
290
400
` ` ` js
291
401
should (myActionCreator ())
292
- .withState ({property: ' value' })
293
- .dispatchActions ({type: ' MY_ACTION_START' }, callback);
402
+ .withState ({ property: ' value' })
403
+ .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
294
404
295
405
should (myActionCreator ())
296
- .with .state ({property: ' value' })
297
- .dispatchActions ({type: ' MY_ACTION_START' }, callback);
406
+ .with .state ({ property: ' value' })
407
+ .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
298
408
299
409
myActionCreator ().should
300
- .withState ({property: ' value' })
301
- .dispatchActions ({type: ' MY_ACTION_START' }, callback);
410
+ .withState ({ property: ' value' })
411
+ .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
302
412
303
413
myActionCreator ().should
304
- .with .state ({property: ' value' })
305
- .dispatchActions ({type: ' MY_ACTION_START' }, callback);
414
+ .with .state ({ property: ' value' })
415
+ .dispatchActions ({ type: ' MY_ACTION_START' }, callback);
306
416
` ` `
0 commit comments