Skip to content

Commit

Permalink
fix(algoliaOptions): ensure we keep default options
Browse files Browse the repository at this point in the history
While overriding the algoliaOptions, we were losing the default
hitsPerPage query parameters.

Fix #78
  • Loading branch information
redox committed Mar 10, 2016
1 parent be259f4 commit b284dda
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/lib/DocSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class DocSearch {
indexName,
inputSelector,
appId = 'BH4D9OD16A',
algoliaOptions = {
hitsPerPage: 5
},
algoliaOptions = {},
autocompleteOptions = {
debug: false,
hint: false
Expand All @@ -48,7 +46,7 @@ class DocSearch {
this.appId = appId;
this.indexName = indexName;
this.input = DocSearch.getInputFromSelector(inputSelector);
this.algoliaOptions = algoliaOptions;
this.algoliaOptions = {hitsPerPage: 5, ...algoliaOptions};
this.autocompleteOptions = autocompleteOptions;

this.client = algoliasearch(this.appId, this.apiKey);
Expand Down
40 changes: 35 additions & 5 deletions test/DocSearch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ describe('DocSearch', () => {
// Then
expect(actual.appId).toEqual('foo');
});
it('should allow customize algoliaOptions without loosing default options', () => {
// Given
let options = {
algoliaOptions: {
facetFilters: ['version:1.0']
},
...defaultOptions
};

// When
let actual = new DocSearch(options);

// Then
expect(actual.algoliaOptions).toEqual({hitsPerPage: 5, facetFilters: ['version:1.0']});
});
it('should allow customize hitsPerPage', () => {
// Given
let options = {
algoliaOptions: {
hitsPerPage: 10
},
...defaultOptions
};

// When
let actual = new DocSearch(options);

// Then
expect(actual.algoliaOptions).toEqual({hitsPerPage: 10});
});
it('should pass the input element as an instance property', () => {
// Given
let options = defaultOptions;
Expand All @@ -128,15 +158,16 @@ describe('DocSearch', () => {
// Given
let options = {
...defaultOptions,
algoliaOptions: 'algoliaOptions',
algoliaOptions: {anOption: 42},
autocompleteOptions: 'autocompleteOptions'
};

// When
let actual = new DocSearch(options);

// Then
expect(actual.algoliaOptions).toEqual('algoliaOptions');
expect(typeof actual.algoliaOptions).toEqual('object');
expect(actual.algoliaOptions.anOption).toEqual(42);
expect(actual.autocompleteOptions).toEqual('autocompleteOptions');
});
it('should instantiate algoliasearch with the correct values', () => {
Expand Down Expand Up @@ -294,8 +325,7 @@ describe('DocSearch', () => {
docsearch = new DocSearch({
indexName: 'indexName',
apiKey: 'apiKey',
inputSelector: '#input',
algoliaOptions: 'algoliaOptions'
inputSelector: '#input'
});
});

Expand Down Expand Up @@ -327,7 +357,7 @@ describe('DocSearch', () => {
let expectedArguments = {
indexName: 'indexName',
query: 'query',
params: 'algoliaOptions'
params: {hitsPerPage: 5}
};
expect(client.search.calledWith([expectedArguments])).toBe(true);
});
Expand Down

0 comments on commit b284dda

Please sign in to comment.