diff --git a/packages/react-instantsearch/src/core/createConnector.js b/packages/react-instantsearch/src/core/createConnector.js index 12342c5112..43831304e6 100644 --- a/packages/react-instantsearch/src/core/createConnector.js +++ b/packages/react-instantsearch/src/core/createConnector.js @@ -64,7 +64,7 @@ export default function createConnector(connectorDesc) { this.unsubscribe = store.subscribe(() => { this.setState({ - props: this.getProvidedProps(props), + props: this.getProvidedProps(this.props), }); }); diff --git a/packages/react-instantsearch/src/core/createConnector.test.js b/packages/react-instantsearch/src/core/createConnector.test.js index 6931ee4cc3..1c3210e36b 100644 --- a/packages/react-instantsearch/src/core/createConnector.test.js +++ b/packages/react-instantsearch/src/core/createConnector.test.js @@ -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(, { + 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',