Skip to content

Commit a12e567

Browse files
authored
fix(helper): correctly set isRefined for hierarchical facet values with trailing spaces (#6059)
1 parent e98e898 commit a12e567

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

packages/algoliasearch-helper/src/SearchResults/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,8 @@ function extractNormalizedFacetValues(results, attribute) {
755755
* @return {undefined} function mutates the item
756756
*/
757757
function setIsRefined(item, currentRefinement, depth) {
758-
item.isRefined = item.name === currentRefinement[depth];
758+
item.isRefined =
759+
item.name === (currentRefinement[depth] && currentRefinement[depth].trim());
759760
if (item.data) {
760761
item.data.forEach(function (child) {
761762
setIsRefined(child, currentRefinement, depth + 1);

packages/algoliasearch-helper/test/spec/SearchResults/getFacetValues.js

+55
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,61 @@ test('getFacetValues(hierarchical) takes `rootPath` into account', function () {
495495
expect(facetValues).toEqual(expected);
496496
});
497497

498+
test('getFacetValues(hierarchical) correctly sets `isRefined` on facet values with trailing spaces', function () {
499+
var searchParams = new SearchParameters({
500+
index: 'instant_search',
501+
hierarchicalFacets: [
502+
{
503+
name: 'type',
504+
attributes: ['type1', 'type2', 'type3'],
505+
},
506+
],
507+
hierarchicalFacetsRefinements: { type: ['something > discounts '] },
508+
});
509+
510+
var result = {
511+
query: '',
512+
facets: {
513+
type1: {
514+
dogs: 1,
515+
something: 5,
516+
},
517+
type2: {
518+
'dogs > hounds': 1,
519+
'something > discounts ': 5,
520+
},
521+
type3: {
522+
'something > discounts > -5%': 1,
523+
'something > discounts > full price': 4,
524+
},
525+
},
526+
exhaustiveFacetsCount: true,
527+
};
528+
529+
var results = new SearchResults(searchParams, [result, result, result]);
530+
531+
var facetValues = results.getFacetValues('type');
532+
533+
expect(facetValues).toEqual(
534+
expect.objectContaining({
535+
name: 'type',
536+
isRefined: true,
537+
data: expect.arrayContaining([
538+
expect.objectContaining({
539+
name: 'something',
540+
isRefined: true,
541+
data: expect.arrayContaining([
542+
expect.objectContaining({
543+
name: 'discounts',
544+
isRefined: true,
545+
}),
546+
]),
547+
}),
548+
]),
549+
})
550+
);
551+
});
552+
498553
test('getFacetValues(unknown) returns undefined (does not throw)', function () {
499554
var searchParams = new SearchParameters({
500555
index: 'instant_search',

0 commit comments

Comments
 (0)