Skip to content

Commit

Permalink
fix(instantsearch): warn deprecated usage of searchParameters (#4151)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Oct 1, 2019
1 parent 90ad88c commit 18e1c36
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
15 changes: 9 additions & 6 deletions .storybook/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ export const withHits = (
const search = instantsearch({
indexName,
searchClient: algoliasearch(appId, apiKey),
searchParameters: {
hitsPerPage: 4,
attributesToSnippet: ['description:15'],
snippetEllipsisText: '[…]',
...searchParameters,
},
routing: {
router: {
write: (routeState: object) => {
Expand All @@ -47,6 +41,15 @@ export const withHits = (
...instantsearchOptions,
});

search.addWidget(
instantsearch.widgets.configure({
hitsPerPage: 4,
attributesToSnippet: ['description:15'],
snippetEllipsisText: '[…]',
...searchParameters,
})
);

const containerElement = document.createElement('div');

// Add the preview container to add the stories in
Expand Down
21 changes: 20 additions & 1 deletion src/lib/InstantSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import version from './version';
import createHelpers from './createHelpers';
import {
createDocumentationMessageGenerator,
createDocumentationLink,
findIndex,
isPlainObject,
mergeDeep,
noop,
warning,
} from './utils';

const withUsage = createDocumentationMessageGenerator({
Expand Down Expand Up @@ -48,7 +50,7 @@ class InstantSearch extends EventEmitter {
const {
indexName = null,
numberLocale,
searchParameters = {},
searchParameters,
routing = null,
searchFunction,
stalledSearchDelay = 200,
Expand Down Expand Up @@ -88,6 +90,23 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend
throw new Error('The provided `insightsClient` must be a function.');
}

warning(
!searchParameters,
`The \`searchParameters\` option is deprecated and will not be supported in InstantSearch.js 4.x.
You can replace it with the \`configure\` widget:
\`\`\`
search.addWidgets([
configure(${JSON.stringify(searchParameters, null, 2)})
]);
\`\`\`
See ${createDocumentationLink({
name: 'configure',
})}`
);

this.client = searchClient;
this.insightsClient = insightsClient;
this.helper = null;
Expand Down
39 changes: 39 additions & 0 deletions src/lib/__tests__/InstantSearch-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import algoliaSearchHelper from 'algoliasearch-helper';
import InstantSearch from '../InstantSearch';
import version from '../version';
import { warning } from '../utils';

jest.mock('algoliasearch-helper', () => {
const module = require.requireActual('algoliasearch-helper');
Expand Down Expand Up @@ -217,6 +218,44 @@ describe('InstantSearch lifecycle', () => {
expect(algoliaSearchHelper).not.toHaveBeenCalled();
});

it('warns deprecated usage of `searchParameters`', () => {
warning.cache = {};

expect(() => {
// eslint-disable-next-line no-new
new InstantSearch({
indexName,
searchClient: algoliasearch(appId, apiKey),
searchParameters: {
disjunctiveFacets: ['brand'],
disjunctiveFacetsRefinements: {
brand: ['Samsung'],
},
},
});
})
.toWarnDev(`[InstantSearch.js]: The \`searchParameters\` option is deprecated and will not be supported in InstantSearch.js 4.x.
You can replace it with the \`configure\` widget:
\`\`\`
search.addWidgets([
configure({
"disjunctiveFacets": [
"brand"
],
"disjunctiveFacetsRefinements": {
"brand": [
"Samsung"
]
}
})
]);
\`\`\`
See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);
});

it('does not fail when passing same references inside multiple searchParameters props', () => {
const disjunctiveFacetsRefinements = { fruits: ['apple'] };
const facetsRefinements = disjunctiveFacetsRefinements;
Expand Down

0 comments on commit 18e1c36

Please sign in to comment.