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(vue-instantsearch): compute initial state from helper instead of results #5403

Merged
merged 1 commit into from
Jan 2, 2023
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
1 change: 1 addition & 0 deletions examples/vue/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"algoliasearch": "4.14.3",
"cross-env": "^5.2.0",
"css-loader": "^4.3.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this line, the example doesn't run (probably due to it being in a monorepo now). I set the version to one that is already in yarn.lock.

"nuxt": "^2.4.5",
"vue-instantsearch": "4.7.0",
"vue-server-renderer": "2.7.14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createServerRootMixin } from '../createServerRootMixin';
import InstantSearchSsr from '../../components/InstantSearchSsr';
import Configure from '../../components/Configure';
import SearchBox from '../../components/SearchBox.vue';
import Index from '../../components/Index';
import { createWidgetMixin } from '../../mixins/widget';
import { createFakeClient } from '../testutils/client';
import { createSerializedState } from '../testutils/helper';
Expand Down Expand Up @@ -288,7 +289,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear
it('returns correct results state', () => {
const searchClient = createFakeClient();

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const app = {
mixins: [
forceIsServerMixin,
Expand All @@ -305,15 +306,31 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear
},
}),
h(SearchBox),
h(
Index,
{
attrs: {
indexName: 'hello',
indexId: 'nestedIndex',
},
},
[]
),
])
),
async serverPrefetch() {
const state = await this.instantsearch.findResultsState({
component: this,
renderToString,
});
expect(state).toEqual({
hello: {

try {
expect(state).toEqual({
hello: expect.objectContaining({}),
nestedIndex: expect.objectContaining({}),
});

expect(state.hello).toEqual({
results: [
{
query: '',
Expand All @@ -333,8 +350,32 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear
query: '',
tagRefinements: [],
},
},
});
});

// Parent's widgets state should not be merged into nested index state
expect(state.nestedIndex).toEqual({
results: [
{
query: '',
},
],
state: {
disjunctiveFacets: [],
disjunctiveFacetsRefinements: {},
facets: [],
facetsExcludes: {},
facetsRefinements: {},
hierarchicalFacets: [],
hierarchicalFacetsRefinements: {},
index: 'hello',
numericRefinements: {},
tagRefinements: [],
},
});
} catch (err) {
reject(err);
}

resolve();
return state;
},
Expand Down
17 changes: 9 additions & 8 deletions packages/vue-instantsearch/src/util/createServerRootMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ function augmentInstantSearch(instantSearchOptions, cloneComponent) {
.then(() => {
initialResults = {};
walkIndex(instance.mainIndex, (widget) => {
const { _state, _rawResults } = widget.getResults();

initialResults[widget.getIndexId()] = {
// copy just the values of SearchParameters, not the functions
state: Object.keys(_state).reduce((acc, key) => {
// eslint-disable-next-line no-param-reassign
acc[key] = _state[key];
return acc;
}, {}),
results: _rawResults,
state: Object.entries(widget.getHelper().state).reduce(
(acc, [key, value]) => {
// eslint-disable-next-line no-param-reassign
acc[key] = value;
return acc;
},
{}
),
results: widget.getResults()._rawResults,
};
});

Expand Down