diff --git a/package.json b/package.json index 277d00c..49c3683 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "rimraf": "^2.5.2" }, "dependencies": { + "lodash.clonedeep": "^4.5.0", "lodash.findindex": "^4.4.0", "lodash.flattendeep": "^4.2.0", "redux": "^3.5.2", diff --git a/src/asserts/toDispatchActionsWithState.js b/src/asserts/toDispatchActionsWithState.js index 21e9345..4e75171 100644 --- a/src/asserts/toDispatchActionsWithState.js +++ b/src/asserts/toDispatchActionsWithState.js @@ -1,7 +1,10 @@ +import { getInitialStoreState } from '../initialState'; +import { isFunction } from '../utils'; import { performAssertion } from './utils/performAssertion'; import { assertDispatchedActions } from './utils/assertDispatchedActions'; -function toDispatchActionsWithState(initialState, action, expectedActions, done, fail) { +function toDispatchActionsWithState(state, action, expectedActions, done, fail) { + const initialState = isFunction(state) ? state(getInitialStoreState()) : state; return performAssertion( assertDispatchedActions, initialState, diff --git a/src/asserts/toNotDispatchActionsWithState.js b/src/asserts/toNotDispatchActionsWithState.js index b7b7ce7..d67be16 100644 --- a/src/asserts/toNotDispatchActionsWithState.js +++ b/src/asserts/toNotDispatchActionsWithState.js @@ -1,7 +1,10 @@ +import { getInitialStoreState } from '../initialState'; +import { isFunction } from '../utils'; import { performAssertion } from './utils/performAssertion'; import { assertNotDispatchedActions } from './utils/assertNotDispatchedActions'; -function toNotDispatchActionsWithState(initialState, action, expectedActions, done, fail) { +function toNotDispatchActionsWithState(state, action, expectedActions, done, fail) { + const initialState = isFunction(state) ? state(getInitialStoreState()) : state; return performAssertion( assertNotDispatchedActions, initialState, diff --git a/src/initialState.js b/src/initialState.js index d136e28..5198c12 100644 --- a/src/initialState.js +++ b/src/initialState.js @@ -1,3 +1,4 @@ +import cloneDeep from 'lodash.clonedeep'; import { createStore } from 'redux'; let state = null; @@ -12,7 +13,7 @@ function buildInitialStoreState(reducer) { } function getInitialStoreState() { - return state; + return cloneDeep(state); } export { diff --git a/test/asserts/toDispatchActionsWithState.js b/test/asserts/toDispatchActionsWithState.js index cdc2fa8..90bfef7 100644 --- a/test/asserts/toDispatchActionsWithState.js +++ b/test/asserts/toDispatchActionsWithState.js @@ -2,10 +2,10 @@ import expect from 'expect'; import * as performAssertionObj from '../../src/asserts/utils/performAssertion'; import * as assertDispatchedActionsObj from '../../src/asserts/utils/assertDispatchedActions'; import { toDispatchActionsWithState } from '../../src/asserts/toDispatchActionsWithState'; -import { getInitialStoreState } from '../../src/initialState'; +import { registerInitialStoreState, getInitialStoreState } from '../../src/initialState'; describe('toDispatchActionsWithState', () => { - const initialState = getInitialStoreState(); + let initialState; const actualAction = { actualAction: 'actualAction' }; const expectedAction = { expectedAction: 'expectedAction' }; const spyDone = expect.createSpy(); @@ -13,12 +13,15 @@ describe('toDispatchActionsWithState', () => { const performAssertionResult = { result: 'result' }; beforeEach(() => { + registerInitialStoreState({ result: 'result' }); + initialState = getInitialStoreState(); expect.spyOn(performAssertionObj, 'performAssertion') .andReturn(performAssertionResult); expect.spyOn(assertDispatchedActionsObj, 'assertDispatchedActions'); }); afterEach(() => { + registerInitialStoreState(null); expect.restoreSpies(); }); @@ -47,4 +50,42 @@ describe('toDispatchActionsWithState', () => { expect(result).toBe(performAssertionResult); }); + + describe('when state is a function', () => { + const stateFunctionResult = { newResult: 'newResult' }; + let stateFunction; + + beforeEach(() => { + stateFunction = expect.createSpy().andReturn(stateFunctionResult); + }); + + it('should execute it with initial state as argument', () => { + toDispatchActionsWithState( + stateFunction, + actualAction, + expectedAction, + spyDone, spyFail + ); + + expect(stateFunction).toHaveBeenCalledWith(initialState); + }); + + it('should call performAssertion with result from state function as initial state', () => { + toDispatchActionsWithState( + stateFunction, + actualAction, + expectedAction, + spyDone, spyFail + ); + + expect(performAssertionObj.performAssertion).toHaveBeenCalledWith( + assertDispatchedActionsObj.assertDispatchedActions, + stateFunctionResult, + actualAction, + expectedAction, + spyDone, + spyFail + ); + }); + }); }); diff --git a/test/asserts/toNotDispatchActionsWithState.js b/test/asserts/toNotDispatchActionsWithState.js index 769b295..fa2ecec 100644 --- a/test/asserts/toNotDispatchActionsWithState.js +++ b/test/asserts/toNotDispatchActionsWithState.js @@ -47,4 +47,42 @@ describe('toNotDispatchActionsWithState', () => { expect(result).toBe(performAssertionResult); }); + + describe('when state is a function', () => { + const stateFunctionResult = { newResult: 'newResult' }; + let stateFunction; + + beforeEach(() => { + stateFunction = expect.createSpy().andReturn(stateFunctionResult); + }); + + it('should execute it with initial state as argument', () => { + toNotDispatchActionsWithState( + stateFunction, + actualAction, + expectedAction, + spyDone, spyFail + ); + + expect(stateFunction).toHaveBeenCalledWith(initialState); + }); + + it('should call performAssertion with result from state function as initial state', () => { + toNotDispatchActionsWithState( + stateFunction, + actualAction, + expectedAction, + spyDone, spyFail + ); + + expect(performAssertionObj.performAssertion).toHaveBeenCalledWith( + assertNotDispatchedActionsObj.assertNotDispatchedActions, + stateFunctionResult, + actualAction, + expectedAction, + spyDone, + spyFail + ); + }); + }); }); diff --git a/test/general/initialState.js b/test/general/initialState.js index 94332e5..035b439 100644 --- a/test/general/initialState.js +++ b/test/general/initialState.js @@ -28,6 +28,14 @@ describe('initialState', () => { it('should return registered value', () => { expect(getInitialStoreState()).toEqual(initialStoreState); }); + + it('should return deep clone of registered value', () => { + const initialState = getInitialStoreState(); + initialState.newInitialStoreStateKey = 'newInitialStoreStateKey'; + + expect(getInitialStoreState().newInitialStoreStateKey) + .toNotEqual(initialState.newInitialStoreStateKey); + }); }); });