diff --git a/packages/react-instantsearch/src/connectors/connectHierarchicalMenu.test.js b/packages/react-instantsearch/src/connectors/connectHierarchicalMenu.test.js index ee52c945ce..b3b0079d79 100644 --- a/packages/react-instantsearch/src/connectors/connectHierarchicalMenu.test.js +++ b/packages/react-instantsearch/src/connectors/connectHierarchicalMenu.test.js @@ -22,6 +22,7 @@ describe('connectHierarchicalMenu', () => { getFacetValues: jest.fn(), getFacetByName: () => true, hits: [], + index: 'index', }; results.getFacetValues.mockImplementationOnce(() => ({})); @@ -334,6 +335,7 @@ describe('connectHierarchicalMenu', () => { first: { getFacetValues: jest.fn(), getFacetByName: () => true, + index: 'first', }, }; diff --git a/packages/react-instantsearch/src/connectors/connectHits.test.js b/packages/react-instantsearch/src/connectors/connectHits.test.js index b802b01c5f..a389f3d0e7 100644 --- a/packages/react-instantsearch/src/connectors/connectHits.test.js +++ b/packages/react-instantsearch/src/connectors/connectHits.test.js @@ -12,7 +12,7 @@ describe('connectHits', () => { it('provides the current hits to the component', () => { const hits = [{}]; const props = getProvidedProps(null, null, { - results: { hits }, + results: { hits, index: 'index' }, }); expect(props).toEqual({ hits }); }); @@ -38,7 +38,7 @@ describe('connectHits', () => { it('provides the current hits to the component', () => { const hits = [{}]; const props = getProvidedProps(null, null, { - results: { second: { hits } }, + results: { second: { hits, index: 'second' } }, }); expect(props).toEqual({ hits }); }); diff --git a/packages/react-instantsearch/src/connectors/connectInfiniteHits.test.js b/packages/react-instantsearch/src/connectors/connectInfiniteHits.test.js index 59e3306999..2e84ee83ef 100644 --- a/packages/react-instantsearch/src/connectors/connectInfiniteHits.test.js +++ b/packages/react-instantsearch/src/connectors/connectInfiniteHits.test.js @@ -11,7 +11,7 @@ describe('connectInfiniteHits', () => { it('provides the current hits to the component', () => { const hits = [{}]; const props = getProvidedProps(null, null, { - results: { hits, page: 0, hitsPerPage: 2, nbPages: 3 }, + results: { hits, page: 0, hitsPerPage: 2, nbPages: 3, index: 'index' }, }); expect(props).toEqual({ hits, hasMore: true }); }); @@ -20,7 +20,7 @@ describe('connectInfiniteHits', () => { const hits = [{}, {}]; const hits2 = [{}, {}]; const res1 = getProvidedProps(null, null, { - results: { hits, page: 0, hitsPerPage: 2, nbPages: 3 }, + results: { hits, page: 0, hitsPerPage: 2, nbPages: 3, index: 'index' }, }); expect(res1.hits).toEqual(hits); expect(res1.hasMore).toBe(true); @@ -30,6 +30,7 @@ describe('connectInfiniteHits', () => { page: 1, hitsPerPage: 2, nbPages: 3, + index: 'index', }, }); expect(res2.hits).toEqual([...hits, ...hits2]); @@ -41,7 +42,7 @@ describe('connectInfiniteHits', () => { const hits2 = [{}, {}, {}, {}, {}, {}]; const hits3 = [{}, {}, {}, {}, {}, {}, {}, {}]; const res1 = getProvidedProps(null, null, { - results: { hits, page: 0, hitsPerPage: 6, nbPages: 10 }, + results: { hits, page: 0, hitsPerPage: 6, nbPages: 10, index: 'index' }, }); expect(res1.hits).toEqual(hits); expect(res1.hasMore).toBe(true); @@ -51,6 +52,7 @@ describe('connectInfiniteHits', () => { page: 1, hitsPerPage: 6, nbPages: 10, + index: 'index', }, }); expect(res2.hits).toEqual([...hits, ...hits2]); @@ -61,6 +63,7 @@ describe('connectInfiniteHits', () => { page: 2, hitsPerPage: 8, nbPages: 10, + index: 'index', }, }); expect(res3.hits).toEqual([...hits, ...hits2, ...hits3]); @@ -71,6 +74,7 @@ describe('connectInfiniteHits', () => { page: 2, hitsPerPage: 8, nbPages: 10, + index: 'index', }, }); //re-render with the same property expect(res3.hits).toEqual([...hits, ...hits2, ...hits3]); @@ -90,6 +94,7 @@ describe('connectInfiniteHits', () => { page, hitsPerPage: hits.length, nbPages, + index: 'index', }, }); expect(res.hits).toEqual(allHits); @@ -105,6 +110,7 @@ describe('connectInfiniteHits', () => { page: nbPages - 1, hitsPerPage: hits.length, nbPages, + index: 'index', }, }); expect(res.hits.length).toEqual(nbPages * 2); @@ -117,7 +123,7 @@ describe('connectInfiniteHits', () => { const hits2 = [{}, {}]; const hits3 = [{}]; getProvidedProps(null, null, { - results: { hits, page: 0, hitsPerPage: 2, nbPages: 3 }, + results: { hits, page: 0, hitsPerPage: 2, nbPages: 3, index: 'index' }, }); getProvidedProps(null, null, { results: { @@ -125,6 +131,7 @@ describe('connectInfiniteHits', () => { page: 1, hitsPerPage: 2, nbPages: 3, + index: 'index', }, }); const props = getProvidedProps(null, null, { @@ -133,6 +140,7 @@ describe('connectInfiniteHits', () => { page: 2, hitsPerPage: 2, nbPages: 3, + index: 'index', }, }); expect(props.hits).toEqual([...hits, ...hits2, ...hits3]); @@ -171,7 +179,15 @@ describe('connectInfiniteHits', () => { it('provides the current hits to the component', () => { const hits = [{}]; const props = getProvidedProps(null, null, { - results: { second: { hits, page: 0, hitsPerPage: 2, nbPages: 3 } }, + results: { + second: { + hits, + page: 0, + hitsPerPage: 2, + nbPages: 3, + index: 'second', + }, + }, }); expect(props).toEqual({ hits, hasMore: true }); }); @@ -180,13 +196,27 @@ describe('connectInfiniteHits', () => { const hits = [{}, {}]; const hits2 = [{}, {}]; const res1 = getProvidedProps(null, null, { - results: { second: { hits, page: 0, hitsPerPage: 2, nbPages: 3 } }, + results: { + second: { + hits, + page: 0, + hitsPerPage: 2, + nbPages: 3, + index: 'second', + }, + }, }); expect(res1.hits).toEqual(hits); expect(res1.hasMore).toBe(true); const res2 = getProvidedProps(null, null, { results: { - second: { hits: hits2, page: 1, hitsPerPage: 2, nbPages: 3 }, + second: { + hits: hits2, + page: 1, + hitsPerPage: 2, + nbPages: 3, + index: 'second', + }, }, }); expect(res2.hits).toEqual([...hits, ...hits2]); @@ -198,27 +228,53 @@ describe('connectInfiniteHits', () => { const hits2 = [{}, {}, {}, {}, {}, {}]; const hits3 = [{}, {}, {}, {}, {}, {}, {}, {}]; const res1 = getProvidedProps(null, null, { - results: { second: { hits, page: 0, hitsPerPage: 6, nbPages: 10 } }, + results: { + second: { + hits, + page: 0, + hitsPerPage: 6, + nbPages: 10, + index: 'second', + }, + }, }); expect(res1.hits).toEqual(hits); expect(res1.hasMore).toBe(true); const res2 = getProvidedProps(null, null, { results: { - second: { hits: hits2, page: 1, hitsPerPage: 6, nbPages: 10 }, + second: { + hits: hits2, + page: 1, + hitsPerPage: 6, + nbPages: 10, + index: 'second', + }, }, }); expect(res2.hits).toEqual([...hits, ...hits2]); expect(res2.hasMore).toBe(true); let res3 = getProvidedProps(null, null, { results: { - second: { hits: hits3, page: 2, hitsPerPage: 8, nbPages: 10 }, + second: { + hits: hits3, + page: 2, + hitsPerPage: 8, + nbPages: 10, + index: 'second', + }, }, }); expect(res3.hits).toEqual([...hits, ...hits2, ...hits3]); expect(res3.hasMore).toBe(true); res3 = getProvidedProps(null, null, { results: { - second: { hits: hits3, page: 2, hitsPerPage: 8, nbPages: 10 }, + second: { + hits: hits3, + page: 2, + hitsPerPage: 8, + nbPages: 10, + index: 'second', + }, }, }); //re-render with the same property expect(res3.hits).toEqual([...hits, ...hits2, ...hits3]); @@ -239,6 +295,7 @@ describe('connectInfiniteHits', () => { page, hitsPerPage: hits.length, nbPages, + index: 'second', }, }, }); @@ -256,6 +313,7 @@ describe('connectInfiniteHits', () => { page: nbPages - 1, hitsPerPage: hits.length, nbPages, + index: 'second', }, }, }); @@ -269,16 +327,36 @@ describe('connectInfiniteHits', () => { const hits2 = [{}, {}]; const hits3 = [{}]; getProvidedProps(null, null, { - results: { second: { hits, page: 0, hitsPerPage: 2, nbPages: 3 } }, + results: { + second: { + hits, + page: 0, + hitsPerPage: 2, + nbPages: 3, + index: 'second', + }, + }, }); getProvidedProps(null, null, { results: { - second: { hits: hits2, page: 1, hitsPerPage: 2, nbPages: 3 }, + second: { + hits: hits2, + page: 1, + hitsPerPage: 2, + nbPages: 3, + index: 'second', + }, }, }); const props = getProvidedProps(null, null, { results: { - second: { hits: hits3, page: 2, hitsPerPage: 2, nbPages: 3 }, + second: { + hits: hits3, + page: 2, + hitsPerPage: 2, + nbPages: 3, + index: 'second', + }, }, }); expect(props.hits).toEqual([...hits, ...hits2, ...hits3]); diff --git a/packages/react-instantsearch/src/connectors/connectMenu.test.js b/packages/react-instantsearch/src/connectors/connectMenu.test.js index dcc2fef755..8d9c18ec5a 100644 --- a/packages/react-instantsearch/src/connectors/connectMenu.test.js +++ b/packages/react-instantsearch/src/connectors/connectMenu.test.js @@ -25,6 +25,7 @@ describe('connectMenu', () => { getFacetValues: jest.fn(() => []), getFacetByName: () => true, hits: [], + index: 'index', }; props = getProvidedProps({ attributeName: 'ok' }, {}, {}); @@ -212,6 +213,7 @@ describe('connectMenu', () => { getFacetValues: jest.fn(() => []), getFacetByName: () => true, hits: [], + index: 'index', }; results.getFacetValues.mockImplementation(() => [ { @@ -386,6 +388,7 @@ describe('connectMenu', () => { getFacetValues: jest.fn(() => []), getFacetByName: () => true, hits: [], + index: 'index', }; results.getFacetValues.mockClear(); results.getFacetValues.mockImplementation(() => [ @@ -462,6 +465,7 @@ describe('connectMenu', () => { first: { getFacetValues: jest.fn(() => []), getFacetByName: () => true, + index: 'first', }, }; @@ -650,6 +654,7 @@ describe('connectMenu', () => { first: { getFacetValues: jest.fn(() => []), getFacetByName: () => true, + index: 'first', }, }; results.first.getFacetValues.mockImplementation(() => [ diff --git a/packages/react-instantsearch/src/connectors/connectMultiRange.test.js b/packages/react-instantsearch/src/connectors/connectMultiRange.test.js index 9a1a7fdd51..be4a2e9ea0 100644 --- a/packages/react-instantsearch/src/connectors/connectMultiRange.test.js +++ b/packages/react-instantsearch/src/connectors/connectMultiRange.test.js @@ -21,6 +21,7 @@ describe('connectMultiRange', () => { getFacetStats: () => ({ min: 0, max: 300 }), getFacetByName: () => true, hits: [], + index: 'index', }; it('provides the correct props to the component', () => { @@ -372,6 +373,7 @@ describe('connectMultiRange', () => { first: { getFacetStats: () => ({ min: 0, max: 300 }), getFacetByName: () => true, + index: 'first', }, }; diff --git a/packages/react-instantsearch/src/connectors/connectPagination.test.js b/packages/react-instantsearch/src/connectors/connectPagination.test.js index 737d4fdb56..62377587de 100644 --- a/packages/react-instantsearch/src/connectors/connectPagination.test.js +++ b/packages/react-instantsearch/src/connectors/connectPagination.test.js @@ -18,29 +18,22 @@ describe('connectPagination', () => { const cleanUp = connect.cleanUp.bind(context); it('provides the correct props to the component', () => { - props = getProvidedProps({}, {}, { results: { nbPages: 666, hits: [] } }); + const results = { nbPages: 666, hits: [], index: 'index' }; + props = getProvidedProps({}, {}, { results }); expect(props).toEqual({ currentRefinement: 1, nbPages: 666, canRefine: true, }); - props = getProvidedProps( - {}, - { page: 5 }, - { results: { nbPages: 666, hits: [] } } - ); + props = getProvidedProps({}, { page: 5 }, { results }); expect(props).toEqual({ currentRefinement: 5, nbPages: 666, canRefine: true, }); - props = getProvidedProps( - {}, - { page: '5' }, - { results: { nbPages: 666, hits: [] } } - ); + props = getProvidedProps({}, { page: '5' }, { results }); expect(props).toEqual({ currentRefinement: 5, nbPages: 666, @@ -50,7 +43,7 @@ describe('connectPagination', () => { props = getProvidedProps( {}, { page: '1' }, - { results: { nbPages: 1, hits: [] } } + { results: { nbPages: 1, hits: [], index: 'index' } } ); expect(props).toEqual({ currentRefinement: 1, @@ -106,11 +99,8 @@ describe('connectPagination', () => { const cleanUp = connect.cleanUp.bind(context); it('provides the correct props to the component', () => { - props = getProvidedProps( - {}, - {}, - { results: { first: { nbPages: 666 } } } - ); + const results = { first: { nbPages: 666, index: 'first' } }; + props = getProvidedProps({}, {}, { results }); expect(props).toEqual({ currentRefinement: 1, nbPages: 666, @@ -120,7 +110,7 @@ describe('connectPagination', () => { props = getProvidedProps( {}, { indices: { first: { page: 5 } } }, - { results: { first: { nbPages: 666 } } } + { results } ); expect(props).toEqual({ currentRefinement: 5, diff --git a/packages/react-instantsearch/src/connectors/connectRange.test.js b/packages/react-instantsearch/src/connectors/connectRange.test.js index 02453fa06e..816e076489 100644 --- a/packages/react-instantsearch/src/connectors/connectRange.test.js +++ b/packages/react-instantsearch/src/connectors/connectRange.test.js @@ -39,6 +39,7 @@ describe('connectRange', () => { ], getFacetByName: () => true, hits: [], + index: 'index', }; props = getProvidedProps({ attributeName: 'ok' }, {}, { results }); expect(props).toEqual({ @@ -264,6 +265,7 @@ describe('connectRange', () => { { name: '2', count: 20 }, ], getFacetByName: () => true, + index: 'first', }, }; props = getProvidedProps({ attributeName: 'ok' }, {}, { results }); diff --git a/packages/react-instantsearch/src/connectors/connectRefinementList.test.js b/packages/react-instantsearch/src/connectors/connectRefinementList.test.js index 083b61f60b..a7a9e6e865 100644 --- a/packages/react-instantsearch/src/connectors/connectRefinementList.test.js +++ b/packages/react-instantsearch/src/connectors/connectRefinementList.test.js @@ -24,6 +24,7 @@ describe('connectRefinementList', () => { getFacetValues: jest.fn(() => []), getFacetByName: () => true, hits: [], + index: 'index', }; it('provides the correct props to the component', () => { diff --git a/packages/react-instantsearch/src/connectors/connectStats.test.js b/packages/react-instantsearch/src/connectors/connectStats.test.js index 182aba20c1..451f6d4e6b 100644 --- a/packages/react-instantsearch/src/connectors/connectStats.test.js +++ b/packages/react-instantsearch/src/connectors/connectStats.test.js @@ -13,7 +13,7 @@ describe('connectStats', () => { expect(props).toBe(null); props = getProvidedProps(null, null, { - results: { nbHits: 666, processingTimeMS: 1, hits: [] }, + results: { nbHits: 666, processingTimeMS: 1, hits: [], index: 'index' }, }); expect(props).toEqual({ nbHits: 666, processingTimeMS: 1 }); }); @@ -31,7 +31,9 @@ describe('connectStats', () => { expect(props).toBe(null); props = getProvidedProps(null, null, { - results: { second: { nbHits: 666, processingTimeMS: 1 } }, + results: { + second: { nbHits: 666, processingTimeMS: 1, index: 'second' }, + }, }); expect(props).toEqual({ nbHits: 666, processingTimeMS: 1 }); }); diff --git a/packages/react-instantsearch/src/core/indexUtils.js b/packages/react-instantsearch/src/core/indexUtils.js index beab0ed0be..766cd03537 100644 --- a/packages/react-instantsearch/src/core/indexUtils.js +++ b/packages/react-instantsearch/src/core/indexUtils.js @@ -7,12 +7,16 @@ export function getIndex(context) { } export function getResults(searchResults, context) { + const index = getIndex(context); if (searchResults.results && !searchResults.results.hits) { - return searchResults.results[getIndex(context)] + const results = searchResults.results[getIndex(context)]; + return results && index === results.index ? searchResults.results[getIndex(context)] : null; } else { - return searchResults.results ? searchResults.results : null; + return searchResults.results && searchResults.results.index === index + ? searchResults.results + : null; } } diff --git a/packages/react-instantsearch/src/core/indexUtils.test.js b/packages/react-instantsearch/src/core/indexUtils.test.js index 58caed4246..2fc28c13d3 100644 --- a/packages/react-instantsearch/src/core/indexUtils.test.js +++ b/packages/react-instantsearch/src/core/indexUtils.test.js @@ -230,11 +230,20 @@ describe('utility method for manipulating the search state', () => { }); }); it('get results', () => { - const searchResults = { results: { hits: ['some'] } }; + const searchResults = { results: { hits: ['some'], index: 'index' } }; const results = getResults(searchResults, context); - expect(results).toEqual({ hits: ['some'] }); + expect(results.hits).toEqual(['some']); + }); + it('get no results if index name is different than context', () => { + const searchResults = { + results: { hits: ['some'], index: 'otherIndex' }, + }; + + const results = getResults(searchResults, context); + + expect(results).toBeNull(); }); }); describe('when there are multiple index', () => { @@ -491,19 +500,23 @@ describe('utility method for manipulating the search state', () => { }); it('get results', () => { - let searchResults = { results: { first: { some: 'results' } } }; + let searchResults = { + results: { first: { hits: 'results', index: 'first' } }, + }; - let results = getResults(searchResults, context); + let results = getResults(searchResults, { + ais: { mainTargetedIndex: 'first' }, + }); - expect(results).toEqual({ some: 'results' }); + expect(results.hits).toEqual('results'); - searchResults = { results: { first: { some: 'results' } } }; + searchResults = { results: { second: { some: 'results' } } }; results = getResults(searchResults, { ais: { mainTargetedIndex: 'first' }, }); - expect(results).toEqual({ some: 'results' }); + expect(results).toBeNull(); }); }); });