Skip to content

Commit

Permalink
fix(connectRefinementList): set default value for limit (#2517)
Browse files Browse the repository at this point in the history
* fix(connectRefinementList): set default value for limit
* fix(connectRefinementList): compute of `hasExhaustiveItems` in edge cases
* docs(connectRefinementList): missing word
* refactor(connectRefinementList): `rawItems` -> `facetValues`
  • Loading branch information
Maxime Janton authored and bobylito committed Oct 23, 2017
1 parent 06aa626 commit 32918c9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 9 deletions.
102 changes: 102 additions & 0 deletions src/connectors/refinement-list/__tests__/connectRefinementList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('connectRefinementList', () => {

expect(widget.getConfiguration()).toEqual({
disjunctiveFacets: ['myFacet'],
maxValuesPerFacet: 10,
});
});

Expand Down Expand Up @@ -79,6 +80,7 @@ describe('connectRefinementList', () => {

expect(widget.getConfiguration()).toEqual({
facets: ['myFacet'],
maxValuesPerFacet: 10,
});
});
});
Expand Down Expand Up @@ -234,6 +236,105 @@ describe('connectRefinementList', () => {
expect(secondRenderingOptions.canToggleShowMore).toBe(false);
});

it('If there are no showMoreLimit specified, canToggleShowMore is false', () => {
const widget = makeWidget({
attributeName: 'category',
limit: 1,
});

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

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

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

const secondRenderingOptions = rendering.lastCall.args[0];
expect(secondRenderingOptions.canToggleShowMore).toBe(false);
});

it('If there are same amount of items then canToggleShowMore is false', () => {
const widget = makeWidget({
attributeName: 'category',
limit: 2,
showMoreLimit: 10,
});

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

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

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

const secondRenderingOptions = rendering.lastCall.args[0];
expect(secondRenderingOptions.canToggleShowMore).toBe(false);
});

it('If there are enough items then canToggleShowMore is true', () => {
const widget = makeWidget({
attributeName: 'category',
Expand Down Expand Up @@ -342,6 +443,7 @@ describe('connectRefinementList', () => {
});

const secondRenderingOptions = rendering.lastCall.args[0];
expect(secondRenderingOptions.canToggleShowMore).toEqual(true);
expect(secondRenderingOptions.items).toEqual([
{
label: 'c1',
Expand Down
19 changes: 10 additions & 9 deletions src/connectors/refinement-list/connectRefinementList.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ export const checkUsage = ({
* @typedef {Object} CustomRefinementListWidgetOptions
* @property {string} attributeName The name of the attribute in the records.
* @property {"and"|"or"} [operator = 'or'] How the filters are combined together.
* @property {number} [limit = undefined] The max number of items to display when
* `showMoreLimit` is not or if the widget is showing less value.
* @property {number} [limit = 10] The max number of items to display when
* `showMoreLimit` is not set or if the widget is showing less value.
* @property {number} [showMoreLimit] The max number of items to display if the widget
* is showing more items.
* @property {string[]|function} [sortBy = ['isRefined', 'count:desc', 'name:asc']] How to sort refinements. Possible values: `count|isRefined|name:asc|name:desc`.
Expand Down Expand Up @@ -148,7 +148,7 @@ export default function connectRefinementList(renderFn) {
const {
attributeName,
operator = 'or',
limit,
limit = 10,
showMoreLimit,
sortBy = ['isRefined', 'count:desc', 'name:asc'],
} = widgetParams;
Expand Down Expand Up @@ -202,7 +202,9 @@ export default function connectRefinementList(renderFn) {
canRefine: isFromSearch || items.length > 0,
widgetParams,
isShowingMore,
canToggleShowMore: isShowingMore || !hasExhaustiveItems,
canToggleShowMore: showMoreLimit
? isShowingMore || !hasExhaustiveItems
: false,
toggleShowMore,
hasExhaustiveItems,
},
Expand Down Expand Up @@ -328,12 +330,11 @@ export default function connectRefinementList(renderFn) {
createURL,
instantSearchInstance,
} = renderOptions;
const items = results
.getFacetValues(attributeName, { sortBy })
.slice(0, this.getLimit())
.map(formatItems);

const hasExhaustiveItems = items.length < this.getLimit();
const facetValues = results.getFacetValues(attributeName, { sortBy });
const items = facetValues.slice(0, this.getLimit()).map(formatItems);

const hasExhaustiveItems = facetValues.length <= this.getLimit();

lastResultsFromMainSearch = items;

Expand Down

0 comments on commit 32918c9

Please sign in to comment.