Skip to content

Commit 952dc70

Browse files
samoussHaroenv
authored andcommitted
fix(index): ensure that we always use the index set by widgets (#4125)
This PR fixes an issue where the initial index name is not correctly set. When a widget manages the `index` (e.g. `sortBy`), the first computation of the `SearchParameters` could be incorrect. This is because the `Helper` **always override** the `index` provided by the last argument (see [L124](https://github.com/algolia/algoliasearch-helper-js/blob/5a0352aa233c5ea932df6b054a16989c8d302404/src/algoliasearch.helper.js#L124)). The value set by the widget will be overridden anyway. We now first compute the `SearchParameters` from the widgets with an initial value that contains the top-level `indexName`. If one of the widgets overrides it the return value will contain the correct index if not the original index is preserved (this is not true at the moment, `connectSortBy` will be fixed in a separate #4126). Then we use the return value for both arguments of the Helper.
1 parent e51d35b commit 952dc70

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

src/lib/__tests__/RoutingManager-test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ describe('RoutingManager', () => {
127127
...uiState,
128128
q: searchParameters.query,
129129
})),
130-
getWidgetSearchParameters: jest.fn(),
130+
getWidgetSearchParameters: jest.fn(
131+
searchParameters => searchParameters
132+
),
131133
};
132134

133135
search.addWidget(widget);
@@ -240,13 +242,15 @@ describe('RoutingManager', () => {
240242

241243
search.addWidget(
242244
createWidget({
243-
getWidgetSearchParameters: jest.fn(),
244245
getWidgetState(uiState, { searchParameters }) {
245246
return {
246247
...uiState,
247248
query: searchParameters.query,
248249
};
249250
},
251+
getWidgetSearchParameters: jest.fn(
252+
searchParameters => searchParameters
253+
),
250254
})
251255
);
252256

src/widgets/index/__tests__/index-test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,42 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
681681
);
682682
});
683683

684+
it('uses the index set by the widget for the queries', () => {
685+
const instance = index({ indexName: 'indexName' });
686+
const searchClient = createSearchClient();
687+
const mainHelper = algoliasearchHelper(searchClient, '', {});
688+
const instantSearchInstance = createInstantSearch({
689+
mainHelper,
690+
});
691+
692+
instance.addWidgets([
693+
createWidget({
694+
getWidgetSearchParameters(state) {
695+
return state.setQueryParameter('index', 'widgetIndexName');
696+
},
697+
}),
698+
]);
699+
700+
instance.init(
701+
createInitOptions({
702+
instantSearchInstance,
703+
parent: null,
704+
})
705+
);
706+
707+
// Simulate a call to search from a widget
708+
instance.getHelper()!.search();
709+
710+
expect(searchClient.search).toHaveBeenCalledWith(
711+
expect.arrayContaining([
712+
{
713+
indexName: 'widgetIndexName',
714+
params: expect.any(Object),
715+
},
716+
])
717+
);
718+
});
719+
684720
it('inherits from the parent states for the queries', () => {
685721
const level0 = index({ indexName: 'level0IndexName' });
686722
const level1 = index({ indexName: 'level1IndexName' });

src/widgets/index/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,17 @@ const index = (props: IndexProps): Index => {
288288
// inside InstantSearch at the `start` method, which occurs before the `init`
289289
// step.
290290
const mainHelper = instantSearchInstance.mainHelper!;
291+
const parameters = getLocalWidgetsSearchParameters(localWidgets, {
292+
uiState: localUiState,
293+
initialSearchParameters: new algoliasearchHelper.SearchParameters({
294+
index: indexName,
295+
}),
296+
});
291297

292298
// This Helper is only used for state management we do not care about the
293299
// `searchClient`. Only the "main" Helper created at the `InstantSearch`
294300
// level is aware of the client.
295-
helper = algoliasearchHelper(
296-
{} as Client,
297-
indexName,
298-
getLocalWidgetsSearchParameters(localWidgets, {
299-
uiState: localUiState,
300-
initialSearchParameters: new algoliasearchHelper.SearchParameters(),
301-
})
302-
);
301+
helper = algoliasearchHelper({} as Client, parameters.index, parameters);
303302

304303
// We forward the call to `search` to the "main" instance of the Helper
305304
// which is responsible for managing the queries (it's the only one that is

0 commit comments

Comments
 (0)