Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(searchParameters): avoid mutating provided objects #1148

Merged
merged 1 commit into from
Jul 26, 2016
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
26 changes: 18 additions & 8 deletions src/lib/InstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import forEach from 'lodash/collection/forEach';
import merge from 'lodash/object/merge';
import union from 'lodash/array/union';
import clone from 'lodash/lang/clone';
import isObject from 'lodash/lang/isObject';
import {EventEmitter} from 'events';
import urlSyncWidget from './url-sync.js';
import version from './version.js';
Expand Down Expand Up @@ -194,19 +195,28 @@ Usage: instantsearch({
function enhanceConfiguration(configuration, widgetDefinition) {
if (!widgetDefinition.getConfiguration) return configuration;

// Update searchParameters with the configuration from the widgets
// Get the relevant partial configuration asked by the widget
const partialConfiguration = widgetDefinition.getConfiguration(configuration);

const customizer = (a, b) => {
// always create a unified array for facets refinements
if (Array.isArray(a)) {
return union(a, b);
}

// avoid mutating objects
if (isObject(a)) {
return merge({}, a, b, customizer);
}

return undefined;
};

return merge(
{},
configuration,
partialConfiguration,
(a, b) => {
if (Array.isArray(a)) {
return union(a, b);
}

return undefined;
}
customizer
);
}

Expand Down
20 changes: 20 additions & 0 deletions src/lib/__tests__/InstantSearch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ describe('InstantSearch lifecycle', () => {
expect(helperSearchSpy.calledOnce).toBe(false);
});

it('does not fail when passing same references inside multiple searchParameters props', () => {
const disjunctiveFacetsRefinements = {fruits: ['apple']};
const facetsRefinements = disjunctiveFacetsRefinements;
search = new InstantSearch({
appId,
apiKey,
indexName,
searchParameters: {
disjunctiveFacetsRefinements,
facetsRefinements
}
});
search.addWidget({
getConfiguration: () => ({disjunctiveFacetsRefinements: {fruits: ['orange']}}),
init: () => {}
});
search.start();
expect(search.searchParameters.facetsRefinements).toEqual({fruits: ['apple']});
});

context('when adding a widget', () => {
let widget;

Expand Down