Skip to content

Commit 5f9e138

Browse files
committed
fix(menu): prevent error on stale search (#3934)
* fix(menu): prevent error on stale search If a menu is added while the search is being stale, we will recieve undefined (currently an error), so we can not fetch `.data` from it. requires algolia/algoliasearch-helper-js#720 * test: add a test declaring the use case with wrong helper state
1 parent 0d4b49b commit 5f9e138

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/connectors/menu/__tests__/connectMenu-test.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import jsHelper, {
22
SearchResults,
33
SearchParameters,
44
} from 'algoliasearch-helper';
5-
5+
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
66
import connectMenu from '../connectMenu';
77

88
describe('connectMenu', () => {
@@ -266,6 +266,45 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
266266
);
267267
});
268268

269+
it('returns empty items if the facet is not declared', () => {
270+
const widget = makeWidget({
271+
attribute: 'category',
272+
});
273+
274+
// note that the helper is called with empty search parameters
275+
// which means this can only happen in a stale search situation
276+
// when this widget gets mounted
277+
const helper = jsHelper({}, '', {});
278+
279+
widget.render({
280+
results: new SearchResults(helper.state, [
281+
createSingleSearchResponse({
282+
hits: [],
283+
facets: {
284+
category: {
285+
Decoration: 880,
286+
},
287+
},
288+
}),
289+
createSingleSearchResponse({
290+
facets: {
291+
category: {
292+
Decoration: 880,
293+
Outdoor: 47,
294+
},
295+
},
296+
}),
297+
]),
298+
state: helper.state,
299+
helper,
300+
});
301+
302+
expect(rendering).toHaveBeenLastCalledWith(
303+
expect.objectContaining({ items: [] }),
304+
false
305+
);
306+
});
307+
269308
it('provides the correct transformed facet values', () => {
270309
const widget = makeWidget({
271310
attribute: 'category',

src/connectors/menu/connectMenu.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ export default function connectMenu(renderFn, unmountFn = noop) {
190190
},
191191

192192
render({ results, instantSearchInstance }) {
193+
const facetValues = results.getFacetValues(attribute, { sortBy });
193194
const facetItems =
194-
results.getFacetValues(attribute, { sortBy }).data || [];
195+
facetValues && facetValues.data ? facetValues.data : [];
196+
195197
const items = transformItems(
196198
facetItems
197199
.slice(0, this.getLimit())

0 commit comments

Comments
 (0)