fix(algoliaOptions): ensure we keep default options
While overriding the algoliaOptions, we were losing the default hitsPerPage query parameters. Fix #78
This commit is contained in:
parent
be259f486c
commit
b284dda7a9
2 changed files with 37 additions and 9 deletions
|
|
@ -34,9 +34,7 @@ class DocSearch {
|
||||||
indexName,
|
indexName,
|
||||||
inputSelector,
|
inputSelector,
|
||||||
appId = 'BH4D9OD16A',
|
appId = 'BH4D9OD16A',
|
||||||
algoliaOptions = {
|
algoliaOptions = {},
|
||||||
hitsPerPage: 5
|
|
||||||
},
|
|
||||||
autocompleteOptions = {
|
autocompleteOptions = {
|
||||||
debug: false,
|
debug: false,
|
||||||
hint: false
|
hint: false
|
||||||
|
|
@ -48,7 +46,7 @@ class DocSearch {
|
||||||
this.appId = appId;
|
this.appId = appId;
|
||||||
this.indexName = indexName;
|
this.indexName = indexName;
|
||||||
this.input = DocSearch.getInputFromSelector(inputSelector);
|
this.input = DocSearch.getInputFromSelector(inputSelector);
|
||||||
this.algoliaOptions = algoliaOptions;
|
this.algoliaOptions = {hitsPerPage: 5, ...algoliaOptions};
|
||||||
this.autocompleteOptions = autocompleteOptions;
|
this.autocompleteOptions = autocompleteOptions;
|
||||||
|
|
||||||
this.client = algoliasearch(this.appId, this.apiKey);
|
this.client = algoliasearch(this.appId, this.apiKey);
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,36 @@ describe('DocSearch', () => {
|
||||||
// Then
|
// Then
|
||||||
expect(actual.appId).toEqual('foo');
|
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', () => {
|
it('should pass the input element as an instance property', () => {
|
||||||
// Given
|
// Given
|
||||||
let options = defaultOptions;
|
let options = defaultOptions;
|
||||||
|
|
@ -128,7 +158,7 @@ describe('DocSearch', () => {
|
||||||
// Given
|
// Given
|
||||||
let options = {
|
let options = {
|
||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
algoliaOptions: 'algoliaOptions',
|
algoliaOptions: {anOption: 42},
|
||||||
autocompleteOptions: 'autocompleteOptions'
|
autocompleteOptions: 'autocompleteOptions'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -136,7 +166,8 @@ describe('DocSearch', () => {
|
||||||
let actual = new DocSearch(options);
|
let actual = new DocSearch(options);
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
expect(actual.algoliaOptions).toEqual('algoliaOptions');
|
expect(typeof actual.algoliaOptions).toEqual('object');
|
||||||
|
expect(actual.algoliaOptions.anOption).toEqual(42);
|
||||||
expect(actual.autocompleteOptions).toEqual('autocompleteOptions');
|
expect(actual.autocompleteOptions).toEqual('autocompleteOptions');
|
||||||
});
|
});
|
||||||
it('should instantiate algoliasearch with the correct values', () => {
|
it('should instantiate algoliasearch with the correct values', () => {
|
||||||
|
|
@ -294,8 +325,7 @@ describe('DocSearch', () => {
|
||||||
docsearch = new DocSearch({
|
docsearch = new DocSearch({
|
||||||
indexName: 'indexName',
|
indexName: 'indexName',
|
||||||
apiKey: 'apiKey',
|
apiKey: 'apiKey',
|
||||||
inputSelector: '#input',
|
inputSelector: '#input'
|
||||||
algoliaOptions: 'algoliaOptions'
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -327,7 +357,7 @@ describe('DocSearch', () => {
|
||||||
let expectedArguments = {
|
let expectedArguments = {
|
||||||
indexName: 'indexName',
|
indexName: 'indexName',
|
||||||
query: 'query',
|
query: 'query',
|
||||||
params: 'algoliaOptions'
|
params: {hitsPerPage: 5}
|
||||||
};
|
};
|
||||||
expect(client.search.calledWith([expectedArguments])).toBe(true);
|
expect(client.search.calledWith([expectedArguments])).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue