Skip to content

Commit 6cf2a3a

Browse files
committed
Add expect.js
1 parent 91e6281 commit 6cf2a3a

File tree

7 files changed

+110
-14
lines changed

7 files changed

+110
-14
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ It use [redux-mock-store](https://github.com/arnaudbenard/redux-mock-store) to m
1010
## Supported Assertion Frameworks/Libraries:
1111
- [expect](#expect)
1212
- [chai](#chai)
13-
- [expect.js](#expectjs) [In Progress]
14-
- [should](#should) [In Progress]
13+
- [expect.js](#expectjs)
14+
- [should](#should)
1515
- [jasmine and jest](#jasmine-and-jest) [In Progress]
1616

1717
If you have not found assertion framework/library that you are using - you still can use [pure assertion function](#javascript).
@@ -222,6 +222,31 @@ var registerAssertions = require('redux-actions-assertions/expect.js').registerA
222222
registerAssertions();
223223
```
224224

225+
### Usage
226+
227+
#### .dispatchActions
228+
229+
> `expect(action).to.dispatchActions(expectedActions, callback)`
230+
231+
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.
232+
233+
```js
234+
expect(myActionCreator())
235+
.to.dispatchActions({type: 'MY_ACTION_START'}, callback);
236+
```
237+
238+
#### .withState
239+
240+
> `expect(action).withState(state).to.dispatchActions(expectedActions, callback)`
241+
242+
Asserts that store initialised with `state` before `action` is dispatched.
243+
244+
```js
245+
expect(myActionCreator())
246+
.withState({property: 'value'})
247+
.to.dispatchActions([{type: 'MY_ACTION_START'}, finishActionCreator()], callback);
248+
```
249+
225250
## [should](https://github.com/shouldjs/should.js)
226251

227252
### Registration

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"test:general": "mocha --compilers js:babel-register --reporter spec test/*.js",
99
"test:chai": "mocha --compilers js:babel-register --reporter spec test/chai/*.js",
1010
"test:expect": "mocha --compilers js:babel-register --reporter spec test/expect/*.js",
11+
"test:expectjs": "mocha --compilers js:babel-register --reporter spec test/expect.js/*.js",
1112
"test:should": "mocha --compilers js:babel-register --reporter spec test/should/*.js",
12-
"test": "npm run test:general && npm run test:chai && npm run test:expect && npm run test:should",
13+
"test": "npm run test:general && npm run test:chai && npm run test:expect && npm run test:expectjs && npm run test:should",
1314
"prepublish": "rimraf lib && babel src --out-dir lib"
1415
},
1516
"repository": {

src/actionWithInitialState.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function ActionWithInitialState(action, state) {
2+
this.action = action;
3+
this.state = state;
4+
}
5+
export default ActionWithInitialState;

src/expect.js/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import expect from 'expect.js';
2+
import assertions from '../assertions';
3+
import ActionWithInitialState from '../ActionWithInitialState';
4+
5+
function withState(state) {
6+
return expect(new ActionWithInitialState(this.obj, state));
7+
}
8+
9+
function dispatchActions(expectedActions, done) {
10+
if (this.obj instanceof ActionWithInitialState) {
11+
const action = this.obj.action;
12+
const state = this.obj.state;
13+
assertions.toDispatchActionsWithState(state, action, expectedActions, done);
14+
} else {
15+
assertions.toDispatchActions(this.obj, expectedActions, done);
16+
}
17+
}
18+
19+
function registerAssertions() {
20+
expect.Assertion.prototype.withState = withState;
21+
expect.Assertion.prototype.dispatchActions = dispatchActions;
22+
}
23+
24+
export {
25+
registerAssertions
26+
};

src/expect/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import expect from 'expect';
22
import assertions from '../assertions';
33

4+
function withState(state) {
5+
this.state = state;
6+
return this;
7+
}
8+
49
function toDispatchActions(expectedActions, done) {
510
if (this.state) {
611
return assertions.toDispatchActionsWithState(this.state, this.actual, expectedActions, done);
712
}
813
return assertions.toDispatchActions(this.actual, expectedActions, done);
914
}
1015

11-
function withState(state) {
12-
this.state = state;
13-
14-
return this;
15-
}
16-
1716
function registerAssertions() {
1817
expect.extend({
1918
toDispatchActions,

src/should/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import should from 'should';
22
import assertions from '../assertions';
3-
4-
function ActionWithInitialState(action, state) {
5-
this.action = action;
6-
this.state = state;
7-
}
3+
import ActionWithInitialState from '../ActionWithInitialState';
84

95
function withState(state) {
106
this.obj = new ActionWithInitialState(this.obj, state);

test/expect.js/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import expect from 'expect.js';
2+
import thunk from 'redux-thunk';
3+
import { registerMiddlewares } from '../../src';
4+
import { registerAssertions } from '../../src/expect.js';
5+
import actions from '../testingData/actions';
6+
7+
registerMiddlewares([thunk]);
8+
registerAssertions();
9+
10+
describe('expect.js', () => {
11+
describe('.withState', () => {
12+
it('should accept object', (done) => {
13+
expect(actions.actionCreatorWithGetState())
14+
.withState({ property: 'value' })
15+
.to.dispatchActions(actions.actionWithGetState({ property: 'value' }), done);
16+
});
17+
18+
it('should accept function', (done) => {
19+
expect(actions.actionCreatorWithGetState())
20+
.withState(() => { return { property: 'value' };})
21+
.to.dispatchActions(actions.actionWithGetState({ property: 'value' }), done);
22+
});
23+
});
24+
25+
describe('.dispatchActions', () => {
26+
it('should accept single action', (done) => {
27+
expect(actions.start()).to.dispatchActions(actions.start(), done);
28+
});
29+
30+
it('should accept array with one action', (done) => {
31+
expect(actions.start()).to.dispatchActions([actions.start()], done);
32+
});
33+
34+
it('should accept array with multiple actions', (done) => {
35+
expect(actions.asyncActionCreator())
36+
.to.dispatchActions(actions.expectedActions, done);
37+
});
38+
39+
it('should accept array with nested async action creators', (done) => {
40+
expect(actions.parentAsyncActionCreator())
41+
.to.dispatchActions(actions.expectedParentActions, done);
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)