Skip to content

Commit 28e0e66

Browse files
committed
Refactor utils.
Split utils to separate files. Add "toNotDispatchActions" and "toNotDispatchActionsWithState".
1 parent 724354b commit 28e0e66

28 files changed

+562
-304
lines changed

src/assertions.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import { toDispatchActions } from './asserts/toDispatchActions';
22
import { toDispatchActionsWithState } from './asserts/toDispatchActionsWithState';
3+
import { toNotDispatchActions } from './asserts/toNotDispatchActions';
4+
import { toNotDispatchActionsWithState } from './asserts/toNotDispatchActionsWithState';
35

4-
export default { toDispatchActions, toDispatchActionsWithState };
6+
7+
export default {
8+
toDispatchActions,
9+
toDispatchActionsWithState,
10+
toNotDispatchActions,
11+
toNotDispatchActionsWithState
12+
};

src/asserts/actionUtils.js

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function dispatchedActionError(dispatchedActions, expectedActions, action) {
2+
return new Error(
3+
`Action ${JSON.stringify(action)} was dispatched when it was unexpected.\n` +
4+
`Actions expected to be not dispatched: ${JSON.stringify(expectedActions)}\n` +
5+
`Actual dispatched actions: ${JSON.stringify(dispatchedActions)}`
6+
);
7+
}
8+
9+
export { dispatchedActionError };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function notDispatchedActionError(dispatchedActions, expectedActions, action) {
2+
return new Error(
3+
`Action ${JSON.stringify(action)} was not dispatched when it was expected.\n` +
4+
`Actions expected to be dispatched: ${JSON.stringify(expectedActions)}\n` +
5+
`Actual dispatched actions: ${JSON.stringify(dispatchedActions)}`
6+
);
7+
}
8+
9+
export { notDispatchedActionError };

src/asserts/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/asserts/toDispatchActions.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import getInitialStoreState from '../initialState';
1+
import { getInitialStoreState } from '../initialState';
22
import { toDispatchActionsWithState } from './toDispatchActionsWithState';
33

4-
function toDispatchActions(actionUnderTest, expectedActions, done, fail) {
4+
function toDispatchActions(action, expectedActions, done, fail) {
55
return toDispatchActionsWithState(
66
getInitialStoreState(),
7-
actionUnderTest,
7+
action,
88
expectedActions,
99
done, fail
1010
);
Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,8 @@
1-
import { isFunction, isObject } from '../utils';
2-
import { getDispatchedActions, unrollActions, assertDispatchedActions } from './actionUtils';
1+
import { performAssertion } from './utils/performAssertion';
2+
import { assertDispatchedActions } from './utils/assertDispatchedActions';
33

4-
function toDispatchActionsWithState(initialState, actionUnderTest, expectedActions, done, fail) {
5-
if (!isFunction(actionUnderTest) && !isObject(actionUnderTest)) {
6-
throw new Error(
7-
'The "actualAction" argument must be a function or an object'
8-
);
9-
}
10-
11-
if (!isFunction(expectedActions) &&
12-
!isObject(expectedActions) &&
13-
!Array.isArray(expectedActions)) {
14-
throw new Error(
15-
'The "expectedActions" argument must be ' +
16-
'an action creator function, an action object, or an array of them'
17-
);
18-
}
19-
20-
return getDispatchedActions(initialState, actionUnderTest).then((dispatchedActions) => {
21-
return unrollActions(initialState, expectedActions).then((expectedUnrolledActions) => {
22-
assertDispatchedActions(dispatchedActions, expectedUnrolledActions);
23-
24-
if (isFunction(done)) {
25-
done();
26-
}
27-
});
28-
}).catch((err) => {
29-
if (isFunction(fail)) {
30-
fail(err);
31-
return;
32-
} else if (isFunction(done)) {
33-
done(err);
34-
return;
35-
}
36-
throw new Error(JSON.stringify(err));
37-
});
4+
function toDispatchActionsWithState(initialState, action, expectedActions, done, fail) {
5+
performAssertion(assertDispatchedActions, initialState, action, expectedActions, done, fail);
386
}
397

40-
export {
41-
toDispatchActionsWithState
42-
};
8+
export { toDispatchActionsWithState };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getInitialStoreState } from '../initialState';
2+
import { toNotDispatchActionsWithState } from './toNotDispatchActionsWithState';
3+
4+
function toNotDispatchActions(action, expectedActions, done, fail) {
5+
return toNotDispatchActionsWithState(
6+
getInitialStoreState(),
7+
action,
8+
expectedActions,
9+
done, fail
10+
);
11+
}
12+
13+
export { toNotDispatchActions };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { performAssertion } from './utils/performAssertion';
2+
import { assertNotDispatchedActions } from './utils/assertNotDispatchedActions';
3+
4+
function toNotDispatchActionsWithState(initialState, action, expectedActions, done, fail) {
5+
performAssertion(assertNotDispatchedActions, initialState, action, expectedActions, done, fail);
6+
}
7+
8+
export { toNotDispatchActionsWithState };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import findIndex from 'lodash.findindex';
2+
import { notDispatchedActionError } from '../errors/notDispatchedActionError';
3+
4+
function assertDispatchedActions(dispatched, expected) {
5+
const availableActions = dispatched.slice();
6+
7+
for (let indexInExpected = 0; indexInExpected < expected.length; indexInExpected++) {
8+
const indexInAvailable = findIndex(availableActions, expected[indexInExpected]);
9+
10+
if (indexInAvailable !== -1) {
11+
availableActions.splice(indexInAvailable, 1);
12+
} else {
13+
throw notDispatchedActionError(dispatched, expected, expected[indexInExpected]);
14+
}
15+
}
16+
}
17+
18+
export { assertDispatchedActions };

0 commit comments

Comments
 (0)