From d21d6203e68e144ecf722ab8fa48b6c80d96be52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Wed, 24 Jul 2019 12:34:02 +0200 Subject: [PATCH] fix(hitsPerPage): update lifecycle state (#3978) --- .../__tests__/connectHitsPerPage-test.js | 40 ++++++++++++++++--- .../hits-per-page/connectHitsPerPage.js | 12 ++++-- .../__tests__/hits-per-page-test.js | 15 +++++-- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js b/src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js index 6f0050c2d7..f10f3a6076 100644 --- a/src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js +++ b/src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js @@ -64,8 +64,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa ], }); - expect(typeof widget.getConfiguration).toEqual('function'); - expect(widget.getConfiguration()).toEqual({}); + expect(widget.getConfiguration(new SearchParameters({}))).toEqual( + new SearchParameters({}) + ); expect(renderFn).toHaveBeenCalledTimes(0); @@ -173,9 +174,30 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa ], }); - expect(widget.getConfiguration()).toEqual({ - hitsPerPage: 10, + expect(widget.getConfiguration(new SearchParameters({}))).toEqual( + new SearchParameters({ + hitsPerPage: 10, + }) + ); + }); + + it('Configures the search with the previous hitsPerPage', () => { + const renderFn = jest.fn(); + const makeWidget = connectHitsPerPage(renderFn); + const widget = makeWidget({ + items: [ + { value: 3, label: '3 items per page' }, + { value: 10, label: '10 items per page', default: true }, + ], }); + + expect( + widget.getConfiguration(new SearchParameters({ hitsPerPage: 20 })) + ).toEqual( + new SearchParameters({ + hitsPerPage: 20, + }) + ); }); it('Does not configures the search when there is no default value', () => { @@ -188,7 +210,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa ], }); - expect(widget.getConfiguration()).toEqual({}); + expect(widget.getConfiguration(new SearchParameters({}))).toEqual( + new SearchParameters({}) + ); }); it('Provide a function to change the current hits per page, and provide the current value', () => { @@ -508,7 +532,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa ], }); - const helper = algoliasearchHelper({}, '', widget.getConfiguration({})); + const helper = algoliasearchHelper( + {}, + '', + widget.getConfiguration(new SearchParameters({})) + ); helper.search = jest.fn(); widget.init({ diff --git a/src/connectors/hits-per-page/connectHitsPerPage.js b/src/connectors/hits-per-page/connectHitsPerPage.js index 5e7d4b7458..1d22506068 100644 --- a/src/connectors/hits-per-page/connectHitsPerPage.js +++ b/src/connectors/hits-per-page/connectHitsPerPage.js @@ -120,10 +120,14 @@ export default function connectHitsPerPage(renderFn, unmountFn = noop) { return { $$type: 'ais.hitsPerPage', - getConfiguration() { - return defaultValues.length > 0 - ? { hitsPerPage: defaultValues[0].value } - : {}; + getConfiguration(state) { + if (!defaultValue) { + return state; + } + + return state.setQueryParameters({ + hitsPerPage: state.hitsPerPage || defaultValue.value, + }); }, init({ helper, createURL, state, instantSearchInstance }) { diff --git a/src/widgets/hits-per-page/__tests__/hits-per-page-test.js b/src/widgets/hits-per-page/__tests__/hits-per-page-test.js index 87a3688e0d..90fff33c11 100644 --- a/src/widgets/hits-per-page/__tests__/hits-per-page-test.js +++ b/src/widgets/hits-per-page/__tests__/hits-per-page-test.js @@ -1,4 +1,5 @@ import { render } from 'preact-compat'; +import { SearchParameters } from 'algoliasearch-helper'; import hitsPerPage from '../hits-per-page'; jest.mock('preact-compat', () => { @@ -62,7 +63,9 @@ describe('hitsPerPage()', () => { it('does not configure the default hits per page if not specified', () => { expect(typeof widget.getConfiguration).toEqual('function'); - expect(widget.getConfiguration()).toEqual({}); + expect(widget.getConfiguration(new SearchParameters({}))).toEqual( + new SearchParameters({}) + ); }); it('does configures the default hits per page if specified', () => { @@ -74,9 +77,13 @@ describe('hitsPerPage()', () => { ], }); - expect(widgetWithDefaults.getConfiguration()).toEqual({ - hitsPerPage: 20, - }); + expect( + widgetWithDefaults.getConfiguration(new SearchParameters({})) + ).toEqual( + new SearchParameters({ + hitsPerPage: 20, + }) + ); }); it('calls twice render(, container)', () => {