From ec765f77464bc472dcf5e77fe09d5f373857e504 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Wed, 10 Jul 2019 12:57:45 +0200 Subject: [PATCH 1/2] fix(getFacetValues): don't throw error when there's no facet connectMenu could throw if a facet is retrieved which isn't in the SearchParameters, but we are changing that to return undefined in those cases. This is part 1 of the big error removal process. --- index.d.ts | 2 +- src/SearchResults/index.js | 6 ++++-- test/spec/SearchResults/getFacetValues.js | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 153279612..5cd17dc4f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1155,7 +1155,7 @@ declare namespace algoliasearchHelper { getFacetValues( attribute: string, opts: any - ): SearchResults.FacetValue[] | SearchResults.HierarchicalFacet; + ): SearchResults.FacetValue[] | SearchResults.HierarchicalFacet | undefined; /** * Returns the facet stats if attribute is defined and the facet contains some. diff --git a/src/SearchResults/index.js b/src/SearchResults/index.js index aaf6acc6e..d33f0efec 100644 --- a/src/SearchResults/index.js +++ b/src/SearchResults/index.js @@ -674,7 +674,7 @@ function vanillaSortFn(order, data) { * bigger or -1 otherwise. * * The default value for this attribute `['isRefined:desc', 'count:desc', 'name:asc']` - * @return {FacetValue[]|HierarchicalFacet} depending on the type of facet of + * @return {FacetValue[]|HierarchicalFacet|undefined} depending on the type of facet of * the attribute requested (hierarchical, disjunctive or conjunctive) * @example * helper.on('result', function(event){ @@ -693,7 +693,9 @@ function vanillaSortFn(order, data) { */ SearchResults.prototype.getFacetValues = function(attribute, opts) { var facetValues = extractNormalizedFacetValues(this, attribute); - if (!facetValues) throw new Error(attribute + ' is not a retrieved facet.'); + if (!facetValues) { + return undefined; + } var options = defaultsPure({}, opts, {sortBy: SearchResults.DEFAULT_SORT}); diff --git a/test/spec/SearchResults/getFacetValues.js b/test/spec/SearchResults/getFacetValues.js index f5fecac5b..af9a68d9a 100644 --- a/test/spec/SearchResults/getFacetValues.js +++ b/test/spec/SearchResults/getFacetValues.js @@ -148,3 +148,20 @@ test('getFacetValues(disjunctive) returns correct facet values with the name `le expect(facetValues).toEqual(expected); expect(facetValues.length).toBe(2); }); + +test.only('getFacetValues(unknown) returns undefined (does not throw)', function() { + var searchParams = new SearchParameters({ + index: 'instant_search' + }); + + var result = { + query: '', + // it does not matter if the result here is given or not, + // if something is not a parameter, it will not be read. + facets: {} + }; + + var results = new SearchResults(searchParams, [result, result]); + + expect(results.getFacetValues('type')).toEqual(undefined); +}); From 42a1ab92307b537762e37f850cdbd6cc710ea4a5 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Wed, 10 Jul 2019 13:02:51 +0200 Subject: [PATCH 2/2] Apply suggestions from code review good calls, thanks @samouss Co-Authored-By: Samuel Vaillant --- test/spec/SearchResults/getFacetValues.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/spec/SearchResults/getFacetValues.js b/test/spec/SearchResults/getFacetValues.js index af9a68d9a..0f58fcf2a 100644 --- a/test/spec/SearchResults/getFacetValues.js +++ b/test/spec/SearchResults/getFacetValues.js @@ -149,7 +149,7 @@ test('getFacetValues(disjunctive) returns correct facet values with the name `le expect(facetValues.length).toBe(2); }); -test.only('getFacetValues(unknown) returns undefined (does not throw)', function() { +test('getFacetValues(unknown) returns undefined (does not throw)', function() { var searchParams = new SearchParameters({ index: 'instant_search' }); @@ -163,5 +163,5 @@ test.only('getFacetValues(unknown) returns undefined (does not throw)', function var results = new SearchResults(searchParams, [result, result]); - expect(results.getFacetValues('type')).toEqual(undefined); + expect(results.getFacetValues('type')).toBeUndefined(); });