Skip to content

Commit

Permalink
fix(hitsPerPage): update lifecycle state (#3978)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored and Haroenv committed Oct 23, 2019
1 parent 0ea4950 commit d21d620
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
40 changes: 34 additions & 6 deletions src/connectors/hits-per-page/__tests__/connectHitsPerPage-test.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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({
Expand Down
12 changes: 8 additions & 4 deletions src/connectors/hits-per-page/connectHitsPerPage.js
Expand Up @@ -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 }) {
Expand Down
15 changes: 11 additions & 4 deletions 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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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(<Selector props />, container)', () => {
Expand Down

0 comments on commit d21d620

Please sign in to comment.