Skip to content

Commit 350ab93

Browse files
committed
Add possibility to assert duplicate actions
1 parent 59d24fd commit 350ab93

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"should": "^8.3.1"
4242
},
4343
"dependencies": {
44-
"lodash.find": "^4.3.0",
44+
"lodash.findindex": "^4.4.0",
4545
"lodash.flattendeep": "^4.2.0",
4646
"redux": "^3.5.2",
4747
"redux-mock-store": "^1.0.2"

src/asserts/actionUtils.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import find from 'lodash.find';
1+
import findIndex from 'lodash.findindex';
22
import flattenDeep from 'lodash.flattendeep';
33
import { toArray } from '../utils';
44
import getMockStore from '../mockStore';
@@ -33,13 +33,24 @@ function unrollActions(initialState, expectedActions) {
3333
});
3434
}
3535

36+
function notDispatchedError(dispatchedActions, expectedActions, expectedAction) {
37+
return new Error(
38+
`Expected action ${JSON.stringify(expectedAction)} was not dispatched.\n` +
39+
`Expected dispatched actions: ${JSON.stringify(expectedActions)}` +
40+
`Actual dispatched actions: ${JSON.stringify(dispatchedActions)}`
41+
);
42+
}
43+
3644
function assertDispatchedActions(dispatched, expected) {
37-
for (let index = 0; index < expected.length; index++) {
38-
if (!find(dispatched, expected[index])) {
39-
throw new Error(
40-
`Expected action ${JSON.stringify(expected[index])} was not dispatched.\n` +
41-
`Actual dispatched actions: ${JSON.stringify(dispatched)}`
42-
);
45+
const availableActions = dispatched.slice();
46+
47+
for (let indexInExpected = 0; indexInExpected < expected.length; indexInExpected++) {
48+
const indexInAvailable = findIndex(availableActions, expected[indexInExpected]);
49+
50+
if (indexInAvailable !== -1) {
51+
availableActions.splice(indexInAvailable, 1);
52+
} else {
53+
throw notDispatchedError(dispatched, expected, expected[indexInExpected]);
4354
}
4455
}
4556
}

test/assertions/actionUtils.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('assertions', () => {
3434

3535
it('should be function', () => { expect(unrollActions).toBeA('function'); });
3636

37-
it('should return flat array with all actions', () => {
37+
it('should return flat array with all the actions', () => {
3838
unrollActions({}, asyncActionCreator()).then((result) => {
3939
const expectedActions = [
4040
{ type: '0-0' },
@@ -50,20 +50,62 @@ describe('assertions', () => {
5050
});
5151

5252
describe('assertDispatchedActions', () => {
53-
it('should be function', () => { expect(assertDispatchedActions).toBeA('function'); });
53+
it('should be function', () => {
54+
expect(assertDispatchedActions).toBeA('function');
55+
});
56+
57+
describe('when expected action was not dispatched', () => {
58+
it('should throw an error', () => {
59+
const dispatchedActions = [
60+
{ type: '0-0' },
61+
{ type: '0-1' }
62+
];
63+
const expectedActions = [
64+
{ type: '0-0' },
65+
{ type: '10-0' }
66+
];
67+
68+
expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); })
69+
.toThrow();
70+
});
71+
});
5472

55-
it('should throw error if expected action was not dispatched', () => {
73+
it('should accept expected duplicate actions', () => {
5674
const dispatchedActions = [
5775
{ type: '0-0' },
58-
{ type: '0-1' }
76+
{ type: '0-1' },
77+
{ type: '0-0' },
78+
{ type: '0-2' }
5979
];
6080
const expectedActions = [
6181
{ type: '0-0' },
62-
{ type: '10-0' }
82+
{ type: '0-0' },
83+
{ type: '0-1' },
84+
{ type: '0-2' }
6385
];
6486

6587
expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); })
66-
.toThrow();
88+
.toNotThrow();
89+
});
90+
91+
describe('when expected duplicate actions were not dispatched', () => {
92+
it('should throw an error', () => {
93+
const dispatchedActions = [
94+
{ type: '0-0' },
95+
{ type: '0-1' },
96+
{ type: '0-2' },
97+
{ type: '0-3' }
98+
];
99+
const expectedActions = [
100+
{ type: '0-0' },
101+
{ type: '0-0' },
102+
{ type: '0-1' },
103+
{ type: '0-2' }
104+
];
105+
106+
expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); })
107+
.toThrow();
108+
});
67109
});
68110
});
69111
});

0 commit comments

Comments
 (0)