diff --git a/src/connectors/menu/__tests__/connectMenu-test.js b/src/connectors/menu/__tests__/connectMenu-test.js index 88fb722a6f..a37197c8f2 100644 --- a/src/connectors/menu/__tests__/connectMenu-test.js +++ b/src/connectors/menu/__tests__/connectMenu-test.js @@ -2,7 +2,7 @@ import jsHelper, { SearchResults, SearchParameters, } from 'algoliasearch-helper'; - +import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse'; import connectMenu from '../connectMenu'; describe('connectMenu', () => { @@ -266,6 +266,45 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co ); }); + it('returns empty items if the facet is not declared', () => { + const widget = makeWidget({ + attribute: 'category', + }); + + // note that the helper is called with empty search parameters + // which means this can only happen in a stale search situation + // when this widget gets mounted + const helper = jsHelper({}, '', {}); + + widget.render({ + results: new SearchResults(helper.state, [ + createSingleSearchResponse({ + hits: [], + facets: { + category: { + Decoration: 880, + }, + }, + }), + createSingleSearchResponse({ + facets: { + category: { + Decoration: 880, + Outdoor: 47, + }, + }, + }), + ]), + state: helper.state, + helper, + }); + + expect(rendering).toHaveBeenLastCalledWith( + expect.objectContaining({ items: [] }), + false + ); + }); + it('provides the correct transformed facet values', () => { const widget = makeWidget({ attribute: 'category', diff --git a/src/connectors/menu/connectMenu.js b/src/connectors/menu/connectMenu.js index 712a597c7b..3e2b0c55e4 100644 --- a/src/connectors/menu/connectMenu.js +++ b/src/connectors/menu/connectMenu.js @@ -189,8 +189,10 @@ export default function connectMenu(renderFn, unmountFn) { }, render({ results, instantSearchInstance }) { + const facetValues = results.getFacetValues(attribute, { sortBy }); const facetItems = - results.getFacetValues(attribute, { sortBy }).data || []; + facetValues && facetValues.data ? facetValues.data : []; + const items = transformItems( facetItems .slice(0, this.getLimit())