Skip to content

Commit 355f080

Browse files
Maxime Jantonbobylito
authored andcommitted
feat(hitsPerPageSelector): default hits per page setting
* feat(hitsPerPageSelector): accept default value into items[] * doc(dev): add example of hitPerPageSelector with default value * test(connectHitsPerPage): it now provides `getConfiguration()` * fix(connectHitsPerPage): dont attribute a default value when none specified * test(hitsPerPage): comply to no default value when none specified * feat(hitsPerPage): display warning when more than one default value * refactor(connectHitsPerPage): remove a loop * feat(connectHitsPerPage): thrown an error instead of warning * refactor(connectHitsPerPage): check for error on init * test(connectHitsPerPage): throw when two or more default * test(hits-per-page-selector): configuration is correct
1 parent 61569c4 commit 355f080

File tree

5 files changed

+110
-20
lines changed

5 files changed

+110
-20
lines changed

dev/app/init-builtin-widgets.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,37 @@ export default () => {
7676
})
7777
);
7878

79-
storiesOf('HitsPerPageSelector').add(
80-
'default',
81-
wrapWithHits(container => {
82-
window.search.addWidget(
83-
instantsearch.widgets.hitsPerPageSelector({
84-
container,
85-
items: [
86-
{ value: 3, label: '3 per page' },
87-
{ value: 5, label: '5 per page' },
88-
{ value: 10, label: '10 per page' },
89-
],
90-
})
91-
);
92-
})
93-
);
79+
storiesOf('HitsPerPageSelector')
80+
.add(
81+
'default',
82+
wrapWithHits(container => {
83+
window.search.addWidget(
84+
instantsearch.widgets.hitsPerPageSelector({
85+
container,
86+
items: [
87+
{ value: 3, label: '3 per page' },
88+
{ value: 5, label: '5 per page' },
89+
{ value: 10, label: '10 per page' },
90+
],
91+
})
92+
);
93+
})
94+
)
95+
.add(
96+
'With default hitPerPage to 5',
97+
wrapWithHits(container => {
98+
window.search.addWidget(
99+
instantsearch.widgets.hitsPerPageSelector({
100+
container,
101+
items: [
102+
{ value: 3, label: '3 per page' },
103+
{ value: 5, label: '5 per page', default: true },
104+
{ value: 10, label: '10 per page' },
105+
],
106+
})
107+
);
108+
})
109+
);
94110

