Skip to content

Commit b284dda

Browse files
committed
fix(algoliaOptions): ensure we keep default options
While overriding the algoliaOptions, we were losing the default hitsPerPage query parameters. Fix #78
1 parent be259f4 commit b284dda

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/lib/DocSearch.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ class DocSearch {
3434
indexName,
3535
inputSelector,
3636
appId = 'BH4D9OD16A',
37-
algoliaOptions = {
38-
hitsPerPage: 5
39-
},
37+
algoliaOptions = {},
4038
autocompleteOptions = {
4139
debug: false,
4240
hint: false
@@ -48,7 +46,7 @@ class DocSearch {
4846
this.appId = appId;
4947
this.indexName = indexName;
5048
this.input = DocSearch.getInputFromSelector(inputSelector);
51-
this.algoliaOptions = algoliaOptions;
49+
this.algoliaOptions = {hitsPerPage: 5, ...algoliaOptions};
5250
this.autocompleteOptions = autocompleteOptions;
5351

5452
this.client = algoliasearch(this.appId, this.apiKey);

test/DocSearch-test.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ describe('DocSearch', () => {
111111
// Then
112112
expect(actual.appId).toEqual('foo');
113113
});
114+
it('should allow customize algoliaOptions without loosing default options', () => {
115+
// Given
116+
let options = {
117+
algoliaOptions: {
118+
facetFilters: ['version:1.0']
119+
},
120+
...defaultOptions
121+
};
122+
123+
// When
124+
let actual = new DocSearch(options);
125+
126+
// Then
127+
expect(actual.algoliaOptions).toEqual({hitsPerPage: 5, facetFilters: ['version:1.0']});
128+
});
129+
it('should allow customize hitsPerPage', () => {
130+
// Given
131+
let options = {
132+
algoliaOptions: {
133+
hitsPerPage: 10
134+
},
135+
...defaultOptions
136+
};
137+
138+
// When
139+
let actual = new DocSearch(options);
140+
141+
// Then
142+
expect(actual.algoliaOptions).toEqual({hitsPerPage: 10});
143+
});
114144
it('should pass the input element as an instance property', () => {
115145
// Given
116146
let options = defaultOptions;
@@ -128,15 +158,16 @@ describe('DocSearch', () => {
128158
// Given
129159
let options = {
130160
...defaultOptions,
131-
algoliaOptions: 'algoliaOptions',
161+
algoliaOptions: {anOption: 42},
132162
autocompleteOptions: 'autocompleteOptions'
133163
};
134164

135165
// When
136166
let actual = new DocSearch(options);
137167

138168
// Then
139-
expect(actual.algoliaOptions).toEqual('algoliaOptions');
169+
expect(typeof actual.algoliaOptions).toEqual('object');
170+
expect(actual.algoliaOptions.anOption).toEqual(42);
140171
expect(actual.autocompleteOptions).toEqual('autocompleteOptions');
141172
});
142173
it('should instantiate algoliasearch with the correct values', () => {
@@ -294,8 +325,7 @@ describe('DocSearch', () => {
294325
docsearch = new DocSearch({
295326
indexName: 'indexName',
296327
apiKey: 'apiKey',
297-
inputSelector: '#input',
298-
algoliaOptions: 'algoliaOptions'
328+
inputSelector: '#input'
299329
});
300330
});
301331

@@ -327,7 +357,7 @@ describe('DocSearch', () => {
327357
let expectedArguments = {
328358
indexName: 'indexName',
329359
query: 'query',
330-
params: 'algoliaOptions'
360+
params: {hitsPerPage: 5}
331361
};
332362
expect(client.search.calledWith([expectedArguments])).toBe(true);
333363
});

0 commit comments

Comments
 (0)