Skip to content

Commit b971f09

Browse files
committed
Extract Javascript part of redux-actions-assertions
0 parents  commit b971f09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1431
-0
lines changed

.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"es2015"
4+
]
5+
}

.eslintrc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"extends": "airbnb-base",
3+
"parserOptions": {
4+
"ecmaVersion": 6,
5+
"sourceType": "module",
6+
"ecmaFeatures": {
7+
},
8+
},
9+
"env": {
10+
"es6": true,
11+
"mocha": true
12+
},
13+
"rules": {
14+
"arrow-body-style": ["error", "always"],
15+
"prefer-const": 1,
16+
"no-param-reassign": 0,
17+
"new-cap": 0,
18+
"comma-dangle": [2, "never"],
19+
"spaced-comment": [2, "always", { exceptions: ["*", "-"] }],
20+
"quote-props": [2, "consistent"],
21+
"no-underscore-dangle": ["error", { "allowAfterThis": true }]
22+
}
23+
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
logs
2+
*.log
3+
node_modules
4+
lib

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- stable
5+
script:
6+
- npm run test
7+
- npm run prepublish
8+
branches:
9+
only:
10+
- master

lib/assertions.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _toDispatchActions = require('./asserts/toDispatchActions');
8+
9+
var _toDispatchActionsWithState = require('./asserts/toDispatchActionsWithState');
10+
11+
var _toNotDispatchActions = require('./asserts/toNotDispatchActions');
12+
13+
var _toNotDispatchActionsWithState = require('./asserts/toNotDispatchActionsWithState');
14+
15+
exports.default = {
16+
toDispatchActions: _toDispatchActions.toDispatchActions,
17+
toDispatchActionsWithState: _toDispatchActionsWithState.toDispatchActionsWithState,
18+
toNotDispatchActions: _toNotDispatchActions.toNotDispatchActions,
19+
toNotDispatchActionsWithState: _toNotDispatchActionsWithState.toNotDispatchActionsWithState
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
function dispatchedActionError(dispatchedActions, expectedActions, action) {
7+
return new Error("Action " + JSON.stringify(action) + " was dispatched when it was unexpected.\n" + ("Actions expected to be not dispatched: " + JSON.stringify(expectedActions) + "\n") + ("Actual dispatched actions: " + JSON.stringify(dispatchedActions)));
8+
}
9+
10+
exports.dispatchedActionError = dispatchedActionError;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
function notDispatchedActionError(dispatchedActions, expectedActions, action) {
7+
return new Error("Action " + JSON.stringify(action) + " was not dispatched when it was expected.\n" + ("Actions expected to be dispatched: " + JSON.stringify(expectedActions) + "\n") + ("Actual dispatched actions: " + JSON.stringify(dispatchedActions)));
8+
}
9+
10+
exports.notDispatchedActionError = notDispatchedActionError;

lib/asserts/toDispatchActions.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.toDispatchActions = undefined;
7+
8+
var _initialState = require('../initialState');
9+
10+
var _toDispatchActionsWithState = require('./toDispatchActionsWithState');
11+
12+
function toDispatchActions(action, expectedActions, done, fail) {
13+
return (0, _toDispatchActionsWithState.toDispatchActionsWithState)((0, _initialState.getInitialStoreState)(), action, expectedActions, done, fail);
14+
}
15+
16+
exports.toDispatchActions = toDispatchActions;
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.toDispatchActionsWithState = undefined;
7+
8+
var _performAssertion = require('./utils/performAssertion');
9+
10+
var _assertDispatchedActions = require('./utils/assertDispatchedActions');
11+
12+
function toDispatchActionsWithState(initialState, action, expectedActions, done, fail) {
13+
(0, _performAssertion.performAssertion)(_assertDispatchedActions.assertDispatchedActions, initialState, action, expectedActions, done, fail);
14+
}
15+
16+
exports.toDispatchActionsWithState = toDispatchActionsWithState;

lib/asserts/toNotDispatchActions.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.toNotDispatchActions = undefined;
7+
8+
var _initialState = require('../initialState');
9+
10+
var _toNotDispatchActionsWithState = require('./toNotDispatchActionsWithState');
11+
12+
function toNotDispatchActions(action, expectedActions, done, fail) {
13+
return (0, _toNotDispatchActionsWithState.toNotDispatchActionsWithState)((0, _initialState.getInitialStoreState)(), action, expectedActions, done, fail);
14+
}
15+
16+
exports.toNotDispatchActions = toNotDispatchActions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.toNotDispatchActionsWithState = undefined;
7+
8+
var _performAssertion = require('./utils/performAssertion');
9+
10+
var _assertNotDispatchedActions = require('./utils/assertNotDispatchedActions');
11+
12+
function toNotDispatchActionsWithState(initialState, action, expectedActions, done, fail) {
13+
(0, _performAssertion.performAssertion)(_assertNotDispatchedActions.assertNotDispatchedActions, initialState, action, expectedActions, done, fail);
14+
}
15+
16+
exports.toNotDispatchActionsWithState = toNotDispatchActionsWithState;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.assertDispatchedActions = undefined;
7+
8+
var _lodash = require('lodash.findindex');
9+
10+
var _lodash2 = _interopRequireDefault(_lodash);
11+
12+
var _notDispatchedActionError = require('../errors/notDispatchedActionError');
13+
14+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15+
16+
function assertDispatchedActions(dispatched, expected) {
17+
var availableActions = dispatched.slice();
18+
19+
for (var indexInExpected = 0; indexInExpected < expected.length; indexInExpected++) {
20+
var indexInAvailable = (0, _lodash2.default)(availableActions, expected[indexInExpected]);
21+
22+
if (indexInAvailable !== -1) {
23+
availableActions.splice(indexInAvailable, 1);
24+
} else {
25+
throw (0, _notDispatchedActionError.notDispatchedActionError)(dispatched, expected, expected[indexInExpected]);
26+
}
27+
}
28+
}
29+
30+
exports.assertDispatchedActions = assertDispatchedActions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.assertNotDispatchedActions = undefined;
7+
8+
var _lodash = require('lodash.findindex');
9+
10+
var _lodash2 = _interopRequireDefault(_lodash);
11+
12+
var _dispatchedActionError = require('../errors/dispatchedActionError');
13+
14+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15+
16+
function assertNotDispatchedActions(dispatched, expected) {
17+
for (var indexInExpected = 0; indexInExpected < expected.length; indexInExpected++) {
18+
if ((0, _lodash2.default)(dispatched, expected[indexInExpected]) !== -1) {
19+
throw (0, _dispatchedActionError.dispatchedActionError)(dispatched, expected, expected[indexInExpected]);
20+
}
21+
}
22+
}
23+
24+
exports.assertNotDispatchedActions = assertNotDispatchedActions;
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.getDispatchedActions = undefined;
7+
8+
var _mockStore = require('../../mockStore');
9+
10+
var _mockStore2 = _interopRequireDefault(_mockStore);
11+
12+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13+
14+
function getDispatchedActions(initialState, action) {
15+
return new Promise(function (resolve, reject) {
16+
var store = (0, _mockStore2.default)()(initialState);
17+
var dispatchResult = store.dispatch(action);
18+
19+
if (dispatchResult instanceof Promise) {
20+
dispatchResult.then(function () {
21+
resolve(store.getActions());
22+
}).catch(function (result) {
23+
reject(result);
24+
});
25+
} else {
26+
resolve(store.getActions());
27+
}
28+
});
29+
}
30+
31+
exports.getDispatchedActions = getDispatchedActions;

lib/asserts/utils/performAssertion.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.performAssertion = undefined;
7+
8+
var _utils = require('../../utils');
9+
10+
var _getDispatchedActions = require('./getDispatchedActions');
11+
12+
var _unrollActions = require('./unrollActions');
13+
14+
function performAssertion(assertFunction, initialState, action, expectedActions, done, fail) {
15+
if (!(0, _utils.isFunction)(action) && !(0, _utils.isObject)(action)) {
16+
throw new Error('The "action" argument must be a function or an object');
17+
}
18+
19+
if (!(0, _utils.isFunction)(expectedActions) && !(0, _utils.isObject)(expectedActions) && !Array.isArray(expectedActions)) {
20+
throw new Error('The "expectedActions" argument must be ' + 'an action creator function, an action object, or an array of them');
21+
}
22+
23+
return (0, _getDispatchedActions.getDispatchedActions)(initialState, action).then(function (dispatchedActions) {
24+
return (0, _unrollActions.unrollActions)(initialState, expectedActions).then(function (expectedUnrolledActions) {
25+
assertFunction(dispatchedActions, expectedUnrolledActions);
26+
27+
if ((0, _utils.isFunction)(done)) {
28+
done();
29+
}
30+
});
31+
}).catch(function (err) {
32+
if ((0, _utils.isFunction)(fail)) {
33+
fail(err);
34+
return;
35+
} else if ((0, _utils.isFunction)(done)) {
36+
done(err);
37+
return;
38+
}
39+
throw new Error(JSON.stringify(err));
40+
});
41+
}
42+
43+
exports.performAssertion = performAssertion;

lib/asserts/utils/unrollActions.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.unrollActions = undefined;
7+
8+
var _lodash = require('lodash.flattendeep');
9+
10+
var _lodash2 = _interopRequireDefault(_lodash);
11+
12+
var _utils = require('../../utils');
13+
14+
var _getDispatchedActions = require('./getDispatchedActions');
15+
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17+
18+
function unrollActions(initialState, expectedActions) {
19+
var promises = [];
20+
var actions = (0, _utils.toArray)(expectedActions);
21+
22+
for (var index = 0; index < actions.length; index++) {
23+
promises.push((0, _getDispatchedActions.getDispatchedActions)(initialState, actions[index]));
24+
}
25+
26+
return Promise.all(promises).then(function (resultActions) {
27+
return (0, _lodash2.default)(resultActions);
28+
});
29+
}
30+
31+
exports.unrollActions = unrollActions;

lib/index.js

+21
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+
exports.assertions = exports.buildInitialStoreState = exports.registerInitialStoreState = exports.registerMiddlewares = undefined;
7+
8+
var _mockStore = require('./mockStore');
9+
10+
var _initialState = require('./initialState');
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+
exports.registerMiddlewares = _mockStore.registerMiddlewares;
19+
exports.registerInitialStoreState = _initialState.registerInitialStoreState;
20+
exports.buildInitialStoreState = _initialState.buildInitialStoreState;
21+
exports.assertions = _assertions2.default;

lib/initialState.js

+27
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.getInitialStoreState = 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.getInitialStoreState = getInitialStoreState;

0 commit comments

Comments
 (0)