From 0528b55999099aee4981f745027f175a0df49094 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Thu, 18 Jun 2020 14:07:16 +0200 Subject: [PATCH] fix(ssr): allow getWidgetState to be empty for createURL (#805) * fix(ssr): allow getWidgetState to be empty for createURL This must have been caused by a wrong copy-paste * test(ssr): add case for createURL --- .../__tests__/createServerRootMixin.test.js | 87 ++++++++++++++----- src/util/createServerRootMixin.js | 2 +- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/util/__tests__/createServerRootMixin.test.js b/src/util/__tests__/createServerRootMixin.test.js index 5131f1e9f..d62a9c5b8 100644 --- a/src/util/__tests__/createServerRootMixin.test.js +++ b/src/util/__tests__/createServerRootMixin.test.js @@ -433,35 +433,78 @@ Object { ); }); - it('has a fake createURL', () => { - const app = new Vue({ - mixins: [ - createServerRootMixin({ - searchClient: createFakeClient(), - indexName: 'lol', - }), - ], - }); + describe('createURL', () => { + it('returns # if instantsearch has no routing', () => { + const app = new Vue({ + mixins: [ + createServerRootMixin({ + searchClient: createFakeClient(), + indexName: 'lol', + }), + ], + }); - const widget = { - init: jest.fn(), - render: jest.fn(), - }; + const widget = { + init: jest.fn(), + render: jest.fn(), + }; - const instantSearchInstance = app.$data.instantsearch; + const instantSearchInstance = app.$data.instantsearch; - instantSearchInstance.hydrate({ - lol: createSerializedState(), + instantSearchInstance.hydrate({ + lol: createSerializedState(), + }); + + instantSearchInstance.__forceRender( + widget, + instantSearchInstance.mainIndex + ); + + const renderArgs = widget.render.mock.calls[0][0]; + + expect(renderArgs.createURL()).toBe('#'); }); - instantSearchInstance.__forceRender( - widget, - instantSearchInstance.mainIndex - ); + it('allows for widgets without getWidgetState', () => { + const app = new Vue({ + mixins: [ + createServerRootMixin({ + searchClient: createFakeClient(), + indexName: 'lol', + }), + ], + }); + + const widget = { + init: jest.fn(), + render: jest.fn(), + getWidgetState(uiState) { + return uiState; + }, + }; - const renderArgs = widget.render.mock.calls[0][0]; + const widgetWithoutGetWidgetState = { + init: jest.fn(), + render: jest.fn(), + }; + + const instantSearchInstance = app.$data.instantsearch; + + instantSearchInstance.hydrate({ + lol: createSerializedState(), + }); - expect(renderArgs.createURL()).toBe('#'); + instantSearchInstance.addWidgets([widget, widgetWithoutGetWidgetState]); + + instantSearchInstance.__forceRender( + widget, + instantSearchInstance.mainIndex + ); + + const renderArgs = widget.render.mock.calls[0][0]; + + expect(renderArgs.createURL()).toBe('#'); + }); }); }); }); diff --git a/src/util/createServerRootMixin.js b/src/util/createServerRootMixin.js index f3e1763c1..5d656f999 100644 --- a/src/util/createServerRootMixin.js +++ b/src/util/createServerRootMixin.js @@ -167,7 +167,7 @@ function augmentInstantSearch(instantSearchOptions, searchClient, indexName) { .getWidgets() .filter(w => w.$$type !== 'ais.index') .reduce((uiState, w) => { - if (!widget.getWidgetState) { + if (!w.getWidgetState) { return uiState; }