From 9a21e49fa2ddc22409878ef8c6563ac21e31d148 Mon Sep 17 00:00:00 2001 From: Dhaya <154633+dhayab@users.noreply.github.com> Date: Mon, 2 Jan 2023 16:39:03 +0100 Subject: [PATCH] fix(vue-instantsearch): compute initial state from helper instead of results --- examples/vue/nuxt/package.json | 1 + .../__tests__/createServerRootMixin.test.js | 51 +++++++++++++++++-- .../src/util/createServerRootMixin.js | 17 ++++--- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/examples/vue/nuxt/package.json b/examples/vue/nuxt/package.json index e08c90fba9..0974807355 100644 --- a/examples/vue/nuxt/package.json +++ b/examples/vue/nuxt/package.json @@ -13,6 +13,7 @@ "dependencies": { "algoliasearch": "4.14.3", "cross-env": "^5.2.0", + "css-loader": "^4.3.0", "nuxt": "^2.4.5", "vue-instantsearch": "4.7.0", "vue-server-renderer": "2.7.14" diff --git a/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js b/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js index 2f1375a8ad..5fb4de395a 100644 --- a/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js +++ b/packages/vue-instantsearch/src/util/__tests__/createServerRootMixin.test.js @@ -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'; @@ -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, @@ -305,6 +306,16 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear }, }), h(SearchBox), + h( + Index, + { + attrs: { + indexName: 'hello', + indexId: 'nestedIndex', + }, + }, + [] + ), ]) ), async serverPrefetch() { @@ -312,8 +323,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear component: this, renderToString, }); - expect(state).toEqual({ - hello: { + + try { + expect(state).toEqual({ + hello: expect.objectContaining({}), + nestedIndex: expect.objectContaining({}), + }); + + expect(state.hello).toEqual({ results: [ { query: '', @@ -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; }, diff --git a/packages/vue-instantsearch/src/util/createServerRootMixin.js b/packages/vue-instantsearch/src/util/createServerRootMixin.js index 8a254a1773..a48908bc42 100644 --- a/packages/vue-instantsearch/src/util/createServerRootMixin.js +++ b/packages/vue-instantsearch/src/util/createServerRootMixin.js @@ -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, }; });