Skip to content

Commit 1cd404c

Browse files
committed
no message
1 parent db41094 commit 1cd404c

File tree

12 files changed

+353
-0
lines changed

12 files changed

+353
-0
lines changed

lib/assertions/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _toDispatchActions = require('./toDispatchActions');
8+
9+
var _toDispatchActions2 = _interopRequireDefault(_toDispatchActions);
10+
11+
var _toDispatchActionsWithState = require('./toDispatchActionsWithState');
12+
13+
var _toDispatchActionsWithState2 = _interopRequireDefault(_toDispatchActionsWithState);
14+
15+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16+
17+
exports.default = { toDispatchActions: _toDispatchActions2.default, toDispatchActionsWithState: _toDispatchActionsWithState2.default };
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _initialState = require('../initialState');
8+
9+
var _initialState2 = _interopRequireDefault(_initialState);
10+
11+
var _toDispatchActionsWithState = require('./toDispatchActionsWithState');
12+
13+
var _toDispatchActionsWithState2 = _interopRequireDefault(_toDispatchActionsWithState);
14+
15+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16+
17+
function toDispatchActions(actionUnderTest, expectedActions, done, fail) {
18+
return (0, _toDispatchActionsWithState2.default)((0, _initialState2.default)(), actionUnderTest, expectedActions, done, fail);
19+
}
20+
21+
exports.default = toDispatchActions;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _lodash = require('lodash.flattendeep');
8+
9+
var _lodash2 = _interopRequireDefault(_lodash);
10+
11+
var _lodash3 = require('lodash.find');
12+
13+
var _lodash4 = _interopRequireDefault(_lodash3);
14+
15+
var _utils = require('../utils');
16+
17+
var _mockStore = require('../mockStore');
18+
19+
var _mockStore2 = _interopRequireDefault(_mockStore);
20+
21+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22+
23+
function getDispatchedActions(initialState, action) {
24+
return new Promise(function (resolve, reject) {
25+
var store = (0, _mockStore2.default)()(initialState);
26+
var dispatchResult = store.dispatch(action);
27+
28+
if (dispatchResult instanceof Promise) {
29+
dispatchResult.then(function () {
30+
resolve(store.getActions());
31+
}).catch(function (result) {
32+
reject(result);
33+
});
34+
} else {
35+
resolve(store.getActions());
36+
}
37+
});
38+
}
39+
40+
function unrollActions(initialState, expectedActions) {
41+
var promises = [];
42+
var actions = (0, _utils.toArray)(expectedActions);
43+
44+
for (var index = 0; index < actions.length; index++) {
45+
promises.push(getDispatchedActions(initialState, actions[index]));
46+
}
47+
48+
return Promise.all(promises).then(function (resultActions) {
49+
return (0, _lodash2.default)(resultActions);
50+
});
51+
}
52+
53+
function toDispatchActionsWithState(initialState, actionUnderTest, expectedActions, done, fail) {
54+
if (!(0, _utils.isFunction)(actionUnderTest) && !(0, _utils.isObject)(actionUnderTest)) {
55+
throw new Error('The "actualAction" argument must be a function or object');
56+
}
57+
58+
if (!(0, _utils.isFunction)(expectedActions) && !(0, _utils.isObject)(expectedActions)) {
59+
throw new Error('The "expectedActions" argument must be ' + 'an action creator function or an action object or an array of them');
60+
}
61+
62+
return getDispatchedActions(initialState, actionUnderTest).then(function (dispatchedActions) {
63+
return unrollActions(initialState, expectedActions).then(function (expectedUnrolledActions) {
64+
for (var index = 0; index < expectedUnrolledActions.length; index++) {
65+
if (!(0, _lodash4.default)(dispatchedActions, expectedUnrolledActions[index])) {
66+
throw new Error('Expected action ' + JSON.stringify(expectedUnrolledActions[index]) + ' was not dispatched.' + ('\nActual dispatched actions: ' + JSON.stringify(dispatchedActions)));
67+
}
68+
}
69+
if ((0, _utils.isFunction)(done)) {
70+
done();
71+
}
72+
}).catch(function (err) {
73+
if ((0, _utils.isFunction)(fail)) {
74+
fail(err);
75+
return;
76+
} else if ((0, _utils.isFunction)(done)) {
77+
done(err);
78+
return;
79+
}
80+
throw new Error(err);
81+
});
82+
});
83+
}
84+
85+
exports.default = toDispatchActionsWithState;

lib/chai/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.registerAssertions = undefined;
7+
8+
var _chai2 = require('chai');
9+
10+
var _chai3 = _interopRequireDefault(_chai2);
11+
12+
var _assertions = require('../assertions');
13+
14+
var _assertions2 = _interopRequireDefault(_assertions);
15+
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17+
18+
function registerAssertions() {
19+
_chai3.default.use(function (_chai, utils) {
20+
function stateMethod(stateValue) {
21+
utils.flag(this, 'state', stateValue);
22+
}
23+
24+
function dispatchProperty() {
25+
utils.flag(this, 'dispatch', true);
26+
}
27+
28+
function dispatchActionsMethod(expectedActions, done) {
29+
if (!utils.flag(this, 'dispatch')) {
30+
throw new Error('"actions" should be used after "dispatch"');
31+
}
32+
33+
var state = utils.flag(this, 'state');
34+
if (state) {
35+
return _assertions2.default.toDispatchActionsWithState(state, this._obj, expectedActions, done);
36+
}
37+
return _assertions2.default.toDispatchActions(this._obj, expectedActions, done);
38+
}
39+
40+
function isDispatchingWithState(actualAction, expectedActions, state, done) {
41+
new _chai.Assertion(actualAction).with.state(state).to.dispatch.actions(expectedActions, done);
42+
}
43+
44+
function isDispatching(actualAction, expectedActions, done) {
45+
new _chai.Assertion(actualAction).to.dispatch.actions(expectedActions, done);
46+
}
47+
48+
_chai.Assertion.addChainableMethod('state', stateMethod);
49+
_chai.Assertion.addProperty('dispatch', dispatchProperty);
50+
_chai.Assertion.addMethod('actions', dispatchActionsMethod);
51+
_chai.assert.isDispatching = isDispatching;
52+
_chai.assert.isDispatchingWithState = isDispatchingWithState;
53+
});
54+
}
55+
56+
exports.registerAssertions = registerAssertions;

lib/expect.js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

lib/expect/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.registerAssertions = undefined;
7+
8+
var _expect = require('expect');
9+
10+
var _expect2 = _interopRequireDefault(_expect);
11+
12+
var _assertions = require('../assertions');
13+
14+
var _assertions2 = _interopRequireDefault(_assertions);
15+
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17+
18+
function toDispatchActions(expectedActions, done) {
19+
if (this.state) {
20+
return _assertions2.default.toDispatchActionsWithState(this.state, this.actual, expectedActions, done);
21+
}
22+
return _assertions2.default.toDispatchActions(this.actual, expectedActions, done);
23+
}
24+
25+
function withState(state) {
26+
this.state = state;
27+
28+
return this;
29+
}
30+
31+
function registerAssertions() {
32+
_expect2.default.extend({
33+
toDispatchActions: toDispatchActions,
34+
withState: withState
35+
});
36+
}
37+
38+
exports.registerAssertions = registerAssertions;

lib/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.buildInitialStoreState = exports.registerInitialStoreState = exports.registerMiddlewares = undefined;
7+
8+
var _mockStore = require('./mockStore');
9+
10+
var _initialState = require('./initialState');
11+
12+
exports.registerMiddlewares = _mockStore.registerMiddlewares;
13+
exports.registerInitialStoreState = _initialState.registerInitialStoreState;
14+
exports.buildInitialStoreState = _initialState.buildInitialStoreState;

lib/initialState.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.registerInitialStoreState = exports.buildInitialStoreState = undefined;
7+
8+
var _redux = require('redux');
9+
10+
var state = null;
11+
12+
function registerInitialStoreState(newState) {
13+
state = newState;
14+
}
15+
16+
function buildInitialStoreState(reducer) {
17+
var store = (0, _redux.createStore)(reducer);
18+
return store.getState();
19+
}
20+
21+
function getInitialStoreState() {
22+
return state;
23+
}
24+
25+
exports.buildInitialStoreState = buildInitialStoreState;
26+
exports.registerInitialStoreState = registerInitialStoreState;
27+
exports.default = getInitialStoreState;

lib/jasmine/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

lib/mockStore.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.registerMiddlewares = undefined;
7+
8+
var _reduxMockStore = require('redux-mock-store');
9+
10+
var _reduxMockStore2 = _interopRequireDefault(_reduxMockStore);
11+
12+
var _utils = require('./utils');
13+
14+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15+
16+
var middlewares = [];
17+
18+
function registerMiddlewares(newMiddlewares) {
19+
middlewares = (0, _utils.toArray)(newMiddlewares);
20+
}
21+
22+
function getMockStore() {
23+
return (0, _reduxMockStore2.default)(middlewares);
24+
}
25+
26+
exports.registerMiddlewares = registerMiddlewares;
27+
exports.default = getMockStore;

0 commit comments

Comments
 (0)