Skip to content

Commit 079db57

Browse files
Yannick CroissantHaroenv
authored andcommitted
fix(InstantSearch): fix initialUIState when refinements are already present in the route (#4103)
1 parent 88f2615 commit 079db57

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/lib/InstantSearch.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ See ${createDocumentationLink({
232232
this._stalledSearchDelay = stalledSearchDelay;
233233
this._searchStalledTimer = null;
234234
this._isSearchStalled = false;
235-
this._initialUiState = initialUiState;
236235

237236
if (searchFunction) {
238237
this._searchFunction = searchFunction;
@@ -246,6 +245,15 @@ See ${createDocumentationLink({
246245
...routing,
247246
};
248247
}
248+
249+
if (this.routing) {
250+
this._initialUiState = {
251+
...initialUiState,
252+
...this.routing.stateMapping.routeToState(this.routing.router.read()),
253+
};
254+
} else {
255+
this._initialUiState = initialUiState;
256+
}
249257
}
250258

251259
/**

src/lib/__tests__/InstantSearch-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,39 @@ describe('start', () => {
440440
});
441441
});
442442

443+
it('forwards the router state to the main index', () => {
444+
const router = {
445+
read: jest.fn(() => ({
446+
indexName: {
447+
hierarchicalMenu: {
448+
'hierarchicalCategories.lvl0': ['Cell Phones'],
449+
},
450+
},
451+
})),
452+
write: jest.fn(),
453+
onUpdate: jest.fn(),
454+
createURL: jest.fn(() => '#'),
455+
};
456+
457+
const search = new InstantSearch({
458+
indexName: 'indexName',
459+
searchClient: createSearchClient(),
460+
routing: {
461+
router,
462+
},
463+
});
464+
465+
search.start();
466+
467+
expect(search.mainIndex.getWidgetState()).toEqual({
468+
indexName: {
469+
hierarchicalMenu: {
470+
'hierarchicalCategories.lvl0': ['Cell Phones'],
471+
},
472+
},
473+
});
474+
});
475+
443476
it('calls `init` on the added widgets', () => {
444477
const search = new InstantSearch({
445478
indexName: 'indexName',

0 commit comments

Comments
 (0)