Skip to content

Commit 91c8958

Browse files
committed
Accept function as initial state for assertion
1 parent c4590fa commit 91c8958

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

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,

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 of 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
});

0 commit comments

Comments
 (0)