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(InstantSearch): fix initialUIState when refinements are already present in the route #4103

Merged
merged 2 commits into from
Sep 3, 2019
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
10 changes: 9 additions & 1 deletion src/lib/InstantSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend
this._stalledSearchDelay = stalledSearchDelay;
this._searchStalledTimer = null;
this._isSearchStalled = false;
this._initialUiState = initialUiState;

if (searchFunction) {
this._searchFunction = searchFunction;
Expand All @@ -227,6 +226,15 @@ See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend
...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
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,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