Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix: bind getSearchParmaters to the component instance
Browse files Browse the repository at this point in the history
* fix: bind getSearchParmaters to the component instance

When reducing over the searchParameters in findResultsState, searchParameters.getSearchParameters was being called with the wrong "this".

This led to a bug in connectRange where this._currentRange was undefined when getSearchParameters was being called.

If a "min" or "max" prop is provided to connectRange, an if block is executed inside getCurrentRefinement that tries to access properties on the undefined this._currentRange, causing an error
  • Loading branch information
BrentLayne authored and samouss committed Sep 18, 2018
1 parent 4a4ff70 commit f34cb3d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/react-instantsearch-core/src/components/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Index extends Component {

componentWillMount() {
this.context.ais.onSearchParameters(
this.getSearchParameters,
this.getSearchParameters.bind(this),
this.getChildContext(),
this.props
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ describe('createConnector', () => {
});

expect(onSearchParameters.mock.calls).toHaveLength(1);
expect(onSearchParameters.mock.calls[0][0]).toBe(getSearchParameters);
expect(onSearchParameters.mock.calls[0][0]).toEqual(expect.any(Function));
expect(onSearchParameters.mock.calls[0][1]).toEqual(context);
expect(onSearchParameters.mock.calls[0][2]).toEqual(props);

Expand All @@ -434,6 +434,46 @@ describe('createConnector', () => {
expect(onSearchParameters.mock.calls).toHaveLength(1);
});

it('binds getSearchParameters to its own instance when calling onSearchParameters in componentWillMount', () => {
const getSearchParameters = jest.fn(() => null);
const onSearchParameters = jest.fn(boundGetSearchParameters =>
// The bound getSearchParameters function must be invoked in order for it
// to be registered as an instance of getSearchParameters
boundGetSearchParameters()
);

const Connected = createConnector({
displayName: 'CoolConnector',
getProvidedProps: () => null,
getSearchParameters,
getId,
})(() => null);
const state = {
widgets: {},
};
const registerWidget = jest.fn();
const props = { hello: 'there' };
const context = {
ais: {
store: {
getState: () => state,
subscribe: () => null,
},
onSearchParameters,
widgetsManager: {
registerWidget,
},
},
};
mount(<Connected {...props} />, {
context,
});

expect(getSearchParameters.mock.instances).toHaveLength(1);
expect(getSearchParameters.mock.instances[0].props).toEqual(props);
expect(getSearchParameters.mock.instances[0].context).toEqual(context);
});

it('calls update when props change', () => {
const transitionState = jest.fn();
const Connected = createConnector({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function createConnector(connectorDesc) {
componentWillMount() {
if (connectorDesc.getSearchParameters) {
this.context.ais.onSearchParameters(
connectorDesc.getSearchParameters,
connectorDesc.getSearchParameters.bind(this),
this.context,
this.props
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ const createInstantSearchServer = algoliasearch => {
)
.reduce(
(acc, searchParameter) =>
searchParameter.getSearchParameters.call(
{ context: searchParameter.context },
searchParameter.getSearchParameters(
acc,
searchParameter.props,
searchParameter.searchState
Expand All @@ -72,8 +71,7 @@ const createInstantSearchServer = algoliasearch => {
.filter(searchParameter => hasMultipleIndex(searchParameter.context))
.reduce((acc, searchParameter) => {
const index = getIndex(searchParameter.context);
const sp = searchParameter.getSearchParameters.call(
{ context: searchParameter.context },
const sp = searchParameter.getSearchParameters(
acc[index] ? acc[index] : sharedSearchParameters,
searchParameter.props,
searchParameter.searchState
Expand Down

0 comments on commit f34cb3d

Please sign in to comment.