This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(answers): add
findAnswers
(#804)
* feat(answers): add searchForAnswers WIP * update parameters * add a FIXME comment * accept options as an object * add jsdoc and typings * add test cases * fix lint error * better types * clean up error message * Update src/algoliasearch.helper.js Co-authored-by: Haroen Viaene <[email protected]> * remove jsdoc * fix type Co-authored-by: Haroen Viaene <[email protected]>
- Loading branch information
Showing
5 changed files
with
287 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
'use strict'; | ||
|
||
var algoliasearchHelper = require('../../../index'); | ||
|
||
function makeFakeFindAnswersResponse() { | ||
return { | ||
exhaustiveFacetsCount: true, | ||
facetHits: [], | ||
processingTimeMS: 3 | ||
}; | ||
} | ||
|
||
function setupTestEnvironment(helperOptions) { | ||
var findAnswers = jest.fn(function() { | ||
return Promise.resolve([makeFakeFindAnswersResponse()]); | ||
}); | ||
|
||
var fakeClient = { | ||
initIndex: function() { | ||
return { | ||
findAnswers: findAnswers | ||
}; | ||
} | ||
}; | ||
|
||
var helper = algoliasearchHelper(fakeClient, 'index', helperOptions); | ||
|
||
return { | ||
findAnswers: findAnswers, | ||
helper: helper | ||
}; | ||
} | ||
|
||
test('returns an empty array with no derived helper', function() { | ||
var env = setupTestEnvironment(); | ||
var helper = env.helper; | ||
var findAnswers = env.findAnswers; | ||
|
||
return helper | ||
.findAnswers({ | ||
attributesForPrediction: ['description'], | ||
queryLanguages: ['en'], | ||
nbHits: 1 | ||
}) | ||
.then(function(result) { | ||
expect(findAnswers).toHaveBeenCalledTimes(0); | ||
expect(result).toEqual([]); | ||
}); | ||
}); | ||
|
||
test('returns a correct result with one derivation', function() { | ||
var env = setupTestEnvironment(); | ||
var helper = env.helper; | ||
var findAnswers = env.findAnswers; | ||
|
||
helper.derive(function(state) { | ||
return state; | ||
}); | ||
|
||
return helper | ||
.findAnswers({ | ||
attributesForPrediction: ['description'], | ||
queryLanguages: ['en'], | ||
nbHits: 1 | ||
}) | ||
.then(function(result) { | ||
expect(findAnswers).toHaveBeenCalledTimes(1); | ||
expect(result).toEqual([makeFakeFindAnswersResponse()]); | ||
}); | ||
}); | ||
|
||
test('runs findAnswers with facets', function() { | ||
var env = setupTestEnvironment({facets: ['facet1']}); | ||
var helper = env.helper; | ||
var findAnswers = env.findAnswers; | ||
helper.addFacetRefinement('facet1', 'facetValue'); | ||
|
||
helper.derive(function(state) { | ||
return state; | ||
}); | ||
|
||
helper.setQuery('hello'); | ||
|
||
return helper | ||
.findAnswers({ | ||
attributesForPrediction: ['description'], | ||
queryLanguages: ['en'], | ||
nbHits: 1 | ||
}) | ||
.then(function() { | ||
expect(findAnswers).toHaveBeenCalledTimes(1); | ||
expect(findAnswers).toHaveBeenCalledWith('hello', ['en'], { | ||
attributesForPrediction: ['description'], | ||
nbHits: 1, | ||
params: { | ||
facetFilters: ['facet1:facetValue'], | ||
facets: ['facet1'], | ||
query: 'hello', | ||
tagFilters: '' | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.