Skip to content

Commit 3a83e53

Browse files
authored
Merge pull request #3 from dmitry-zaets/accept-assertion-state-as-function
Accept function as initial state for assertion
2 parents 409cc4c + 0ad4fc7 commit 3a83e53

File tree

7 files changed

+100
-5
lines changed

7 files changed

+100
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"rimraf": "^2.5.2"
4040
},
4141
"dependencies": {
42+
"lodash.clonedeep": "^4.5.0",
4243
"lodash.findindex": "^4.4.0",
4344
"lodash.flattendeep": "^4.2.0",
4445
"redux": "^3.5.2",

src/asserts/toDispatchActionsWithState.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { getInitialStoreState } from '../initialState';
2+
import { isFunction } from '../utils';
13
import { performAssertion } from './utils/performAssertion';
24
import { assertDispatchedActions } from './utils/assertDispatchedActions';
35

4-
function toDispatchActionsWithState(initialState, action, expectedActions, done, fail) {
6+
function toDispatchActionsWithState(state, action, expectedActions, done, fail) {
7+
const initialState = isFunction(state) ? state(getInitialStoreState()) : state;
58
return performAssertion(
69
assertDispatchedActions,
710
initialState,

src/asserts/toNotDispatchActionsWithState.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { getInitialStoreState } from '../initialState';
2+
import { isFunction } from '../utils';
13
import { performAssertion } from './utils/performAssertion';
24
import { assertNotDispatchedActions } from './utils/assertNotDispatchedActions';
35

4-
function toNotDispatchActionsWithState(initialState, action, expectedActions, done, fail) {
6+
function toNotDispatchActionsWithState(state, action, expectedActions, done, fail) {
7+
const initialState = isFunction(state) ? state(getInitialStoreState()) : state;
58
return performAssertion(
69
assertNotDispatchedActions,
710
initialState,

src/initialState.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cloneDeep from 'lodash.clonedeep';
12
import { createStore } from 'redux';
23

34
let state = null;
@@ -12,7 +13,7 @@ function buildInitialStoreState(reducer) {
1213
}
1314

1415
function getInitialStoreState() {
15-
return state;
16+
return cloneDeep(state);
1617
}
1718

1819
export {

test/asserts/toDispatchActionsWithState.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@ import expect from 'expect';
22
import * as performAssertionObj from '../../src/asserts/utils/performAssertion';
33
import * as assertDispatchedActionsObj from '../../src/asserts/utils/assertDispatchedActions';
44
import { toDispatchActionsWithState } from '../../src/asserts/toDispatchActionsWithState';
5-
import { getInitialStoreState } from '../../src/initialState';
5+
import { registerInitialStoreState, getInitialStoreState } from '../../src/initialState';
66

77
describe('toDispatchActionsWithState', () => {
8-
const initialState = getInitialStoreState();
8+
let initialState;
99
const actualAction = { actualAction: 'actualAction' };
1010
const expectedAction = { expectedAction: 'expectedAction' };
1111
const spyDone = expect.createSpy();
1212
const spyFail = expect.createSpy();
1313
const performAssertionResult = { result: 'result' };
1414

1515
beforeEach(() => {
16+
registerInitialStoreState({ result: 'result' });
17+
initialState = getInitialStoreState();
1618
expect.spyOn(performAssertionObj, 'performAssertion')
1719
.andReturn(performAssertionResult);
1820
expect.spyOn(assertDispatchedActionsObj, 'assertDispatchedActions');
1921
});
2022

2123
afterEach(() => {
24+
registerInitialStoreState(null);
2225
expect.restoreSpies();
2326
});
2427

@@ -47,4 +50,42 @@ describe('toDispatchActionsWithState', () => {
4750

4851
expect(result).toBe(performAssertionResult);
4952
});
53+
54+
describe('when state is a function', () => {
55+
const stateFunctionResult = { newResult: 'newResult' };
56+
let stateFunction;
57+
58+
beforeEach(() => {
59+
stateFunction = expect.createSpy().andReturn(stateFunctionResult);
60+
});
61+
62+
it('should execute it with initial state as argument', () => {
63+
toDispatchActionsWithState(
64+
stateFunction,
65+
actualAction,
66+
expectedAction,
67+
spyDone, spyFail
68+
);
69+
70+
expect(stateFunction).toHaveBeenCalledWith(initialState);
71+
});
72+
73+
it('should call performAssertion with result from state function as initial state', () => {
74+
toDispatchActionsWithState(
75+
stateFunction,
76+
actualAction,
77+
expectedAction,
78+
spyDone, spyFail
79+
);
80+
81+
expect(performAssertionObj.performAssertion).toHaveBeenCalledWith(
82+
assertDispatchedActionsObj.assertDispatchedActions,
83+
stateFunctionResult,
84+
actualAction,
85+
expectedAction,
86+
spyDone,
87+
spyFail
88+
);
89+
});
90+
});
5091
});

test/asserts/toNotDispatchActionsWithState.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,42 @@ describe('toNotDispatchActionsWithState', () => {
4747

4848
expect(result).toBe(performAssertionResult);
4949
});
50+
51+
describe('when state is a function', () => {
52+
const stateFunctionResult = { newResult: 'newResult' };
53+
let stateFunction;
54+
55+
beforeEach(() => {
56+
stateFunction = expect.createSpy().andReturn(stateFunctionResult);
57+
});
58+
59+
it('should execute it with initial state as argument', () => {
60+
toNotDispatchActionsWithState(
61+
stateFunction,
62+
actualAction,
63+
expectedAction,
64+
spyDone, spyFail
65+
);
66+
67+
expect(stateFunction).toHaveBeenCalledWith(initialState);
68+
});
69+
70+
it('should call performAssertion with result from state function as initial state', () => {
71+
toNotDispatchActionsWithState(
72+
stateFunction,
73+
actualAction,
74+
expectedAction,
75+
spyDone, spyFail
76+
);
77+
78+
expect(performAssertionObj.performAssertion).toHaveBeenCalledWith(
79+
assertNotDispatchedActionsObj.assertNotDispatchedActions,
80+
stateFunctionResult,
81+
actualAction,
82+
expectedAction,
83+
spyDone,
84+
spyFail
85+
);
86+
});
87+
});
5088
});

test/general/initialState.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ describe('initialState', () => {
2828
it('should return registered value', () => {
2929
expect(getInitialStoreState()).toEqual(initialStoreState);
3030
});
31+
32+
it('should return deep clone of registered value', () => {
33+
const initialState = getInitialStoreState();
34+
initialState.newInitialStoreStateKey = 'newInitialStoreStateKey';
35+
36+
expect(getInitialStoreState().newInitialStoreStateKey)
37+
.toNotEqual(initialState.newInitialStoreStateKey);
38+
});
3139
});
3240
});
3341

0 commit comments

Comments
 (0)