diff --git a/index.d.ts b/index.d.ts index 419ac41b2..ff092a577 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1353,7 +1353,7 @@ declare namespace algoliasearchHelper { getFacetValues( attribute: string, opts: { - sortBy: string[], + sortBy: any; facetOrdering: boolean; } ): SearchResults.FacetValue[] | SearchResults.HierarchicalFacet | undefined; diff --git a/src/SearchResults/index.js b/src/SearchResults/index.js index 186338b5d..b41ddf334 100644 --- a/src/SearchResults/index.js +++ b/src/SearchResults/index.js @@ -719,8 +719,10 @@ function sortViaFacetOrdering(facetValues, facetOrdering) { }, {}); facetValues.forEach(function(item) { - if (reverseOrder[item.name] !== undefined) { - orderedFacets[reverseOrder[item.name]] = item; + // hierarchical facets get sorted using their raw name + var name = item.path || item.name; + if (reverseOrder[name] !== undefined) { + orderedFacets[reverseOrder[name]] = item; } else { remainingFacets.push(item); } @@ -731,7 +733,7 @@ function sortViaFacetOrdering(facetValues, facetOrdering) { if (sortRemainingBy === 'hidden') { return orderedFacets; } else if (sortRemainingBy === 'alpha') { - ordering = [['name'], ['asc']]; + ordering = [['path', 'name'], ['asc', 'asc']]; } else { ordering = [['count'], ['desc']]; } diff --git a/test/spec/SearchResults/getFacetValues-facetOrdering.js b/test/spec/SearchResults/getFacetValues-facetOrdering.js index 665c61f22..d32c3807d 100644 --- a/test/spec/SearchResults/getFacetValues-facetOrdering.js +++ b/test/spec/SearchResults/getFacetValues-facetOrdering.js @@ -275,6 +275,14 @@ describe('hierarchical facet', function() { isRefined: false, exhaustive: true, data: null + }, + { + name: 'Useless Gift Cards', + path: 'Best Buy Gift Cards > Useless Gift Cards', + count: 12, + isRefined: false, + exhaustive: true, + data: null } ] }, @@ -374,6 +382,14 @@ describe('hierarchical facet', function() { isRefined: false, exhaustive: true, data: null + }, + { + name: 'Useless Gift Cards', + path: 'Best Buy Gift Cards > Useless Gift Cards', + count: 12, + isRefined: false, + exhaustive: true, + data: null } ] }, @@ -466,6 +482,14 @@ describe('hierarchical facet', function() { isRefined: false, exhaustive: true, data: null + }, + { + name: 'Useless Gift Cards', + path: 'Best Buy Gift Cards > Useless Gift Cards', + count: 12, + isRefined: false, + exhaustive: true, + data: null } ] }, @@ -558,6 +582,14 @@ describe('hierarchical facet', function() { isRefined: false, exhaustive: true, data: null + }, + { + name: 'Useless Gift Cards', + path: 'Best Buy Gift Cards > Useless Gift Cards', + count: 12, + isRefined: false, + exhaustive: true, + data: null } ] }, @@ -658,6 +690,14 @@ describe('hierarchical facet', function() { isRefined: false, exhaustive: true, data: null + }, + { + name: 'Useless Gift Cards', + path: 'Best Buy Gift Cards > Useless Gift Cards', + count: 12, + isRefined: false, + exhaustive: true, + data: null } ] } @@ -678,7 +718,7 @@ describe('hierarchical facet', function() { sortRemainingBy: 'hidden' }, 'hierarchicalCategories.lvl1': { - order: ['Swag Gift Cards'], + order: ['Best Buy Gift Cards > Entertainment Gift Cards'], sortRemainingBy: 'count' } } @@ -707,6 +747,14 @@ describe('hierarchical facet', function() { isRefined: true, exhaustive: true, data: [ + { + name: 'Entertainment Gift Cards', + path: 'Best Buy Gift Cards > Entertainment Gift Cards', + count: 17, + isRefined: true, + exhaustive: true, + data: null + }, { name: 'Swag Gift Cards', path: 'Best Buy Gift Cards > Swag Gift Cards', @@ -716,10 +764,10 @@ describe('hierarchical facet', function() { data: null }, { - name: 'Entertainment Gift Cards', - path: 'Best Buy Gift Cards > Entertainment Gift Cards', - count: 17, - isRefined: true, + name: 'Useless Gift Cards', + path: 'Best Buy Gift Cards > Useless Gift Cards', + count: 12, + isRefined: false, exhaustive: true, data: null } diff --git a/test/spec/SearchResults/getFacetValues/hierarchical.json b/test/spec/SearchResults/getFacetValues/hierarchical.json index 805a97bfe..c632f0a66 100644 --- a/test/spec/SearchResults/getFacetValues/hierarchical.json +++ b/test/spec/SearchResults/getFacetValues/hierarchical.json @@ -937,8 +937,9 @@ "Best Buy Gift Cards": 80 }, "hierarchicalCategories.lvl1": { + "Best Buy Gift Cards > Swag Gift Cards": 20, "Best Buy Gift Cards > Entertainment Gift Cards": 17, - "Best Buy Gift Cards > Swag Gift Cards": 20 + "Best Buy Gift Cards > Useless Gift Cards": 12 } }, "exhaustiveFacetsCount": true,