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

Commit

Permalink
fix(connectRange): check if facet exist before access (#797)
Browse files Browse the repository at this point in the history
* fix(connectRange): check if facet exist before access

* refactor(connectRange): use hasFacet instead of isFacetExist

* refactor(connectRange): avoid to inline the map for better visibility
  • Loading branch information
samouss authored Jan 8, 2018
1 parent 340b16d commit 6520760
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
15 changes: 8 additions & 7 deletions packages/react-instantsearch/src/connectors/connectRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
53 changes: 49 additions & 4 deletions packages/react-instantsearch/src/connectors/connectRange.test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -214,6 +212,7 @@ describe('connectRange', () => {
});

results = {
getFacetByName: () => false,
getFacetStats: () => {},
getFacetValues: () => [],
hits: [],
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit 6520760

Please sign in to comment.