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

Commit

Permalink
fix(sffv): clamp maxFacetHits to the allowed range (#1696)
Browse files Browse the repository at this point in the history
* fix(createInstantSearchManager): use default value for maxFacetHits

* fix(createInstantSearchManager): clamp maxFacetHits to allowed range

* chore: increate bundlesize by .25
  • Loading branch information
samouss authored Oct 29, 2018
1 parent 48c8ea8 commit 83ce245
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('createInstantSearchManager', () => {
expect(searchForFacetValues).toHaveBeenCalledWith(
'facetName',
'query',
undefined
10
);

expect(store.searchingForFacetValues).toBe(false);
Expand Down Expand Up @@ -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);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 83ce245

Please sign in to comment.