Skip to content

Commit

Permalink
fix(InstantSearch): fix initialUIState when refinements are already p…
Browse files Browse the repository at this point in the history
…resent in the route (#4103)
  • Loading branch information
yannickcr authored and Haroenv committed Oct 23, 2019
1 parent 88f2615 commit 079db57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib/InstantSearch.ts
Expand Up @@ -232,7 +232,6 @@ See ${createDocumentationLink({
this._stalledSearchDelay = stalledSearchDelay;
this._searchStalledTimer = null;
this._isSearchStalled = false;
this._initialUiState = initialUiState;

if (searchFunction) {
this._searchFunction = searchFunction;
Expand All @@ -246,6 +245,15 @@ See ${createDocumentationLink({
...routing,
};
}

if (this.routing) {
this._initialUiState = {
...initialUiState,
...this.routing.stateMapping.routeToState(this.routing.router.read()),
};
} else {
this._initialUiState = initialUiState;
}
}

/**
Expand Down
33 changes: 33 additions & 0 deletions src/lib/__tests__/InstantSearch-test.js
Expand Up @@ -440,6 +440,39 @@ describe('start', () => {
});
});

it('forwards the router state to the main index', () => {
const router = {
read: jest.fn(() => ({
indexName: {
hierarchicalMenu: {
'hierarchicalCategories.lvl0': ['Cell Phones'],
},
},
})),
write: jest.fn(),
onUpdate: jest.fn(),
createURL: jest.fn(() => '#'),
};

const search = new InstantSearch({
indexName: 'indexName',
searchClient: createSearchClient(),
routing: {
router,
},
});

search.start();

expect(search.mainIndex.getWidgetState()).toEqual({
indexName: {
hierarchicalMenu: {
'hierarchicalCategories.lvl0': ['Cell Phones'],
},
},
});
});

it('calls `init` on the added widgets', () => {
const search = new InstantSearch({
indexName: 'indexName',
Expand Down

0 comments on commit 079db57

Please sign in to comment.