1
0
Fork 0

Merge branch 'develop'

This commit is contained in:
Maxime 2017-08-08 11:42:44 -07:00
commit f8cada1429
3 changed files with 46 additions and 2 deletions

View file

@ -203,6 +203,21 @@ var search = docsearch({
});
```
#### queryHook
If you want modify the query before it is send to Algolia you can pass the `queryHook` option.
```javascript
var search = docsearch({
apiKey: '<API_KEY>',
indexName: '<INDEX_NAME>',
inputSelector: '<YOUR_INPUT_DOM_SELECTOR>',
queryHook: function (query) {
return query + "_modified";
}
});
```
#### transformData
If you want to modify the hits before displaying them you can make use of the

View file

@ -43,6 +43,7 @@ class DocSearch {
autoselect: true,
},
transformData = false,
queryHook = false,
handleSelected = false,
enhancedSearchInput = false,
layout = 'collumns',
@ -56,6 +57,7 @@ class DocSearch {
algoliaOptions,
autocompleteOptions,
transformData,
queryHook,
handleSelected,
enhancedSearchInput,
layout,
@ -89,7 +91,7 @@ class DocSearch {
this.autocomplete = autocomplete(this.input, autocompleteOptions, [
{
source: this.getAutocompleteSource(transformData),
source: this.getAutocompleteSource(transformData, queryHook),
templates: {
suggestion: DocSearch.getSuggestionTemplate(this.isSimpleLayout),
footer: templates.footer,
@ -170,11 +172,16 @@ class DocSearch {
* the Algolia index and call the callbacks with the formatted hits.
* @function getAutocompleteSource
* @param {function} transformData An optional function to transform the hits
* @param {function} queryHook An optional function to transform the query
* @returns {function} Method to be passed as the `source` option of
* autocomplete
*/
getAutocompleteSource(transformData) {
getAutocompleteSource(transformData, queryHook) {
return (query, callback) => {
if (queryHook) {
query = queryHook(query) || query;
}
this.client
.search([
{

View file

@ -367,6 +367,28 @@ describe('DocSearch', () => {
expect(client.search.calledWith([expectedArguments])).toBe(true);
});
});
describe('when queryHook is used', () => {
it('calls the Agolia client with the correct parameters', () => {
// Given
const actual = docsearch.getAutocompleteSource(false, function(query) {
return query + ' modified';
});
// When
actual('query');
// Then
expect(client.search.calledOnce).toBe(true);
// expect(resolvedQuery.calledOnce).toBe(true);
const expectedArguments = {
indexName: 'indexName',
query: 'query modified',
params: { hitsPerPage: 5 },
};
expect(client.search.calledWith([expectedArguments])).toBe(true);
});
});
});
describe('handleSelected', () => {