Skip to content

Commit 535ae97

Browse files
committed
feat(helper): persistHierarchicalRootCount: true (#6478)
* feat(helper): persistHierarchicalRootCount: true BREAKING CHANGE: the default behaviour of the helper is now persistHierarchicalRootCount: true * fix(options): remove persistHierarchicalRootCount option It behaves as persistHierarchicalRootCount: true
1 parent b652405 commit 535ae97

File tree

11 files changed

+107
-380
lines changed

11 files changed

+107
-380
lines changed

packages/algoliasearch-helper/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,6 @@ declare namespace algoliasearchHelper {
11141114
* This is for internal use, e.g., avoiding caching in infinite hits, or delaying the display of these results.
11151115
*/
11161116
__isArtificial?: boolean | undefined;
1117-
persistHierarchicalRootCount?: boolean;
11181117
}
11191118

11201119
type ISearchResponse<T> = Omit<SearchResponse<T>, 'facets' | 'params'> &

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

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,8 @@ function SearchResults(state, results, options) {
240240
});
241241

242242
// Make every key of the result options reachable from the instance
243-
var opts = defaultsPure(options, {
244-
persistHierarchicalRootCount: false,
245-
});
246-
Object.keys(opts).forEach(function (key) {
247-
self[key] = opts[key];
243+
Object.keys(options || {}).forEach(function (key) {
244+
self[key] = options[key];
248245
});
249246

250247
/**
@@ -508,16 +505,10 @@ function SearchResults(state, results, options) {
508505
return;
509506
}
510507

511-
self.hierarchicalFacets[position][attributeIndex].data =
512-
self.persistHierarchicalRootCount
513-
? defaultsPure(
514-
self.hierarchicalFacets[position][attributeIndex].data,
515-
facetResults
516-
)
517-
: defaultsPure(
518-
facetResults,
519-
self.hierarchicalFacets[position][attributeIndex].data
520-
);
508+
self.hierarchicalFacets[position][attributeIndex].data = defaultsPure(
509+
self.hierarchicalFacets[position][attributeIndex].data,
510+
facetResults
511+
);
521512
} else {
522513
position = disjunctiveFacetsIndices[dfacet];
523514

@@ -589,28 +580,7 @@ function SearchResults(state, results, options) {
589580
return;
590581
}
591582

592-
// when we always get root levels, if the hits refinement is `beers > IPA` (count: 5),
593-
// then the disjunctive values will be `beers` (count: 100),
594-
// but we do not want to display
595-
// | beers (100)
596-
// > IPA (5)
597-
// We want
598-
// | beers (5)
599-
// > IPA (5)
600-
// @MAJOR: remove this legacy behaviour in next major version
601-
var defaultData = {};
602-
603-
if (
604-
currentRefinement.length > 0 &&
605-
!self.persistHierarchicalRootCount
606-
) {
607-
var root = currentRefinement[0].split(separator)[0];
608-
defaultData[root] =
609-
self.hierarchicalFacets[position][attributeIndex].data[root];
610-
}
611-
612583
self.hierarchicalFacets[position][attributeIndex].data = defaultsPure(
613-
defaultData,
614584
facetResults,
615585
self.hierarchicalFacets[position][attributeIndex].data
616586
);

packages/algoliasearch-helper/test/datasets/SearchParameters/search.dataset.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ function getData() {
336336
index: 'test_hotels-node',
337337
hitsPerPage: 20,
338338
nbHits: 4,
339-
persistHierarchicalRootCount: false,
340339
nbPages: 1,
341340
page: 0,
342341
params:

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

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,10 @@ test('getFacetValues(facetName) prefers the "main" facet result (disjunctive)',
751751
expect(facetValues).toEqual(expected);
752752
});
753753

754-
test('getFacetValues(facetName) prefers the "main" facet result (hierarchical) (persistHierarchicalRootCount: true)', function () {
754+
test('getFacetValues(facetName) prefers the "main" facet result (hierarchical)', function () {
755755
var data = require('./getFacetValues/hierarchical-non-exhaustive.json');
756756
var searchParams = new SearchParameters(data.state);
757-
var result = new SearchResults(searchParams, data.content.results, {
758-
persistHierarchicalRootCount: true,
759-
});
757+
var result = new SearchResults(searchParams, data.content.results);
760758

761759
var facetValues = result.getFacetValues('brand').data;
762760

@@ -792,45 +790,3 @@ test('getFacetValues(facetName) prefers the "main" facet result (hierarchical) (
792790

793791
expect(facetValues).toEqual(expected);
794792
});
795-
796-
test('getFacetValues(facetName) prefers the "main" facet result (hierarchical) (persistHierarchicalRootCount: false)', function () {
797-
var data = require('./getFacetValues/hierarchical-non-exhaustive.json');
798-
var searchParams = new SearchParameters(data.state);
799-
var result = new SearchResults(searchParams, data.content.results, {
800-
persistHierarchicalRootCount: false,
801-
});
802-
803-
var facetValues = result.getFacetValues('brand').data;
804-
805-
var expected = [
806-
{
807-
count: 50,
808-
data: null,
809-
isRefined: true,
810-
name: 'Apple',
811-
escapedValue: 'Apple',
812-
path: 'Apple',
813-
exhaustive: true,
814-
},
815-
{
816-
count: 551,
817-
data: null,
818-
isRefined: false,
819-
name: 'Insignia™',
820-
escapedValue: 'Insignia™',
821-
path: 'Insignia™',
822-
exhaustive: true,
823-
},
824-
{
825-
count: 551,
826-
data: null,
827-
isRefined: false,
828-
name: 'Samsung',
829-
escapedValue: 'Samsung',
830-
path: 'Samsung',
831-
exhaustive: true,
832-
},
833-
];
834-
835-
expect(facetValues).toEqual(expected);
836-
});

packages/algoliasearch-helper/test/spec/algoliasearch.helper/searchOnce.js

Lines changed: 72 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var algoliasearchHelper = require('../../../index');
44
var SearchParameters = require('../../../src/SearchParameters');
55

6-
test('searchOnce should call the algolia client according to the number of refinements and call callback with no error and with results when no error', function (done) {
6+
test('searchOnce should call the algolia client according to the number of refinements and call callback with no error and with results when no error', async function () {
77
var testData = require('../../datasets/SearchParameters/search.dataset')();
88

99
var client = {
@@ -21,76 +21,72 @@ test('searchOnce should call the algolia client according to the number of refin
2121
.addDisjunctiveFacetRefinement('city', 'Paris')
2222
.addDisjunctiveFacetRefinement('city', 'New York');
2323

24-
helper.searchOnce(parameters, function (err, data) {
25-
expect(err).toBe(null);
26-
27-
// shame deepclone, to remove any associated methods coming from the results
28-
expect(JSON.parse(JSON.stringify(data))).toEqual(
29-
JSON.parse(JSON.stringify(testData.responseHelper))
30-
);
31-
32-
var cityValues = data.getFacetValues('city');
33-
var expectedCityValues = [
34-
{ name: 'Paris', escapedValue: 'Paris', count: 3, isRefined: true },
35-
{ name: 'New York', escapedValue: 'New York', count: 1, isRefined: true },
36-
{
37-
name: 'San Francisco',
38-
escapedValue: 'San Francisco',
39-
count: 1,
40-
isRefined: false,
41-
},
42-
];
43-
44-
expect(cityValues).toEqual(expectedCityValues);
45-
46-
var cityValuesCustom = data.getFacetValues('city', {
47-
sortBy: ['count:asc', 'name:asc'],
48-
});
49-
var expectedCityValuesCustom = [
50-
{ name: 'New York', escapedValue: 'New York', count: 1, isRefined: true },
51-
{
52-
name: 'San Francisco',
53-
escapedValue: 'San Francisco',
54-
count: 1,
55-
isRefined: false,
56-
},
57-
{ name: 'Paris', escapedValue: 'Paris', count: 3, isRefined: true },
58-
];
59-
60-
expect(cityValuesCustom).toEqual(expectedCityValuesCustom);
61-
62-
var cityValuesFn = data.getFacetValues('city', {
63-
sortBy: function (a, b) {
64-
return a.count - b.count;
65-
},
66-
});
67-
var expectedCityValuesFn = [
68-
{ name: 'New York', escapedValue: 'New York', count: 1, isRefined: true },
69-
{
70-
name: 'San Francisco',
71-
escapedValue: 'San Francisco',
72-
count: 1,
73-
isRefined: false,
74-
},
75-
{ name: 'Paris', escapedValue: 'Paris', count: 3, isRefined: true },
76-
];
77-
78-
expect(cityValuesFn).toEqual(expectedCityValuesFn);
79-
80-
expect(client.search).toHaveBeenCalledTimes(1);
81-
82-
var queries = client.search.mock.calls[0][0];
83-
for (var i = 0; i < queries.length; i++) {
84-
var query = queries[i];
85-
expect(query.query).toBeUndefined();
86-
expect(query.params.query).toBeUndefined();
87-
}
88-
89-
done();
24+
const { content } = await helper.searchOnce(parameters);
25+
26+
// shame deepclone, to remove any associated methods coming from the results
27+
expect(JSON.parse(JSON.stringify(content))).toEqual(
28+
JSON.parse(JSON.stringify(testData.responseHelper))
29+
);
30+
31+
var cityValues = content.getFacetValues('city');
32+
var expectedCityValues = [
33+
{ name: 'Paris', escapedValue: 'Paris', count: 3, isRefined: true },
34+
{ name: 'New York', escapedValue: 'New York', count: 1, isRefined: true },
35+
{
36+
name: 'San Francisco',
37+
escapedValue: 'San Francisco',
38+
count: 1,
39+
isRefined: false,
40+
},
41+
];
42+
43+
expect(cityValues).toEqual(expectedCityValues);
44+
45+
var cityValuesCustom = content.getFacetValues('city', {
46+
sortBy: ['count:asc', 'name:asc'],
9047
});
48+
var expectedCityValuesCustom = [
49+
{ name: 'New York', escapedValue: 'New York', count: 1, isRefined: true },
50+
{
51+
name: 'San Francisco',
52+
escapedValue: 'San Francisco',
53+
count: 1,
54+
isRefined: false,
55+
},
56+
{ name: 'Paris', escapedValue: 'Paris', count: 3, isRefined: true },
57+
];
58+
59+
expect(cityValuesCustom).toEqual(expectedCityValuesCustom);
60+
61+
var cityValuesFn = content.getFacetValues('city', {
62+
sortBy: function (a, b) {
63+
return a.count - b.count;
64+
},
65+
});
66+
var expectedCityValuesFn = [
67+
{ name: 'New York', escapedValue: 'New York', count: 1, isRefined: true },
68+
{
69+
name: 'San Francisco',
70+
escapedValue: 'San Francisco',
71+
count: 1,
72+
isRefined: false,
73+
},
74+
{ name: 'Paris', escapedValue: 'Paris', count: 3, isRefined: true },
75+
];
76+
77+
expect(cityValuesFn).toEqual(expectedCityValuesFn);
78+
79+
expect(client.search).toHaveBeenCalledTimes(1);
80+
81+
var queries = client.search.mock.calls[0][0];
82+
for (var i = 0; i < queries.length; i++) {
83+
var query = queries[i];
84+
expect(query.query).toBeUndefined();
85+
expect(query.params.query).toBeUndefined();
86+
}
9187
});
9288

93-
test('searchOnce should call the algolia client according to the number of refinements and call callback with error and no results when error', function (done) {
89+
test('searchOnce should call the algolia client according to the number of refinements and call callback with error and no results when error', async function () {
9490
var error = { message: 'error' };
9591
var client = {
9692
search: jest.fn().mockImplementationOnce(function () {
@@ -107,19 +103,14 @@ test('searchOnce should call the algolia client according to the number of refin
107103
.addDisjunctiveFacetRefinement('city', 'Paris')
108104
.addDisjunctiveFacetRefinement('city', 'New York');
109105

110-
helper.searchOnce(parameters, function (err, data) {
111-
expect(err).toBe(error);
112-
expect(data).toBe(null);
113-
114-
expect(client.search).toHaveBeenCalledTimes(1);
106+
await expect(() => helper.searchOnce(parameters)).rejects.toEqual(error);
115107

116-
var queries = client.search.mock.calls[0][0];
117-
for (var i = 0; i < queries.length; i++) {
118-
var query = queries[i];
119-
expect(query.query).toBeUndefined();
120-
expect(query.params.query).toBeUndefined();
121-
}
108+
expect(client.search).toHaveBeenCalledTimes(1);
122109

123-
done();
124-
});
110+
var queries = client.search.mock.calls[0][0];
111+
for (var i = 0; i < queries.length; i++) {
112+
var query = queries[i];
113+
expect(query.query).toBeUndefined();
114+
expect(query.params.query).toBeUndefined();
115+
}
125116
});

0 commit comments

Comments
 (0)