Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

fix(server): extend component correctly if at root #1105

Merged
merged 5 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
72 changes: 72 additions & 0 deletions src/util/__tests__/createServerRootMixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,78 @@ Array [
expect(instantsearch.helper).toEqual(expect.any(AlgoliaSearchHelper));
expect(instantsearch.mainHelper).toEqual(expect.any(AlgoliaSearchHelper));
});

it('works if component has no $vnode', async () => {
const searchClient = createFakeClient();
let mainIndex;

const app = {
render: h =>
/**
* This code triggers this warning in Vue 3:
* > Non-function value encountered for default slot. Prefer function slots for better performance.
*
* To fix it, replace the third argument
* > [h(...), h(...)]
* with
* > { default: () => [h(...), h(...)] }
*
* but it's not important (and not compatible in vue2), we're leaving it as-is.
*/
h(InstantSearchSsr, {}, [
h(Configure, {
attrs: {
hitsPerPage: 100,
},
}),
h(SearchBox),
]),
};

const wrapper = {
mixins: [
forceIsServerMixin,
createServerRootMixin({
searchClient,
indexName: 'hello',
}),
],
serverPrefetch() {
return this.instantsearch.findResultsState(this);
},
created() {
mainIndex = this.instantsearch.mainIndex;
},
render: h => h(app),
};

await renderToString(wrapper);

expect(mainIndex.getWidgetState()).toMatchInlineSnapshot(`
Object {
"hello": Object {
"configure": Object {
"hitsPerPage": 100,
},
},
}
`);

expect(searchClient.search).toHaveBeenCalledTimes(1);
expect(searchClient.search.mock.calls[0][0]).toMatchInlineSnapshot(`
Array [
Object {
"indexName": "hello",
"params": Object {
"facets": Array [],
"hitsPerPage": 100,
"query": "",
"tagFilters": "",
},
},
]
`);
});
});

describe('__forceRender', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/util/createServerRootMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ function defaultCloneComponent(componentInstance) {

const Extended = componentInstance.$vnode
? componentInstance.$vnode.componentOptions.Ctor.extend(options)
: Vue.component(Object.assign({}, componentInstance.$options, options));
: Vue.component(
options.name,
Object.assign({}, componentInstance.$options, options)
);

const app = new Extended({
propsData: componentInstance.$options.propsData,
Expand Down Expand Up @@ -239,6 +242,8 @@ function augmentInstantSearch(
parent: null,
uiState: search._initialUiState,
});

search.middleware.forEach(({ instance }) => instance.subscribe());
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
};

/* eslint-enable no-param-reassign */
Expand Down