diff --git a/src/components/connector.js b/src/components/connector.js index 5ba17b7..a272dfe 100644 --- a/src/components/connector.js +++ b/src/components/connector.js @@ -52,8 +52,8 @@ export default function Connector(store) { const unsubscribe = store.subscribe(() => { const nextSlice = getStateSlice(store.getState(), finalMapStateToTarget); if (!shallowEqual(slice, nextSlice)) { + updateTarget(target, nextSlice, boundActionCreators, slice); slice = nextSlice; - updateTarget(target, slice, boundActionCreators); } }); return unsubscribe; @@ -62,9 +62,9 @@ export default function Connector(store) { } } -function updateTarget(target, StateSlice, dispatch) { +function updateTarget(target, StateSlice, dispatch, prevStateSlice) { if(isFunction(target)) { - target(StateSlice, dispatch); + target(StateSlice, dispatch, prevStateSlice); } else { assign(target, StateSlice, dispatch); } diff --git a/test/components/connector.spec.js b/test/components/connector.spec.js index 919e66b..ee2db43 100644 --- a/test/components/connector.spec.js +++ b/test/components/connector.spec.js @@ -18,7 +18,12 @@ describe('Connector', () => { baz: -1 }; store = createStore((state = defaultState, action) => { - return assign({}, state, { baz: action.payload }); + switch (action.type) { + case 'ACTION': + return assign({}, state, { baz: action.payload }); + default: + return state; + } }); targetObj = {}; connect = Connector(store); @@ -109,4 +114,20 @@ describe('Connector', () => { expect(receivedDispatch).toBe(store.dispatch); }); + it('Should provide state slice, bound actions and previous state slice to target (function)', () => { + const targetFunc = sinon.spy(); + + connect(state => state, {})(targetFunc); + + expect(targetFunc.calledWith(defaultState, {}, undefined)).toBeTruthy(); + + store.dispatch({ type: 'ACTION', payload: 2 }); + + expect(targetFunc.calledWith( + assign({}, defaultState, { baz: 2 }), + {}, + defaultState) + ).toBeTruthy(); + }); + });