95111
storiesOf('Hits').add(
96112
'default',

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import connectHitsPerPage from '../connectHitsPerPage.js';
77
const fakeClient = { addAlgoliaAgent: () => {} };
88

99
describe('connectHitsPerPage', () => {
10+
it('should throw when there is two default items defined', () => {
11+
expect(() => {
12+
connectHitsPerPage(() => {})({
13+
items: [
14+
{ value: 3, label: '3 items per page', default: true },
15+
{ value: 10, label: '10 items per page', default: true },
16+
],
17+
});
18+
}).toThrow(/^\[Error\]/);
19+
});
20+
1021
it('Renders during init and render', () => {
1122
// test that the dummyRendering is called with the isFirstRendering
1223
// flag set accordingly
@@ -19,7 +30,8 @@ describe('connectHitsPerPage', () => {
1930
],
2031
});
2132

22-
expect(widget.getConfiguration).toEqual(undefined);
33+
expect(typeof widget.getConfiguration).toEqual('function');
34+
expect(widget.getConfiguration()).toEqual({});
2335

2436
// test if widget is not rendered yet at this point
2537
expect(rendering.callCount).toBe(0);
@@ -65,6 +77,34 @@ describe('connectHitsPerPage', () => {
6577
});
6678
});
6779

80+
it('Configures the search with the default hitsPerPage provided', () => {
81+
const rendering = sinon.stub();
82+
const makeWidget = connectHitsPerPage(rendering);
83+
const widget = makeWidget({
84+
items: [
85+
{ value: 3, label: '3 items per page' },
86+
{ value: 10, label: '10 items per page', default: true },
87+
],
88+
});
89+
90+
expect(widget.getConfiguration()).toEqual({
91+
hitsPerPage: 10,
92+
});
93+
});
94+
95+
it('Does not configures the search when there is no default value', () => {
96+
const rendering = sinon.stub();
97+
const makeWidget = connectHitsPerPage(rendering);
98+
const widget = makeWidget({
99+
items: [
100+
{ value: 3, label: '3 items per page' },
101+
{ value: 10, label: '10 items per page' },
102+
],
103+
});
104+
105+
expect(widget.getConfiguration()).toEqual({});
106+
});
107+
68108
it('Provide a function to change the current hits per page, and provide the current value', () => {
69109
const rendering = sinon.stub();
70110
const makeWidget = connectHitsPerPage(rendering);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var customHitsPerPage = connectHitsPerPage(function render(params, isFirstRender
1515
search.addWidget(
1616
customHitsPerPage({
1717
items: [
18+
{value: 5, label: '5 results per page', default: true},
1819
{value: 10, label: '10 results per page'},
1920
{value: 42, label: '42 results per page'},
2021
],
@@ -34,6 +35,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
3435
* @typedef {Object} HitsPerPageWidgetOptionsItem
3536
* @property {number} value Number of hits to display per page.
3637
* @property {string} label Label to display in the option.
38+
* @property {boolean} default The default hits per page on first search.
3739
*/
3840

3941
/**
@@ -96,7 +98,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
9698
* customHitsPerPage({
9799
* containerNode: $('#custom-hits-per-page-container'),
98100
* items: [
99-
* {value: 6, label: '6 per page'},
101+
* {value: 6, label: '6 per page', default: true},
100102
* {value: 12, label: '12 per page'},
101103
* {value: 24, label: '24 per page'},
102104
* ],
@@ -114,7 +116,21 @@ export default function connectHitsPerPage(renderFn) {
114116
throw new Error(usage);
115117
}
116118

119+
const defaultValues = items.filter(item => item.default);
120+
if (defaultValues.length > 1) {
121+
throw new Error(
122+
`[Error][hitsPerPageSelector] more than one default value is specified in \`items[]\`
123+
The first one will be picked, you should probably set only one default value`
124+
);
125+
}
126+
117127
return {
128+
getConfiguration() {
129+
return defaultValues.length > 0
130+
? { hitsPerPage: defaultValues[0].value }
131+
: {};
132+
},
133+
118134
init({ helper, state, instantSearchInstance }) {
119135
const isCurrentInOptions = some(
120136
items,

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,23 @@ describe('hitsPerPageSelector()', () => {
5656
};
5757
});
5858

59-
it("doesn't configure anything", () => {
60-
expect(widget.getConfiguration).toEqual(undefined);
59+
it('does not configure the default hits per page if not specified', () => {
60+
expect(typeof widget.getConfiguration).toEqual('function');
61+
expect(widget.getConfiguration()).toEqual({});
62+
});
63+
64+
it('does configures the default hits per page if specified', () => {
65+
const widgetWithDefaults = hitsPerPageSelector({
66+
container: document.createElement('div'),
67+
items: [
68+
{ value: 10, label: '10 results' },
69+
{ value: 20, label: '20 results', default: true },
70+
],
71+
});
72+
73+
expect(widgetWithDefaults.getConfiguration()).toEqual({
74+
hitsPerPage: 20,
75+
});
6176
});
6277

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

src/widgets/hits-per-page-selector/hits-per-page-selector.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ hitsPerPageSelector({
5050
* @typedef {Object} HitsPerPageSelectorItems
5151
* @property {number} value number of hits to display per page.
5252
* @property {string} label Label to display in the option.
53+
* @property {boolean} default The default hits per page on first search.
5354
*/
5455

5556
/**
@@ -63,6 +64,8 @@ hitsPerPageSelector({
6364
/**
6465
* The hitsPerPageSelector widget gives the user the ability to change the number of results
6566
* displayed in the hits widget.
67+
*
68+
* You can specify the default hits per page using a boolean in the items[] array. If none is specified, this first hits per page option will be picked.
6669
* @type {WidgetFactory}
6770
* @category basic
6871
* @param {HitsPerPageSelectorWidgetOptions} $0 The options of the HitPerPageSelector widget.
@@ -72,7 +75,7 @@ hitsPerPageSelector({
7275
* instantsearch.widgets.hitsPerPageSelector({
7376
* container: '#hits-per-page-selector',
7477
* items: [
75-
* {value: 3, label: '3 per page'},
78+
* {value: 3, label: '3 per page', default: true},
7679
* {value: 6, label: '6 per page'},
7780
* {value: 12, label: '12 per page'},
7881
* ]

0 commit comments

Comments
 (0)