|
| 1 | +# redux-actions-assertions |
| 2 | + |
| 3 | +Assertions for redux actions testing |
| 4 | + |
| 5 | +This library add assertions for [redux actions](http://redux.js.org/docs/advanced/AsyncActions.html) testing. |
| 6 | +It use [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store) to mock redux store. |
| 7 | + |
| 8 | +## Supported Assertion Frameworks/Libraries: |
| 9 | +- [expect](https://github.com/mjackson/expect) |
| 10 | +- [chai](https://github.com/chaijs/chai) |
| 11 | +- [In Progress] [expect.js](https://github.com/Automattic/expect.js) |
| 12 | +- [In Progress] [should](https://github.com/shouldjs/should.js) |
| 13 | +- [In Progress] [jasmine](https://github.com/jasmine/jasmine) and [jest](https://github.com/facebook/jest) |
| 14 | + |
| 15 | +If you have not found assertion framework/library that you are using - you still can use [pure assertion function](). |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Using [npm](https://www.npmjs.org/): |
| 20 | + |
| 21 | + $ npm install --save expect-redux-actions |
| 22 | + |
| 23 | +## Register redux middlewares |
| 24 | + |
| 25 | +```js |
| 26 | +// using ES6 modules |
| 27 | +import { registerMiddlewares } from 'redux-actions-assertions'; |
| 28 | + |
| 29 | +// using CommonJS modules |
| 30 | +var registerMiddlewares = require('redux-actions-assertions').registerMiddlewares; |
| 31 | + |
| 32 | +// registration |
| 33 | +registerMiddlewares([ |
| 34 | + /* Here you need to list your middlewares */ |
| 35 | +]); |
| 36 | +``` |
| 37 | + |
| 38 | +## Register default initial store state |
| 39 | + |
| 40 | +**By using state object or function:** |
| 41 | +```js |
| 42 | +// using ES6 modules |
| 43 | +import { registerInitialStoreState } from 'redux-actions-assertions'; |
| 44 | + |
| 45 | +// using CommonJS modules |
| 46 | +var registerInitialStoreState = require('redux-actions-assertions').registerInitialStoreState; |
| 47 | + |
| 48 | +// registration |
| 49 | +registerInitialStoreState(/* default initial state object or function */); |
| 50 | +``` |
| 51 | +**By using your root reducer:** |
| 52 | +```js |
| 53 | +// using ES6 modules |
| 54 | +import { buildInitialStoreState, registerInitialStoreState } from 'redux-actions-assertions'; |
| 55 | + |
| 56 | +// using CommonJS modules |
| 57 | +var reduxActionsAssertions = require('redux-actions-assertions'); |
| 58 | +var registerInitialStoreState = reduxActionsAssertions.registerInitialStoreState; |
| 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/expect').assertions; |
| 77 | + |
| 78 | +// in test |
| 79 | +assertions.toDispatchActions(/**/) |
| 80 | +assertions.toDispatchActionsWithState(/**/); |
| 81 | +``` |
| 82 | + |
| 83 | +### Usage |
| 84 | + |
| 85 | +#### toDispatchActions |
| 86 | +> `toDispatchActions(action, expectedActions, done)` |
| 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, done)` |
| 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 | +## [expect](https://github.com/mjackson/expect) |
| 105 | + |
| 106 | +### Registration |
| 107 | + |
| 108 | +```js |
| 109 | +// using ES6 modules |
| 110 | +import { registerAssertions } from 'redux-actions-assertions/expect'; |
| 111 | + |
| 112 | +// using CommonJS modules |
| 113 | +var registerAssertions = require('redux-actions-assertions/expect').registerAssertions; |
| 114 | + |
| 115 | +// registration |
| 116 | +registerAssertions(); |
| 117 | +``` |
| 118 | +### Usage |
| 119 | + |
| 120 | +#### toDispatchActions |
| 121 | + |
| 122 | +> `expect(action).toDispatchActions(expectedActions, callback)` |
| 123 | +
|
| 124 | +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. |
| 125 | + |
| 126 | +```js |
| 127 | +expect(myActionCreator()) |
| 128 | + .toDispatchActions({type: 'MY_ACTION_START'}, callback); |
| 129 | +``` |
| 130 | + |
| 131 | +#### withState |
| 132 | + |
| 133 | +> `expect(action).withState(state).toDispatchActions(expectedActions)` |
| 134 | +
|
| 135 | +Asserts that store initialised with `state` before `action` is dispatched. |
| 136 | + |
| 137 | +```js |
| 138 | +expect(myActionCreator()) |
| 139 | + .withState({property: 'value'}) |
| 140 | + .toDispatchActions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 141 | +``` |
| 142 | + |
| 143 | +## [chai](https://github.com/chaijs/chai) |
| 144 | + |
| 145 | +### Registration |
| 146 | + |
| 147 | +```js |
| 148 | +// using ES6 modules |
| 149 | +import { registerAssertions } from 'redux-actions-assertions/chai'; |
| 150 | + |
| 151 | +// using CommonJS modules |
| 152 | +var registerAssertions = require('redux-actions-assertions/chai').registerAssertions; |
| 153 | + |
| 154 | +// registration |
| 155 | +registerAssertions(); |
| 156 | +``` |
| 157 | + |
| 158 | +#### .to.dispatch.actions(assert.isDispatching) |
| 159 | + |
| 160 | +> `expect(action).to.dispatch.actions(expectedActions, callback)` |
| 161 | +> `action.should.dispatch.actions(expectedActions, callback)` |
| 162 | +> `assert.isDispatching(action, expectedActions, callback)` |
| 163 | +
|
| 164 | +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. |
| 165 | + |
| 166 | +```js |
| 167 | +expect(myActionCreator()) |
| 168 | + .to.dispatch.actions({type: 'MY_ACTION_START'}, callback); |
| 169 | + |
| 170 | +myActionCreator() |
| 171 | + .should.dispatch.actions({type: 'MY_ACTION_START'}, callback); |
| 172 | + |
| 173 | +assert.isDispatching( |
| 174 | + myActionCreator(), |
| 175 | + {type: 'MY_ACTION_START'}, |
| 176 | + callback |
| 177 | +); |
| 178 | +``` |
| 179 | + |
| 180 | +#### .with.state(assert.isDispatchingWithState) |
| 181 | + |
| 182 | +> `expect(action).with.state(state).to.dispatch.actions(expectedActions, callback)` |
| 183 | +> `action.should.with.state(state).dispatch.actions(expectedActions, callback)` |
| 184 | +> `assert.isDispatchingWithState(action, expectedActions, state, callback)` |
| 185 | +
|
| 186 | +Asserts that store initialised with `state` before `action` is dispatched. |
| 187 | +```js |
| 188 | +expect(myActionCreator()) |
| 189 | + .with.state({ property: 'value' }) |
| 190 | + .to.dispatch.actions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 191 | + |
| 192 | +myActionCreator() |
| 193 | + .should.with.({ property: 'value' }) |
| 194 | + .dispatch.actions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback); |
| 195 | + |
| 196 | +assert.isDispatchingWithState( |
| 197 | + myActionCreator(), |
| 198 | + [{type: 'MY_ACTION_START'}, finishActionCreator()], |
| 199 | + { property: 'value' } |
| 200 | + callback |
| 201 | +); |
| 202 | +``` |
| 203 | + |
| 204 | +## [expect.js](https://github.com/Automattic/expect.js) |
| 205 | + |
| 206 | +### Registration |
| 207 | + |
| 208 | +```js |
| 209 | +// using ES6 modules |
| 210 | +import { registerAssertions } from 'redux-actions-assertions/expect.js'; |
| 211 | + |
| 212 | +// using CommonJS modules |
| 213 | +var registerAssertions = require('redux-actions-assertions/expect.js').registerAssertions; |
| 214 | + |
| 215 | +// registration |
| 216 | +registerAssertions(); |
| 217 | +``` |
| 218 | + |
| 219 | +## [should](https://github.com/shouldjs/should.js) |
| 220 | + |
| 221 | +### Registration |
| 222 | + |
| 223 | +```js |
| 224 | +// using ES6 modules |
| 225 | +import { registerAssertions } from 'redux-actions-assertions/should'; |
| 226 | + |
| 227 | +// using CommonJS modules |
| 228 | +var registerAssertions = require('redux-actions-assertions/should').registerAssertions; |
| 229 | + |
| 230 | +// registration |
| 231 | +registerAssertions(); |
| 232 | +``` |
| 233 | +## [jasmine](https://github.com/jasmine/jasmine) and [jest](https://github.com/facebook/jest) |
| 234 | + |
| 235 | +### Registration |
| 236 | + |
| 237 | +```js |
| 238 | +// using ES6 modules |
| 239 | +import { registerAssertions } from 'redux-actions-assertions/jasmine'; |
| 240 | + |
| 241 | +// using CommonJS modules |
| 242 | +var registerAssertions = require('redux-actions-assertions/jasmine').registerAssertions; |
| 243 | + |
| 244 | +// registration |
| 245 | +registerAssertions(); |
| 246 | +``` |
0 commit comments