diff --git a/packages/react-instantsearch/src/connectors/connectRange.js b/packages/react-instantsearch/src/connectors/connectRange.js index 46755435c8..8f52cc4a6d 100644 --- a/packages/react-instantsearch/src/connectors/connectRange.js +++ b/packages/react-instantsearch/src/connectors/connectRange.js @@ -190,13 +190,14 @@ export default createConnector({ getProvidedProps(props, searchState, searchResults) { const { attributeName, precision, min: minBound, max: maxBound } = props; const results = getResults(searchResults, this.context); - const stats = results ? results.getFacetStats(attributeName) || {} : {}; - const count = results - ? results.getFacetValues(attributeName).map(v => ({ - value: v.name, - count: v.count, - })) - : []; + const hasFacet = results && results.getFacetByName(attributeName); + const stats = hasFacet ? results.getFacetStats(attributeName) || {} : {}; + const facetValues = hasFacet ? results.getFacetValues(attributeName) : []; + + const count = facetValues.map(v => ({ + value: v.name, + count: v.count, + })); const { min: rangeMin, max: rangeMax } = getCurrentRange( { min: minBound, max: maxBound }, diff --git a/packages/react-instantsearch/src/connectors/connectRange.test.js b/packages/react-instantsearch/src/connectors/connectRange.test.js index 8fc3067505..0d0609d0c4 100644 --- a/packages/react-instantsearch/src/connectors/connectRange.test.js +++ b/packages/react-instantsearch/src/connectors/connectRange.test.js @@ -1,8 +1,6 @@ -/* eslint-env jest, jasmine */ - -import { SearchParameters } from 'algoliasearch-helper'; - +import { SearchParameters, SearchResults } from 'algoliasearch-helper'; import connect from './connectRange'; + jest.mock('../core/createConnector'); let props; @@ -214,6 +212,7 @@ describe('connectRange', () => { }); results = { + getFacetByName: () => false, getFacetStats: () => {}, getFacetValues: () => [], hits: [], @@ -317,6 +316,28 @@ describe('connectRange', () => { precision: 2, }); + props = getProvidedProps( + { + attributeName: 'ok', + precision: 2, + }, + {}, + { + results: new SearchResults(new SearchParameters(), [{ hits: [] }]), + } + ); + expect(props).toEqual({ + min: undefined, + max: undefined, + currentRefinement: { + min: undefined, + max: undefined, + }, + count: [], + canRefine: false, + precision: 2, + }); + expect(() => getProvidedProps( { @@ -631,6 +652,30 @@ describe('connectRange', () => { canRefine: false, precision: 2, }); + + props = getProvidedProps( + { + attributeName: 'ok', + precision: 2, + }, + {}, + { + results: { + first: new SearchResults(new SearchParameters(), [{ hits: [] }]), + }, + } + ); + expect(props).toEqual({ + min: undefined, + max: undefined, + currentRefinement: { + min: undefined, + max: undefined, + }, + count: [], + canRefine: false, + precision: 2, + }); }); it("calling refine updates the widget's search state", () => {