Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(autocomplete): add queryID & position to provided hits (#2687)
Browse files Browse the repository at this point in the history
* feat(autocomplete): add queryID to provided hits if defined

This was accidentally forgotten when we added the queryID to hits & infinite hits.

IFW-902

* feat(autocomplete): add absolute position too

* Update packages/react-instantsearch-core/src/connectors/__tests__/connectAutoComplete.js
  • Loading branch information
Haroenv committed Jul 31, 2019
1 parent 51de682 commit e453dab
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 17 deletions.
Expand Up @@ -13,38 +13,79 @@ describe('connectAutoComplete', () => {
{ contextValue },
{},
{
results: { hits },
results: { hits, page: 0, hitsPerPage: 20 },
}
);
expect(props).toEqual({
hits,
hits: [{ __position: 1 }],
currentRefinement: '',
});

props = connect.getProvidedProps(
{ contextValue },
{ query: 'query' },
{
results: { hits },
results: { hits, page: 0, hitsPerPage: 20 },
}
);
expect(props).toEqual({
hits,
hits: [{ __position: 1 }],
currentRefinement: 'query',
});

props = connect.getProvidedProps(
{ defaultRefinement: 'query', contextValue },
{},
{
results: { hits },
results: { hits, page: 0, hitsPerPage: 20 },
}
);
expect(props).toEqual({
hits,
hits: [{ __position: 1 }],
currentRefinement: 'query',
});
});

it('provides current hits to the component with queryID & position', () => {
const hits = [{}];
const hitsWithExtraInfo = [{ __queryID: 'zombo.com', __position: 1 }];
let props = connect.getProvidedProps(
{ contextValue },
{},
{
results: { hits, page: 0, hitsPerPage: 20, queryID: 'zombo.com' },
}
);
expect(props).toEqual({
hits: hitsWithExtraInfo,
currentRefinement: '',
});

props = connect.getProvidedProps(
{ contextValue },
{ query: 'query' },
{
results: { hits, page: 0, hitsPerPage: 20, queryID: 'zombo.com' },
}
);
expect(props).toEqual({
hits: hitsWithExtraInfo,
currentRefinement: 'query',
});

props = connect.getProvidedProps(
{ defaultRefinement: 'query', contextValue },
{},
{
results: { hits, page: 0, hitsPerPage: 20, queryID: 'zombo.com' },
}
);
expect(props).toEqual({
hits: hitsWithExtraInfo,
currentRefinement: 'query',
});
});

it('refines the query parameter', () => {
const params = connect.getSearchParameters(
new SearchParameters(),
Expand Down Expand Up @@ -86,17 +127,43 @@ describe('connectAutoComplete', () => {
it('provides current hits to the component', () => {
const firstHits = [{}];
const secondHits = [{}];
const firstHitsWithExtraInfo = [
{ __queryID: 'zombo.com', __position: 1 },
];
const secondHitsWithExtraInfo = [
{ __queryID: 'html5zombo.com', __position: 1 },
];
let props = connect.getProvidedProps(
{ contextValue, indexContextValue },
{},
{
results: { first: { hits: firstHits }, second: { hits: secondHits } },
results: {
first: {
hits: firstHits,
page: 0,
hitsPerPage: 20,
queryID: 'zombo.com',
},
second: {
hits: secondHits,
page: 0,
hitsPerPage: 20,
queryID: 'html5zombo.com',
},
},
}
);
expect(props).toEqual({
hits: [
{ hits: firstHits, index: 'first' },
{ hits: secondHits, index: 'second' },
{
hits: firstHitsWithExtraInfo,
index: 'first',
},
{
hits: secondHitsWithExtraInfo,

index: 'second',
},
],
currentRefinement: '',
});
Expand All @@ -105,13 +172,26 @@ describe('connectAutoComplete', () => {
{ contextValue, indexContextValue },
{ indices: { second: { query: 'query' } } },
{
results: { first: { hits: firstHits }, second: { hits: secondHits } },
results: {
first: {
hits: firstHits,
page: 0,
hitsPerPage: 20,
queryID: 'zombo.com',
},
second: {
hits: secondHits,
page: 0,
hitsPerPage: 20,
queryID: 'html5zombo.com',
},
},
}
);
expect(props).toEqual({
hits: [
{ hits: firstHits, index: 'first' },
{ hits: secondHits, index: 'second' },
{ hits: firstHitsWithExtraInfo, index: 'first' },
{ hits: secondHitsWithExtraInfo, index: 'second' },
],
currentRefinement: 'query',
});
Expand All @@ -120,13 +200,26 @@ describe('connectAutoComplete', () => {
{ defaultRefinement: 'query', contextValue, indexContextValue },
{},
{
results: { first: { hits: firstHits }, second: { hits: secondHits } },
results: {
first: {
hits: firstHits,
page: 0,
hitsPerPage: 20,
queryID: 'zombo.com',
},
second: {
hits: secondHits,
page: 0,
hitsPerPage: 20,
queryID: 'html5zombo.com',
},
},
}
);
expect(props).toEqual({
hits: [
{ hits: firstHits, index: 'first' },
{ hits: secondHits, index: 'second' },
{ hits: firstHitsWithExtraInfo, index: 'first' },
{ hits: secondHitsWithExtraInfo, index: 'second' },
],
currentRefinement: 'query',
});
Expand Down
Expand Up @@ -4,6 +4,7 @@ import {
refineValue,
getCurrentRefinementValue,
} from '../core/indexUtils';
import { addQueryID, addAbsolutePositions } from '../core/utils';

const getId = () => 'query';

Expand All @@ -29,14 +30,25 @@ function getHits(searchResults) {
searchResults.results.hits &&
Array.isArray(searchResults.results.hits)
) {
return searchResults.results.hits;
return addAbsolutePositions(
addQueryID(searchResults.results.hits, searchResults.results.queryID),
searchResults.results.hitsPerPage,
searchResults.results.page
);
} else {
return Object.keys(searchResults.results).reduce(
(hits, index) => [
...hits,
{
index,
hits: searchResults.results[index].hits,
hits: addAbsolutePositions(
addQueryID(
searchResults.results[index].hits,
searchResults.results[index].queryID
),
searchResults.results[index].hitsPerPage,
searchResults.results[index].page
),
},
],
[]
Expand Down

0 comments on commit e453dab

Please sign in to comment.