Skip to content

Commit

Permalink
fix(Configure): trigger onSearchStateChange with the right data
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuret authored and vvo committed Feb 8, 2017
1 parent 5111667 commit 11e5af8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
8 changes: 3 additions & 5 deletions packages/react-instantsearch/src/core/InstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,19 @@ class InstantSearch extends Component {
}

onWidgetsInternalStateUpdate(searchState) {
searchState = this.onSearchStateChange(searchState);
searchState = this.aisManager.transitionState(searchState);

this.onSearchStateChange(searchState);

if (!this.isControlled) {
this.aisManager.onExternalStateUpdate(searchState);
}
}

onSearchStateChange(searchState) {
searchState = this.aisManager.transitionState(searchState);

if (this.props.onSearchStateChange) {
this.props.onSearchStateChange(searchState);
}

return searchState;
}

onSearchForFacetValues(searchState) {
Expand Down
8 changes: 7 additions & 1 deletion packages/react-instantsearch/src/core/createConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ export default function createConnector(connectorDesc) {
// Since props might have changed, we need to re-run getSearchParameters
// and getMetadata with the new props.
this.context.ais.widgetsManager.update();
this.context.ais.onSearchStateChange(this.context.ais.store.getState().widgets);
if (connectorDesc.transitionState) {
this.context.ais.onSearchStateChange(connectorDesc.transitionState(
nextProps,
this.context.ais.store.getState().widgets,
this.context.ais.store.getState().widgets,
));
}
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions packages/react-instantsearch/src/core/createConnector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ describe('createConnector', () => {
const getSearchParameters = jest.fn(() => {
});
const onSearchStateChange = jest.fn();
const transitionState = jest.fn();
const update = jest.fn();
const Dummy = jest.fn(() => null);
const Connected = createConnector({
displayName: 'CoolConnector',
getProvidedProps,
getSearchParameters,
transitionState,
getId,
})(Dummy);
const wrapper = mount(<Connected />, {
Expand All @@ -240,11 +242,14 @@ describe('createConnector', () => {
});
expect(onSearchStateChange.mock.calls.length).toBe(0);
expect(update.mock.calls.length).toBe(0);
expect(transitionState.mock.calls.length).toBe(0);
wrapper.setProps({hello: 'there', another: ['one', 'two']});
expect(onSearchStateChange.mock.calls.length).toBe(1);
expect(transitionState.mock.calls.length).toBe(1);
expect(update.mock.calls.length).toBe(1);
wrapper.setProps({hello: 'there', another: ['one', 'two']});
expect(onSearchStateChange.mock.calls.length).toBe(1);
expect(transitionState.mock.calls.length).toBe(1);
expect(update.mock.calls.length).toBe(1);
});
});
Expand Down Expand Up @@ -338,11 +343,13 @@ describe('createConnector', () => {
});

it('calls update when props change', () => {
const transitionState = jest.fn();
const Connected = createConnector({
displayName: 'CoolConnector',
getProvidedProps: () => null,
getMetadata: () => null,
getId,
transitionState,
})(() => null);
const update = jest.fn();
const onSearchStateChange = jest.fn();
Expand All @@ -362,17 +369,53 @@ describe('createConnector', () => {
}});
expect(update.mock.calls.length).toBe(0);
expect(onSearchStateChange.mock.calls.length).toBe(0);
expect(transitionState.mock.calls.length).toBe(0);
wrapper.setProps({hello: 'you'});
expect(update.mock.calls.length).toBe(1);
expect(onSearchStateChange.mock.calls.length).toBe(1);
expect(transitionState.mock.calls.length).toBe(1);
});

it('dont trigger onSearchStateChange when props change and the component has no transitionState function', () => {
const Connected = createConnector({
displayName: 'CoolConnector',
getProvidedProps: () => null,
getMetadata: () => null,
getId,
})(() => null);
const update = jest.fn();
const onSearchStateChange = jest.fn();
const props = {hello: 'there'};
const wrapper = mount(<Connected {...props} />, {
context: {
ais: {
store: {
getState: () => ({}),
subscribe: () => null,
},
widgetsManager: {
registerWidget: () => null,
update,
},
onSearchStateChange,
},
},
});
expect(update.mock.calls.length).toBe(0);
expect(onSearchStateChange.mock.calls.length).toBe(0);
wrapper.setProps({hello: 'you'});
expect(update.mock.calls.length).toBe(1);
expect(onSearchStateChange.mock.calls.length).toBe(0);
});

it('dont update when props dont change', () => {
const transitionState = jest.fn();
const Connected = createConnector({
displayName: 'CoolConnector',
getProvidedProps: () => null,
getMetadata: () => null,
getId,
transitionState,
})(() => null);
const onSearchStateChange = jest.fn();
const update = jest.fn();
Expand All @@ -392,9 +435,11 @@ describe('createConnector', () => {
}});
expect(onSearchStateChange.mock.calls.length).toBe(0);
expect(update.mock.calls.length).toBe(0);
expect(transitionState.mock.calls.length).toBe(0);
wrapper.setProps({hello: 'there'});
expect(onSearchStateChange.mock.calls.length).toBe(0);
expect(update.mock.calls.length).toBe(0);
expect(transitionState.mock.calls.length).toBe(0);
});

it('unregisters itself on unmount', () => {
Expand Down

0 comments on commit 11e5af8

Please sign in to comment.