Skip to content

Commit

Permalink
feat(connectors): add currentRefinement on hierarchical-menu
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 27, 2017
1 parent b6af196 commit 154cdb5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,75 @@ describe('connectHierarchicalMenu', () => {
},
]);
});

it('provides the correct `currentRefinement` value', () => {
const rendering = jest.fn();
const makeWidget = connectHierarchicalMenu(rendering);
const widget = makeWidget({
attributes: ['category', 'subCategory'],
});

const helper = jsHelper({addAlgoliaAgent: () => {}}, '', widget.getConfiguration({}));
helper.search = sinon.stub();

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

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

expect(rendering).toBeCalled();
expect(rendering.mock.calls[0][0].currentRefinement).toBe(null);

widget.render({
results: new SearchResults(helper.state, [{
hits: [],
facets: {
category: {
Decoration: 880,
},
subCategory: {
'Decoration > Candle holders & candles': 193,
'Decoration > Frames & pictures': 173,
},
},
}, {
facets: {
category: {
Decoration: 880,
Outdoor: 47,
},
},
}]),
state: helper.state,
helper,
createURL: () => '#',
});

expect(rendering.mock.calls[1][0].currentRefinement).toEqual({
name: 'Decoration',
path: 'Decoration',
count: 880,
isRefined: true,
data: [
{
name: 'Candle holders & candles',
path: 'Decoration > Candle holders & candles',
count: 193,
isRefined: false,
data: null,
},
{
name: 'Frames & pictures',
path: 'Decoration > Frames & pictures',
count: 173,
isRefined: false,
data: null,
},
],
});
});
});
8 changes: 8 additions & 0 deletions src/connectors/hierarchical-menu/connectHierarchicalMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var customToggle = connectHierarchicalMenu(function renderFn(params, isFirstRend
// refine,
// instantSearchInstance,
// widgetParams,
// currentRefinement,
// }
});
search.addWidget(
Expand Down Expand Up @@ -41,6 +42,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
* @property {function} refine set the path of the hierarchical filter and triggers a new search
* @property {InstantSearch} instantSearchInstance the instance of instantsearch on which the widget is attached
* @property {Object} widgetParams all original options forwarded to rendering
* @property {Object} currentRefinement the refinement currently applied
*/

/**
Expand Down Expand Up @@ -100,6 +102,7 @@ export default function connectHierarchicalMenu(renderFn) {
refine: this._refine,
instantSearchInstance,
widgetParams,
currentRefinement: null,
}, true);
},

Expand All @@ -115,6 +118,10 @@ export default function connectHierarchicalMenu(renderFn) {
});
},

_findCurrentRefinement(items) {
return items.find(({isRefined}) => isRefined);
},

render({results, state, createURL, instantSearchInstance}) {
const items = this._prepareFacetValues(
results.getFacetValues(hierarchicalFacetName, {sortBy}).data || [],
Expand All @@ -132,6 +139,7 @@ export default function connectHierarchicalMenu(renderFn) {
refine: this._refine,
instantSearchInstance,
widgetParams,
currentRefinement: this._findCurrentRefinement(items),
}, false);
},
};
Expand Down

0 comments on commit 154cdb5

Please sign in to comment.