Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
23 changes: 22 additions & 1 deletion test/components/connector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
});

});