Skip to content

Commit d21d620

Browse files
francoischalifourHaroenv
authored andcommitted
fix(hitsPerPage): update lifecycle state (#3978)
1 parent 0ea4950 commit d21d620

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
6464
],
6565
});
6666

67-
expect(typeof widget.getConfiguration).toEqual('function');
68-
expect(widget.getConfiguration()).toEqual({});
67+
expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
68+
new SearchParameters({})
69+
);
6970

7071
expect(renderFn).toHaveBeenCalledTimes(0);
7172

@@ -173,9 +174,30 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits-per-pa
173174
],
174175
});
175176

176-
expect(widget.getConfiguration()).toEqual({
177-
hitsPerPage: 10,
177+
expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
178+
new SearchParameters({
179+
hitsPerPage: 10,
180+
})
181+
);
182+
});
183+
184+
it('Configures the search with the previous hitsPerPage', () => {
185+
const renderFn = jest.fn();
186+
const makeWidget = connectHitsPerPage(renderFn);
187+
const widget = makeWidget({
188+
items: [
189+
{ value: 3, label: '3 items per page' },
190+
{ value: 10, label: '10 items per page', default: true },
191+
],
178192
});
193+
194+
expect(
195+
widget.getConfiguration(new SearchParameters({ hitsPerPage: 20 }))
196+
).toEqual(
197+
new SearchParameters({
198+
hitsPerPage: 20,
199+
})
200+
);
179201
});
180202

181203
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
188210
],
189211
});
190212

191-
expect(widget.getConfiguration()).toEqual({});
213+
expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
214+
new SearchParameters({})
215+
);
192216
});
193217

194218
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
508532
],
509533
});
510534

511-
const helper = algoliasearchHelper({}, '', widget.getConfiguration({}));
535+
const helper = algoliasearchHelper(
536+
{},
537+
'',
538+
widget.getConfiguration(new SearchParameters({}))
539+
);
512540
helper.search = jest.fn();
513541

514542
widget.init({

src/connectors/hits-per-page/connectHitsPerPage.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ export default function connectHitsPerPage(renderFn, unmountFn = noop) {
120120
return {
121121
$$type: 'ais.hitsPerPage',
122122

123-
getConfiguration() {
124-
return defaultValues.length > 0
125-
? { hitsPerPage: defaultValues[0].value }
126-
: {};
123+
getConfiguration(state) {
124+
if (!defaultValue) {
125+
return state;
126+
}
127+
128+
return state.setQueryParameters({
129+
hitsPerPage: state.hitsPerPage || defaultValue.value,
130+
});
127131
},
128132

129133
init({ helper, createURL, state, instantSearchInstance }) {

src/widgets/hits-per-page/__tests__/hits-per-page-test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { render } from 'preact-compat';
2+
import { SearchParameters } from 'algoliasearch-helper';
23
import hitsPerPage from '../hits-per-page';
34

45
jest.mock('preact-compat', () => {
@@ -62,7 +63,9 @@ describe('hitsPerPage()', () => {
6263

6364
it('does not configure the default hits per page if not specified', () => {
6465
expect(typeof widget.getConfiguration).toEqual('function');
65-
expect(widget.getConfiguration()).toEqual({});
66+
expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
67+
new SearchParameters({})
68+
);
6669
});
6770

6871
it('does configures the default hits per page if specified', () => {
@@ -74,9 +77,13 @@ describe('hitsPerPage()', () => {
7477
],
7578
});
7679

77-
expect(widgetWithDefaults.getConfiguration()).toEqual({
78-
hitsPerPage: 20,
79-
});
80+
expect(
81+
widgetWithDefaults.getConfiguration(new SearchParameters({}))
82+
).toEqual(
83+
new SearchParameters({
84+
hitsPerPage: 20,
85+
})
86+
);
8087
});
8188

8289
it('calls twice render(<Selector props />, container)', () => {

0 commit comments

Comments
 (0)