Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(refinementList): fix facet exhaustivity check #2554

Merged
merged 3 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ describe('connectRefinementList', () => {
]);
});

it('hasExhaustiveItems indicates if the items provided are exhaustive', () => {
it('hasExhaustiveItems indicates if the items provided are exhaustive - without other widgets making the maxValuesPerFacet bigger', () => {
const widget = makeWidget({
attributeName: 'category',
limit: 2,
Expand Down Expand Up @@ -512,13 +512,94 @@ describe('connectRefinementList', () => {
facets: {
category: {
c1: 880,
c2: 880,
},
},
},
{
facets: {
category: {
c1: 880,
c2: 880,
},
},
},
]),
state: helper.state,
helper,
createURL: () => '#',
});

// this one is `false` because we're not sure that what we asked is the actual number of facet values
expect(rendering.lastCall.args[0].hasExhaustiveItems).toEqual(false);

widget.render({
results: new SearchResults(helper.state, [
{
hits: [],
facets: {
category: {
c1: 880,
c2: 34,
c3: 440,
},
},
},
{
facets: {
category: {
c1: 880,
c2: 34,
c3: 440,
},
},
},
]),
state: helper.state,
helper,
createURL: () => '#',
});

expect(rendering.lastCall.args[0].hasExhaustiveItems).toEqual(false);
});

it('hasExhaustiveItems indicates if the items provided are exhaustive - with an other widgets making the maxValuesPerFacet bigger', () => {
const widget = makeWidget({
attributeName: 'category',
limit: 2,
});

const helper = algoliasearchHelper(fakeClient, '', {
...widget.getConfiguration({}),
maxValuesPerFacet: 3,
});
helper.search = sinon.stub();

widget.init({
helper,
state: helper.state,
createURL: () => '#',
onHistoryChange: () => {},
});

expect(rendering.lastCall.args[0].hasExhaustiveItems).toEqual(true);

widget.render({
results: new SearchResults(helper.state, [
{
hits: [],
facets: {
category: {
c1: 880,
c2: 880,
},
},
},
{
facets: {
category: {
c1: 880,
c2: 880,
},
},
},
Expand All @@ -539,6 +620,7 @@ describe('connectRefinementList', () => {
c1: 880,
c2: 34,
c3: 440,
c4: 440,
},
},
},
Expand All @@ -548,6 +630,7 @@ describe('connectRefinementList', () => {
c1: 880,
c2: 34,
c3: 440,
c4: 440,
},
},
},
Expand Down
15 changes: 14 additions & 1 deletion src/connectors/refinement-list/connectRefinementList.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,20 @@ export default function connectRefinementList(renderFn) {
const facetValues = results.getFacetValues(attributeName, { sortBy });
const items = facetValues.slice(0, this.getLimit()).map(formatItems);

const hasExhaustiveItems = facetValues.length <= this.getLimit();
const maxValuesPerFacetConfig = state.getQueryParameter(
'maxValuesPerFacet'
);
const currentLimit = this.getLimit();
// If the limit is the max number of facet retrieved it is impossible to know
// if the facets are exhaustives. The only moment we are sure it is exhaustive
// is when it is strictly under the number requested unless we know that another
// widget has requested more values (maxValuesPerFacet > getLimit()).
// Because this is used for making the search of facets unable or not, it is important
// to be conservative here.
const hasExhaustiveItems =
maxValuesPerFacetConfig > currentLimit
? facetValues.length <= currentLimit
: facetValues.length < currentLimit;

lastResultsFromMainSearch = items;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import sinon from 'sinon';
import expectJSX from 'expect-jsx';
expect.extend(expectJSX);

import algoliasearchHelper from 'algoliasearch-helper';
const SearchParameters = algoliasearchHelper.SearchParameters;
import refinementList from '../refinement-list.js';
const instantSearchInstance = { templatesConfig: {} };

Expand Down Expand Up @@ -57,7 +59,7 @@ describe('refinementList()', () => {
.stub()
.returns([{ name: 'foo' }, { name: 'bar' }]),
};
state = { toggleRefinement: sinon.spy() };
state = SearchParameters.make({});
createURL = () => '#';
});

Expand Down