Skip to content

Commit fd8ce3b

Browse files
committed
Add should assertions
1 parent 5d9603c commit fd8ce3b

File tree

5 files changed

+97
-27
lines changed

5 files changed

+97
-27
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"scripts": {
77
"lint": "eslint src test",
88
"test:general": "mocha --compilers js:babel-register --reporter spec test/*.js",
9+
"test:chai": "mocha --compilers js:babel-register --reporter spec test/chai/*.js",
910
"test:expect": "mocha --compilers js:babel-register --reporter spec test/expect/*.js",
10-
"test": "npm run test:general && npm run test:expect",
11+
"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",
1113
"prepublish": "rimraf lib && babel src --out-dir lib"
1214
},
1315
"repository": {

src/chai/index.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import assertions from '../assertions';
33

44
function registerAssertions() {
55
chai.use((_chai, utils) => {
6-
_chai.Assertion.addProperty('with');
7-
8-
function withProperty() {
9-
utils.flag(this, 'with', true);
10-
}
11-
126
function stateMethod(stateValue) {
13-
if (!utils.flag(this, 'with')) {
14-
throw new Error('"state" should be used after "with"');
15-
}
167
utils.flag(this, 'state', stateValue);
178
}
189

@@ -24,6 +15,7 @@ function registerAssertions() {
2415
if (!utils.flag(this, 'dispatch')) {
2516
throw new Error('"actions" should be used after "dispatch"');
2617
}
18+
2719
const state = utils.flag(this, 'state');
2820
if (state) {
2921
assertions.toDispatchActionsWithState(state, this._obj, expectedActions, done);
@@ -43,7 +35,6 @@ function registerAssertions() {
4335
.to.dispatch.actions(expectedActions, done);
4436
}
4537

46-
_chai.Assertion.addProperty('with', withProperty);
4738
_chai.Assertion.addChainableMethod('state', stateMethod);
4839
_chai.Assertion.addProperty('dispatch', dispatchProperty);
4940
_chai.Assertion.addMethod('actions', dispatchActionsMethod);

src/should/index.js

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

test/expect/index.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,14 @@ registerMiddlewares([thunk]);
88
registerAssertions();
99

1010
describe('expect', () => {
11-
describe('with initial state', () => {
12-
it('should accept object and setup getState', (done) => {
13-
expect(actions.actionCreatorWithGetState())
14-
.withState({ property: 'value' })
15-
.toDispatchActions(actions.actionWithGetState({ property: 'value' }), done);
16-
});
17-
18-
it('should accept function and setup getState', (done) => {
19-
expect(actions.actionCreatorWithGetState())
20-
.withState(() => { return { property: 'value' };})
21-
.toDispatchActions(actions.actionWithGetState({ property: 'value' }), done);
22-
});
23-
});
24-
2511
describe('.withState', () => {
26-
it('should accept object and setup getState', (done) => {
12+
it('should accept object', (done) => {
2713
expect(actions.actionCreatorWithGetState())
2814
.withState({ property: 'value' })
2915
.toDispatchActions(actions.actionWithGetState({ property: 'value' }), done);
3016
});
3117

32-
it('should accept function and setup getState', (done) => {
18+
it('should accept function', (done) => {
3319
expect(actions.actionCreatorWithGetState())
3420
.withState(() => { return { property: 'value' };})
3521
.toDispatchActions(actions.actionWithGetState({ property: 'value' }), done);

test/should/index.js

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

0 commit comments

Comments
 (0)