Skip to content

Commit

Permalink
fix(core): support Insights in requesters (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored May 3, 2021
1 parent a16bea2 commit c305ab4
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"files": [
{
"path": "packages/autocomplete-core/dist/umd/index.production.js",
"maxSize": "5 kB"
"maxSize": "5.25 kB"
},
{
"path": "packages/autocomplete-js/dist/umd/index.production.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,138 @@ describe('mapToAlgoliaResponse', () => {
expect(facetHits).toEqual([]);
});

test('returns formatted results', () => {
const { results } = mapToAlgoliaResponse([
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '1',
label: 'Label 1',
},
{
objectID: '2',
label: 'Label 2',
},
],
}),
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '3',
label: 'Label 3',
},
{
objectID: '4',
label: 'Label 4',
},
],
}),
]);

expect(results).toEqual([
expect.objectContaining({
hits: [
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 1',
objectID: '1',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 2',
objectID: '2',
},
],
}),
expect.objectContaining({
hits: [
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 3',
objectID: '3',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 4',
objectID: '4',
},
],
}),
]);
});

test('returns formatted hits', () => {
const { hits } = mapToAlgoliaResponse([
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '1',
label: 'Label 1',
},
{
objectID: '2',
label: 'Label 2',
},
],
}),
createSingleSearchResponse({
index: 'indexName',
queryID: 'queryID',
hits: [
{
objectID: '3',
label: 'Label 3',
},
{
objectID: '4',
label: 'Label 4',
},
],
}),
]);

expect(hits).toEqual([
[
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 1',
objectID: '1',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 2',
objectID: '2',
},
],
[
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 3',
objectID: '3',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 4',
objectID: '4',
},
],
]);
});

test('returns formatted facet hits', () => {
const { facetHits } = mapToAlgoliaResponse([
createSFFVResponse({
Expand Down
19 changes: 18 additions & 1 deletion packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ import type {
} from '@algolia/client-search';

export function mapToAlgoliaResponse<THit>(
results: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
rawResults: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
) {
const results: Array<
SearchResponse<THit> | SearchForFacetValuesResponse
> = rawResults.map((result) => {
return {
...result,
hits: (result as SearchResponse<THit>).hits?.map((hit) => {
// Bring support for the Insights plugin.
return {
...hit,
__autocomplete_indexName: (result as SearchResponse<THit>).index,
__autocomplete_queryID: (result as SearchResponse<THit>).queryID,
};
}),
};
});

return {
results,
hits: results
Expand All @@ -14,6 +30,7 @@ export function mapToAlgoliaResponse<THit>(
facetHits: results
.map((result) =>
(result as SearchForFacetValuesResponse).facetHits?.map((facetHit) => {
// Bring support for the highlighting components.
return {
label: facetHit.value,
count: facetHit.count,
Expand Down

0 comments on commit c305ab4

Please sign in to comment.