Skip to content

Commit

Permalink
fix(helper): correctly set isRefined for hierarchical facet values wi…
Browse files Browse the repository at this point in the history
…th trailing spaces (#6059)
  • Loading branch information
dhayab committed Feb 20, 2024
1 parent e98e898 commit a12e567
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/algoliasearch-helper/src/SearchResults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ function extractNormalizedFacetValues(results, attribute) {
* @return {undefined} function mutates the item
*/
function setIsRefined(item, currentRefinement, depth) {
item.isRefined = item.name === currentRefinement[depth];
item.isRefined =
item.name === (currentRefinement[depth] && currentRefinement[depth].trim());
if (item.data) {
item.data.forEach(function (child) {
setIsRefined(child, currentRefinement, depth + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,61 @@ test('getFacetValues(hierarchical) takes `rootPath` into account', function () {
expect(facetValues).toEqual(expected);
});

test('getFacetValues(hierarchical) correctly sets `isRefined` on facet values with trailing spaces', function () {
var searchParams = new SearchParameters({
index: 'instant_search',
hierarchicalFacets: [
{
name: 'type',
attributes: ['type1', 'type2', 'type3'],
},
],
hierarchicalFacetsRefinements: { type: ['something > discounts '] },
});

var result = {
query: '',
facets: {
type1: {
dogs: 1,
something: 5,
},
type2: {
'dogs > hounds': 1,
'something > discounts ': 5,
},
type3: {
'something > discounts > -5%': 1,
'something > discounts > full price': 4,
},
},
exhaustiveFacetsCount: true,
};

var results = new SearchResults(searchParams, [result, result, result]);

var facetValues = results.getFacetValues('type');

expect(facetValues).toEqual(
expect.objectContaining({
name: 'type',
isRefined: true,
data: expect.arrayContaining([
expect.objectContaining({
name: 'something',
isRefined: true,
data: expect.arrayContaining([
expect.objectContaining({
name: 'discounts',
isRefined: true,
}),
]),
}),
]),
})
);
});

test('getFacetValues(unknown) returns undefined (does not throw)', function () {
var searchParams = new SearchParameters({
index: 'instant_search',
Expand Down

0 comments on commit a12e567

Please sign in to comment.