Skip to content

Commit

Permalink
Merge pull request #3 from dmitry-zaets/accept-assertion-state-as-fun…
Browse files Browse the repository at this point in the history
…ction

Accept function as initial state for assertion
  • Loading branch information
dmitry-zaets committed Aug 25, 2016
2 parents 409cc4c + 0ad4fc7 commit 3a83e53
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion 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,
Expand Down
5 changes: 4 additions & 1 deletion 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,
Expand Down
3 changes: 2 additions & 1 deletion src/initialState.js
@@ -1,3 +1,4 @@
import cloneDeep from 'lodash.clonedeep';
import { createStore } from 'redux';

let state = null;
Expand All @@ -12,7 +13,7 @@ function buildInitialStoreState(reducer) {
}

function getInitialStoreState() {
return state;
return cloneDeep(state);
}

export {
Expand Down
45 changes: 43 additions & 2 deletions test/asserts/toDispatchActionsWithState.js
Expand Up @@ -2,23 +2,26 @@ 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();
const spyFail = expect.createSpy();
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();
});

Expand Down Expand Up @@ -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
);
});
});
});
38 changes: 38 additions & 0 deletions test/asserts/toNotDispatchActionsWithState.js
Expand Up @@ -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
);
});
});
});
8 changes: 8 additions & 0 deletions test/general/initialState.js
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit 3a83e53

Please sign in to comment.