|
| 1 | +# redux-actions-assertions |
| 2 | +Assertions for redux actions testing |
| 3 | + |
| 4 | +This library add assertions for [redux actions](http://redux.js.org/docs/advanced/AsyncActions.html) testing. |
| 5 | +It use [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store) to mock redux store. |
| 6 | + |
| 7 | +[](https://travis-ci.org/dmitry-zaets/redux-actions-assertions) |
| 8 | +[](https://www.npmjs.com/package/redux-actions-assertions) |
| 9 | + |
| 10 | +## Supported Assertion Frameworks/Libraries: |
| 11 | +- [chai](#chai) |
| 12 | +- [expect](#expect) |
| 13 | +- [expect.js](#expectjs) |
| 14 | +- [should](#should) |
| 15 | + |
| 16 | +If you have not found assertion framework/library that you are using - you can use [pure javascript assertion](#javascript) or create an issue. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Using [npm](https://www.npmjs.org/): |
| 21 | + |
| 22 | + $ npm install --save expect-redux-actions |
| 23 | + |
| 24 | +## Register redux middlewares |
| 25 | + |
| 26 | +```js |
| 27 | +// using ES6 modules |
| 28 | +import { registerMiddlewares } from 'redux-actions-assertions'; |
| 29 | + |
| 30 | +// using CommonJS modules |
| 31 | +var registerMiddlewares = require('redux-actions-assertions').registerMiddlewares; |
| 32 | + |
| 33 | +// registration |
| 34 | +registerMiddlewares([ |
| 35 | + /* Here you need to list your middlewares */ |
| 36 | +]); |
| 37 | +``` |
| 38 | + |
| 39 | +## Register default initial store state |
| 40 | + |
| 41 | +**By using state object or function:** |
| 42 | +```js |
| 43 | +// using ES6 modules |
| 44 | +import { registerInitialStoreState } from 'redux-actions-assertions'; |
| 45 | + |
| 46 | +// using CommonJS modules |
| 47 | +var registerInitialStoreState = require('redux-actions-assertions').registerInitialStoreState; |
| 48 | + |
| 49 | +// registration |
| 50 | +registerInitialStoreState(/* default initial state object or function */); |
| 51 | +``` |
| 52 | +**By using your root reducer:** |
| 53 | +```js |
| 54 | +// using ES6 modules |
| 55 | +import { buildInitialStoreState, registerInitialStoreState } from 'redux-actions-assertions'; |
| 56 | + |
| 57 | +// using CommonJS modules |
| 58 | +var reduxActionsAssertions = require('redux-actions-assertions'); |
| 59 | +var registerInitialStoreState = reduxActionsAssertions.registerInitialStoreState; |
| 60 | + |
| 61 | +// registration |
| 62 | +registerInitialStoreState(buildInitialStoreState(/* root reducer function */)); |
| 63 | +``` |
| 64 | + |
| 65 | +## javascript |
| 66 | + |
| 67 | +### Registration |
| 68 | + |
| 69 | +For plain javasript assertions you dont need to register anything. Just import assertions in your tests: |
| 70 | + |
| 71 | +```js |
| 72 | +// using ES6 modules |
| 73 | +import assertions from 'redux-actions-assertions/assertions'; |
| 74 | + |
| 75 | +// using CommonJS modules |
| 76 | +var assertions = require('redux-actions-assertions/assertions'); |
| 77 | + |
| 78 | +// in test |
| 79 | +assertions.toDispatchActions(/**/) |
| 80 | +assertions.toDispatchActionsWithState(/**/); |
| 81 | +``` |
| 82 | + |
| 83 | +### Usage |
| 84 | + |
| 85 | +#### toDispatchActions |
| 86 | +> `toDispatchActions(action, expectedActions, callback)` |
| 87 | +
|
| 88 | +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 | + |
| 90 | +```js |
| 91 | +toDispatchActions(testActionCreator(), [{type: 'MY_ACTION_START'}], callback); |
| 92 | +``` |
| 93 | + |
| 94 | +#### toDispatchActionsWithState |
| 95 | + |
| 96 | +> `toDispatchActionsWithState(initialState, action, expectedActions, callback)` |
| 97 | +
|
| 98 | +Same as `toDispatchActions` + asserts that store initialised with `state` before `action` is dispatched. |
| 99 | + |
| 100 | +```js |
| 101 | +toDispatchActions({property: 'value'}, testActionCreator(), [{type: 'MY_ACTION_START'}], callback); |
| 102 | +``` |
| 103 | + |
| 104 | +## [chai](https://github.com/chaijs/chai) |
| 105 | + |
| 106 | +### Registration |
| 107 | + |
| 108 | +```js |
| 109 | +// using ES6 modules |
| 110 | +import { registerAssertions } from 'redux-actions-assertions/chai'; |
| 111 | + |
| 112 | +// using CommonJS modules |
| 113 | +var registerAssertions = require('redux-actions-assertions/chai').registerAssertions; |
| 114 | + |
| 115 | +// registration |
| 116 | +registerAssertions(); |
| 117 | +``` |
| 118 | + |
| 119 | +#### .to.dispatch.actions or assert.isDispatching |
| 120 | + |
| 121 | +> `expect(action).to.dispatch.actions(expectedActions, callback)` |
| 122 | +
|
| 123 | +> `action.should.dispatch.actions(expectedActions, callback)` |
| 124 | +
|
| 125 | +> `assert.isDispatching(action, expectedActions, callback)` |
| 126 | +
|
| 127 | +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. |
| 128 | + |
| 129 | +```js |
| 130 | +expect(myActionCreator()) |
| 131 | + .to.dispatch.actions({type: 'MY_ACTION_START'}, callback); |
| 132 | + |
| 133 | +myActionCreator() |
| 134 | + .should.dispatch.actions({type: 'MY_ACTION_START'}, callback); |
| 135 | + |
| 136 | +assert.isDispatching( |
| 137 | + myActionCreator(), |
| 138 | + {type: 'MY_ACTION_START'}, |
| 139 | + callback |
| 140 | +); |
| 141 | +``` |
| 142 | + |
| 143 | +#### .with.state or assert.isDispatchingWithState |
| 144 | + |
| 145 | +> `expect(action).with.state(state).to.dispatch.actions(expectedActions, callback)` |
| 146 | +
|
| 147 | +> `action.should.with.state(state).dispatch.actions(expectedActions, callback)` |
| 148 | +
|
| 149 | +> `assert.isDispatchingWithState(action, expectedActions, state, callback)` |
| 150 | +
|
| 151 | +Asserts that store initialised with `state` before `action` is dispatched. |
| 152 | +```js |
| 153 | +expect(myActionCreator()) |
| 154 | + .with.state({ property: 'value' }) |
| 155 | + .to.dispatch.actions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 156 | + |
| 157 | +myActionCreator() |
| 158 | + .should.with.({ property: 'value' }) |
| 159 | + .dispatch.actions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 160 | + |
| 161 | +assert.isDispatchingWithState( |
| 162 | + myActionCreator(), |
| 163 | + [{type: 'MY_ACTION_START'}, finishActionCreator()], |
| 164 | + { property: 'value' } |
| 165 | + callback |
| 166 | +); |
| 167 | +``` |
| 168 | + |
| 169 | +## [expect](https://github.com/mjackson/expect) |
| 170 | + |
| 171 | +### Registration |
| 172 | + |
| 173 | +```js |
| 174 | +// using ES6 modules |
| 175 | +import { registerAssertions } from 'redux-actions-assertions/expect'; |
| 176 | + |
| 177 | +// using CommonJS modules |
| 178 | +var registerAssertions = require('redux-actions-assertions/expect').registerAssertions; |
| 179 | + |
| 180 | +// registration |
| 181 | +registerAssertions(); |
| 182 | +``` |
| 183 | +### Usage |
| 184 | + |
| 185 | +#### .toDispatchActions |
| 186 | + |
| 187 | +> `expect(action).toDispatchActions(expectedActions, callback)` |
| 188 | +
|
| 189 | +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. |
| 190 | + |
| 191 | +```js |
| 192 | +expect(myActionCreator()) |
| 193 | + .toDispatchActions({type: 'MY_ACTION_START'}, callback); |
| 194 | +``` |
| 195 | + |
| 196 | +#### .withState |
| 197 | + |
| 198 | +> `expect(action).withState(state).toDispatchActions(expectedActions, callback)` |
| 199 | +
|
| 200 | +Asserts that store initialised with `state` before `action` is dispatched. |
| 201 | + |
| 202 | +```js |
| 203 | +expect(myActionCreator()) |
| 204 | + .withState({property: 'value'}) |
| 205 | + .toDispatchActions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 206 | +``` |
| 207 | + |
| 208 | +## [expect.js](https://github.com/Automattic/expect.js) |
| 209 | + |
| 210 | +### Registration |
| 211 | + |
| 212 | +```js |
| 213 | +// using ES6 modules |
| 214 | +import { registerAssertions } from 'redux-actions-assertions/expectjs'; |
| 215 | + |
| 216 | +// using CommonJS modules |
| 217 | +var registerAssertions = require('redux-actions-assertions/expectjs').registerAssertions; |
| 218 | + |
| 219 | +// registration |
| 220 | +registerAssertions(); |
| 221 | +``` |
| 222 | + |
| 223 | +### Usage |
| 224 | + |
| 225 | +#### .dispatchActions |
| 226 | + |
| 227 | +> `expect(action).to.dispatchActions(expectedActions, callback)` |
| 228 | +
|
| 229 | +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. |
| 230 | + |
| 231 | +```js |
| 232 | +expect(myActionCreator()) |
| 233 | + .to.dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 234 | +``` |
| 235 | + |
| 236 | +#### .withState |
| 237 | + |
| 238 | +> `expect(action).withState(state).to.dispatchActions(expectedActions, callback)` |
| 239 | +
|
| 240 | +Asserts that store initialised with `state` before `action` is dispatched. |
| 241 | + |
| 242 | +```js |
| 243 | +expect(myActionCreator()) |
| 244 | + .withState({property: 'value'}) |
| 245 | + .to.dispatchActions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 246 | +``` |
| 247 | + |
| 248 | +## [should](https://github.com/shouldjs/should.js) |
| 249 | + |
| 250 | +### Registration |
| 251 | + |
| 252 | +```js |
| 253 | +// using ES6 modules |
| 254 | +import { registerAssertions } from 'redux-actions-assertions/should'; |
| 255 | + |
| 256 | +// using CommonJS modules |
| 257 | +var registerAssertions = require('redux-actions-assertions/should').registerAssertions; |
| 258 | + |
| 259 | +// registration |
| 260 | +registerAssertions(); |
| 261 | +``` |
| 262 | + |
| 263 | +### Usage |
| 264 | + |
| 265 | +#### .dispatchActions |
| 266 | + |
| 267 | +> `should(action).dispatchActions(expectedActions, callback)` |
| 268 | +> `action.should.dispatchActions(expectedActions, callback)` |
| 269 | +
|
| 270 | +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. |
| 271 | + |
| 272 | +```js |
| 273 | +should(myActionCreator()) |
| 274 | + .dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 275 | + |
| 276 | +myActionCreator().should |
| 277 | + .dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 278 | +``` |
| 279 | + |
| 280 | +#### .withState or with.state |
| 281 | + |
| 282 | +> `should(action).withState(state).dispatchActions(expectedActions, callback)` |
| 283 | +> `should(action).with.state(state).dispatchActions(expectedActions, callback)` |
| 284 | +
|
| 285 | +> `action.should.withState(state).dispatchActions(expectedActions, callback)` |
| 286 | +> `action.should.with.state(state).dispatchActions(expectedActions, callback)` |
| 287 | +
|
| 288 | +Asserts that store initialised with `state` before `action` is dispatched. |
| 289 | + |
| 290 | +```js |
| 291 | +should(myActionCreator()) |
| 292 | + .withState({property: 'value'}) |
| 293 | + .dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 294 | + |
| 295 | +should(myActionCreator()) |
| 296 | + .with.state({property: 'value'}) |
| 297 | + .dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 298 | + |
| 299 | +myActionCreator().should |
| 300 | + .withState({property: 'value'}) |
| 301 | + .dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 302 | + |
| 303 | +myActionCreator().should |
| 304 | + .with.state({property: 'value'}) |
| 305 | + .dispatchActions({type: 'MY_ACTION_START'}, callback); |
| 306 | +``` |
0 commit comments