Skip to content

Commit 759f709

Browse files
samoussbobylito
authored andcommitted
fix(connectBreadcrumb): ensure that data is an array (#3067)
1 parent af94ea2 commit 759f709

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,49 @@ describe('connectBreadcrumb', () => {
202202
]);
203203
});
204204

205+
it('provides items from an empty results', () => {
206+
const rendering = jest.fn();
207+
const makeWidget = connectBreadcrumb(rendering);
208+
const widget = makeWidget({
209+
attributes: ['category', 'sub_category'],
210+
});
211+
212+
const config = widget.getConfiguration({});
213+
const helper = jsHelper({}, '', config);
214+
215+
helper.search = jest.fn();
216+
217+
helper.toggleRefinement('category', 'Decoration');
218+
219+
widget.init({
220+
helper,
221+
state: helper.state,
222+
createURL: () => '#',
223+
});
224+
225+
const firstRenderingOptions = rendering.mock.calls[0][0];
226+
expect(firstRenderingOptions.items).toEqual([]);
227+
228+
widget.render({
229+
results: new SearchResults(helper.state, [
230+
{
231+
hits: [],
232+
facets: {},
233+
},
234+
{
235+
hits: [],
236+
facets: {},
237+
},
238+
]),
239+
state: helper.state,
240+
helper,
241+
createURL: () => '#',
242+
});
243+
244+
const secondRenderingOptions = rendering.mock.calls[1][0];
245+
expect(secondRenderingOptions.items).toEqual([]);
246+
});
247+
205248
it('provides the correct facet values when transformed', () => {
206249
const rendering = jest.fn();
207250
const makeWidget = connectBreadcrumb(rendering);
@@ -551,6 +594,7 @@ describe('connectBreadcrumb', () => {
551594
],
552595
});
553596
});
597+
554598
it('Provides an additional configuration if the existing one is different', () => {
555599
const makeWidget = connectBreadcrumb(() => {});
556600
const widget = makeWidget({ attributes: ['category', 'sub_category'] });

src/connectors/breadcrumb/connectBreadcrumb.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ export default function connectBreadcrumb(renderFn, unmountFn) {
154154
render({ instantSearchInstance, results, state }) {
155155
const [{ name: facetName }] = state.hierarchicalFacets;
156156

157-
const facetsValues = results.getFacetValues(facetName);
158-
const items = transformItems(
159-
shiftItemsValues(prepareItems(facetsValues))
160-
);
157+
const facetValues = results.getFacetValues(facetName);
158+
const data = Array.isArray(facetValues.data) ? facetValues.data : [];
159+
const items = transformItems(shiftItemsValues(prepareItems(data)));
161160

162161
renderFn(
163162
{
@@ -179,16 +178,15 @@ export default function connectBreadcrumb(renderFn, unmountFn) {
179178
};
180179
}
181180

182-
function prepareItems(obj) {
183-
return obj.data.reduce((result, currentItem) => {
181+
function prepareItems(data) {
182+
return data.reduce((result, currentItem) => {
184183
if (currentItem.isRefined) {
185184
result.push({
186185
name: currentItem.name,
187186
value: currentItem.path,
188187
});
189188
if (Array.isArray(currentItem.data)) {
190-
const children = prepareItems(currentItem);
191-
result = result.concat(children);
189+
result = result.concat(prepareItems(currentItem.data));
192190
}
193191
}
194192
return result;

0 commit comments

Comments
 (0)