From e453dab1792cbd18852706c53f6006d7665a37e8 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Wed, 31 Jul 2019 15:58:45 +0200 Subject: [PATCH] feat(autocomplete): add queryID & position to provided hits (#2687) * 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 --- .../__tests__/connectAutoComplete.js | 123 +++++++++++++++--- .../src/connectors/connectAutoComplete.js | 16 ++- 2 files changed, 122 insertions(+), 17 deletions(-) diff --git a/packages/react-instantsearch-core/src/connectors/__tests__/connectAutoComplete.js b/packages/react-instantsearch-core/src/connectors/__tests__/connectAutoComplete.js index 56c84a72d1..94963ed58e 100644 --- a/packages/react-instantsearch-core/src/connectors/__tests__/connectAutoComplete.js +++ b/packages/react-instantsearch-core/src/connectors/__tests__/connectAutoComplete.js @@ -13,11 +13,11 @@ describe('connectAutoComplete', () => { { contextValue }, {}, { - results: { hits }, + results: { hits, page: 0, hitsPerPage: 20 }, } ); expect(props).toEqual({ - hits, + hits: [{ __position: 1 }], currentRefinement: '', }); @@ -25,11 +25,11 @@ describe('connectAutoComplete', () => { { contextValue }, { query: 'query' }, { - results: { hits }, + results: { hits, page: 0, hitsPerPage: 20 }, } ); expect(props).toEqual({ - hits, + hits: [{ __position: 1 }], currentRefinement: 'query', }); @@ -37,14 +37,55 @@ describe('connectAutoComplete', () => { { 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(), @@ -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: '', }); @@ -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', }); @@ -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', }); diff --git a/packages/react-instantsearch-core/src/connectors/connectAutoComplete.js b/packages/react-instantsearch-core/src/connectors/connectAutoComplete.js index f9d044626c..38f5c7eb31 100644 --- a/packages/react-instantsearch-core/src/connectors/connectAutoComplete.js +++ b/packages/react-instantsearch-core/src/connectors/connectAutoComplete.js @@ -4,6 +4,7 @@ import { refineValue, getCurrentRefinementValue, } from '../core/indexUtils'; +import { addQueryID, addAbsolutePositions } from '../core/utils'; const getId = () => 'query'; @@ -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 + ), }, ], []