Skip to content

Commit

Permalink
fix(connectBreadcrumb): ensure that data is an array (#3067)
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss authored and bobylito committed Aug 17, 2018
1 parent af94ea2 commit 759f709
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
44 changes: 44 additions & 0 deletions src/connectors/breadcrumb/__tests__/connectBreadcrumb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,49 @@ describe('connectBreadcrumb', () => {
]);
});

it('provides items from an empty results', () => {
const rendering = jest.fn();
const makeWidget = connectBreadcrumb(rendering);
const widget = makeWidget({
attributes: ['category', 'sub_category'],
});

const config = widget.getConfiguration({});
const helper = jsHelper({}, '', config);

helper.search = jest.fn();

helper.toggleRefinement('category', 'Decoration');

widget.init({
helper,
state: helper.state,
createURL: () => '#',
});

const firstRenderingOptions = rendering.mock.calls[0][0];
expect(firstRenderingOptions.items).toEqual([]);

widget.render({
results: new SearchResults(helper.state, [
{
hits: [],
facets: {},
},
{
hits: [],
facets: {},
},
]),
state: helper.state,
helper,
createURL: () => '#',
});

const secondRenderingOptions = rendering.mock.calls[1][0];
expect(secondRenderingOptions.items).toEqual([]);
});

it('provides the correct facet values when transformed', () => {
const rendering = jest.fn();
const makeWidget = connectBreadcrumb(rendering);
Expand Down Expand Up @@ -551,6 +594,7 @@ describe('connectBreadcrumb', () => {
],
});
});

it('Provides an additional configuration if the existing one is different', () => {
const makeWidget = connectBreadcrumb(() => {});
const widget = makeWidget({ attributes: ['category', 'sub_category'] });
Expand Down
14 changes: 6 additions & 8 deletions src/connectors/breadcrumb/connectBreadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ export default function connectBreadcrumb(renderFn, unmountFn) {
render({ instantSearchInstance, results, state }) {
const [{ name: facetName }] = state.hierarchicalFacets;

const facetsValues = results.getFacetValues(facetName);
const items = transformItems(
shiftItemsValues(prepareItems(facetsValues))
);
const facetValues = results.getFacetValues(facetName);
const data = Array.isArray(facetValues.data) ? facetValues.data : [];
const items = transformItems(shiftItemsValues(prepareItems(data)));

renderFn(
{
Expand All @@ -179,16 +178,15 @@ export default function connectBreadcrumb(renderFn, unmountFn) {
};
}

function prepareItems(obj) {
return obj.data.reduce((result, currentItem) => {
function prepareItems(data) {
return data.reduce((result, currentItem) => {
if (currentItem.isRefined) {
result.push({
name: currentItem.name,
value: currentItem.path,
});
if (Array.isArray(currentItem.data)) {
const children = prepareItems(currentItem);
result = result.concat(children);
result = result.concat(prepareItems(currentItem.data));
}
}
return result;
Expand Down

0 comments on commit 759f709

Please sign in to comment.