Skip to content

Commit

Permalink
fix(createConnector): updates with latest props on state change (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhlh authored and bobylito committed Feb 7, 2017
1 parent 687a408 commit cd3a82c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react-instantsearch/src/core/createConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function createConnector(connectorDesc) {

this.unsubscribe = store.subscribe(() => {
this.setState({
props: this.getProvidedProps(props),
props: this.getProvidedProps(this.props),
});
});

Expand Down
54 changes: 54 additions & 0 deletions packages/react-instantsearch/src/core/createConnector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,60 @@ describe('createConnector', () => {
expect(wrapper.find(Dummy).props()).toEqual({...props, ...state.widgets});
});

it('updates with latest props on state change', () => {
const getProvidedProps = jest.fn((props, state) => state);
const Dummy = () => null;
const Connected = createConnector({
displayName: 'CoolConnector',
getProvidedProps,
getId,
})(Dummy);
let state = {
...createState(),
widgets: {
hoy: 'hey',
},
};
let props = {
hello: 'there',
};
let listener;
const wrapper = mount(<Connected {...props} />, {
context: {
ais: {
store: {
getState: () => state,
subscribe: l => {
listener = l;
},
},
},
},
});
expect(wrapper.find(Dummy).props()).toEqual({...props, ...state.widgets});
state = {
...createState(),
widgets: {
hey: 'hoy',
},
};

// also update props
props = {hello: 'you'};
wrapper.setProps(props);

listener();
expect(getProvidedProps.mock.calls.length).toBe(3);
const args = getProvidedProps.mock.calls[2];
expect(args[0]).toEqual(props);
expect(args[1]).toBe(state.widgets);
expect(args[2].results).toBe(state.results);
expect(args[2].error).toBe(state.error);
expect(args[2].searching).toBe(state.searching);
expect(args[3]).toBe(state.metadata);
expect(wrapper.find(Dummy).props()).toEqual({...props, ...state.widgets});
});

it('unsubscribes from the store on unmount', () => {
const Connected = createConnector({
displayName: 'CoolConnector',
Expand Down

0 comments on commit cd3a82c

Please sign in to comment.