diff --git a/package.json b/package.json index 0bea928d3e..054fdfa844 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,7 @@ }, { "path": "packages/react-instantsearch-core/dist/umd/ReactInstantSearchCore.min.js", - "maxSize": "40.5 kB" + "maxSize": "40.75 kB" }, { "path": "packages/react-instantsearch-dom/dist/umd/ReactInstantSearchDOM.min.js", diff --git a/packages/react-instantsearch-core/src/core/__tests__/createInstantSearchManager.result.js b/packages/react-instantsearch-core/src/core/__tests__/createInstantSearchManager.result.js index 3d820940c7..3f73300ba7 100644 --- a/packages/react-instantsearch-core/src/core/__tests__/createInstantSearchManager.result.js +++ b/packages/react-instantsearch-core/src/core/__tests__/createInstantSearchManager.result.js @@ -157,7 +157,7 @@ describe('createInstantSearchManager', () => { expect(searchForFacetValues).toHaveBeenCalledWith( 'facetName', 'query', - undefined + 10 ); expect(store.searchingForFacetValues).toBe(false); @@ -218,6 +218,106 @@ describe('createInstantSearchManager', () => { expect(store.searchingForFacetValues).toBe(false); }); }); + + it('updates the store and searches with maxFacetHits out of range (higher)', () => { + expect.assertions(4); + + const searchForFacetValues = jest.fn(() => + Promise.resolve({ + facetHits: 'results', + }) + ); + + jsHepler.mockImplementationOnce((...args) => { + const instance = jsHepler(...args); + + instance.searchForFacetValues = searchForFacetValues; + + return instance; + }); + + const ism = createInstantSearchManager({ + indexName: 'index', + initialState: {}, + searchParameters: {}, + searchClient: client, + }); + + ism.onSearchForFacetValues({ + facetName: 'facetName', + query: 'query', + maxFacetHits: 125, + }); + + expect(ism.store.getState().results).toBe(null); + + return Promise.resolve().then(() => { + const store = ism.store.getState(); + + expect(searchForFacetValues).toHaveBeenCalledWith( + 'facetName', + 'query', + 100 + ); + + expect(store.resultsFacetValues).toEqual({ + facetName: 'results', + query: 'query', + }); + + expect(store.searchingForFacetValues).toBe(false); + }); + }); + + it('updates the store and searches with maxFacetHits out of range (lower)', () => { + expect.assertions(4); + + const searchForFacetValues = jest.fn(() => + Promise.resolve({ + facetHits: 'results', + }) + ); + + jsHepler.mockImplementationOnce((...args) => { + const instance = jsHepler(...args); + + instance.searchForFacetValues = searchForFacetValues; + + return instance; + }); + + const ism = createInstantSearchManager({ + indexName: 'index', + initialState: {}, + searchParameters: {}, + searchClient: client, + }); + + ism.onSearchForFacetValues({ + facetName: 'facetName', + query: 'query', + maxFacetHits: 0, + }); + + expect(ism.store.getState().results).toBe(null); + + return Promise.resolve().then(() => { + const store = ism.store.getState(); + + expect(searchForFacetValues).toHaveBeenCalledWith( + 'facetName', + 'query', + 1 + ); + + expect(store.resultsFacetValues).toEqual({ + facetName: 'results', + query: 'query', + }); + + expect(store.searchingForFacetValues).toBe(false); + }); + }); }); }); }); diff --git a/packages/react-instantsearch-core/src/core/createInstantSearchManager.js b/packages/react-instantsearch-core/src/core/createInstantSearchManager.js index 6d0728a5b5..e8a16cb737 100644 --- a/packages/react-instantsearch-core/src/core/createInstantSearchManager.js +++ b/packages/react-instantsearch-core/src/core/createInstantSearchManager.js @@ -271,14 +271,18 @@ export default function createInstantSearchManager({ search(); } - function onSearchForFacetValues({ facetName, query, maxFacetHits }) { + function onSearchForFacetValues({ facetName, query, maxFacetHits = 10 }) { + // The values 1, 100 are the min / max values that the engine accepts. + // see: https://www.algolia.com/doc/api-reference/api-parameters/maxFacetHits + const maxFacetHitsWithinRange = Math.max(1, Math.min(maxFacetHits, 100)); + store.setState({ ...store.getState(), searchingForFacetValues: true, }); helper - .searchForFacetValues(facetName, query, maxFacetHits) + .searchForFacetValues(facetName, query, maxFacetHitsWithinRange) .then( content => { store.setState({