Skip to content

Commit 5ecc07d

Browse files
committed
Add unexpected actions support
1 parent 1cd404c commit 5ecc07d

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/asserts/actionUtils.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ function notDispatchedError(dispatchedActions, expectedActions, expectedAction)
4141
);
4242
}
4343

44+
function dispatchedError(dispatchedActions, unExpectedActions, unExpectedAction) {
45+
return new Error(
46+
`Expected action ${JSON.stringify(unExpectedAction)} was not dispatched.\n` +
47+
`Expected dispatched actions: ${JSON.stringify(unExpectedActions)}\n` +
48+
`Actual dispatched actions: ${JSON.stringify(dispatchedActions)}`
49+
);
50+
}
51+
4452
function assertDispatchedActions(dispatched, expected) {
4553
const availableActions = dispatched.slice();
4654

@@ -55,8 +63,24 @@ function assertDispatchedActions(dispatched, expected) {
5563
}
5664
}
5765

66+
function assertUnDispatchedActions(dispatched, unExpected) {
67+
const availableActions = dispatched.slice();
68+
69+
for (let indexInExpected = 0; indexInExpected < unExpected.length; indexInExpected++) {
70+
const indexInAvailable = findIndex(availableActions, unExpected[indexInExpected]);
71+
72+
if (indexInAvailable === -1) {
73+
availableActions.splice(indexInAvailable, 1);
74+
} else {
75+
throw dispatchedError(dispatched, unExpected, unExpected[indexInExpected]);
76+
}
77+
}
78+
}
79+
80+
5881
export {
5982
getDispatchedActions,
6083
unrollActions,
61-
assertDispatchedActions
84+
assertDispatchedActions,
85+
assertUnDispatchedActions
6286
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import getInitialStoreState from '../initialState';
3+
import { toDispatchActionsWithState } from './toDispatchActionsWithState';
4+
5+
function toDispatchActions(actionUnderTest, unExpectedActions, done, fail) {
6+
return toDispatchActionsWithState(
7+
getInitialStoreState(),
8+
actionUnderTest,
9+
unExpectedActions,
10+
done, fail
11+
);
12+
}
13+
14+
export { toDispatchActions };
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { isFunction, isObject } from '../utils';
2+
import {
3+
getDispatchedActions,
4+
unrollActions,
5+
assertDispatchedActions,
6+
assertUnDispatchedActions
7+
} from './actionUtils';
8+
9+
function toDispatchActionsWithState(initialState, actionUnderTest, unExpectedActions, done, fail) {
10+
if (!isFunction(actionUnderTest) && !isObject(actionUnderTest)) {
11+
throw new Error(
12+
'The "actualAction" argument must be a function or an object'
13+
);
14+
}
15+
16+
if (!isFunction(unExpectedActions) &&
17+
!isObject(unExpectedActions) &&
18+
!Array.isArray(unExpectedActions)) {
19+
throw new Error(
20+
'The "unExpectedActions" argument must be ' +
21+
'an action creator function, an action object, or an array of them'
22+
);
23+
}
24+
25+
return getDispatchedActions(initialState, actionUnderTest).then((dispatchedActions) => {
26+
return unrollActions(initialState, unExpectedActions).then((unExpectedUnrolledActions) => {
27+
assertUnDispatchedActions(dispatchedActions, unExpectedUnrolledActions);
28+
29+
if (isFunction(done)) {
30+
done();
31+
}
32+
});
33+
}).catch((err) => {
34+
if (isFunction(fail)) {
35+
fail(err);
36+
return;
37+
} else if (isFunction(done)) {
38+
done(err);
39+
return;
40+
}
41+
throw new Error(JSON.stringify(err));
42+
});
43+
}
44+
45+
export {
46+
toDispatchActionsWithState
47+
};

0 commit comments

Comments
 (0)