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

Commit

Permalink
fix(server): extend component correctly if at root (#1105)
Browse files Browse the repository at this point in the history
* fix(server): extend component correctly if at root

Essentially the problem is that $vnode is usually available, but not when the this is a root Vue instance. In that case we are in the "Vue.component" case, which before now always was wrong (it takes two arguments, not one)

Same as #1104
see also #1054

* Update src/util/createServerRootMixin.js

* make the funky new test pass

* update test name, as in #1104

* Update src/util/__tests__/createServerRootMixin.test.js

Co-authored-by: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>

Co-authored-by: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
  • Loading branch information
Haroenv and sarahdayan authored Jan 31, 2022
1 parent 7fb85d0 commit 4b0d295
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
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 when component is at root (and therefore 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 = new Vue({
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
5 changes: 4 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

0 comments on commit 4b0d295

Please sign in to comment.