Skip to content

Commit

Permalink
feat(connectors): provide currentRefinement on menu
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 27, 2017
1 parent ec7806c commit fb7bc5e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/connectors/menu/__tests__/connectMenu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,51 @@ describe('connectMenu', () => {
},
]);
});

it('provides the correct `currentRefinement` value', () => {
const widget = makeWidget({attributeName: 'category'});

const helper = algoliasearchHelper(fakeClient, '', widget.getConfiguration({}));
helper.search = jest.fn();

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

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

expect(rendering.lastCall.args[0].currentRefinement).toBe(null);

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

expect(rendering.lastCall.args[0].currentRefinement).toEqual({
name: 'Decoration',
path: 'Decoration',
count: 880,
isRefined: true,
data: null,
});
});
});
4 changes: 4 additions & 0 deletions src/connectors/menu/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var customMenu = connectMenu(function render(params, isFirstRendering) {
// instantSearchInstance,
// canRefine,
// widgetParams,
// currentRefinement,
// }
});
search.addWidget(
Expand Down Expand Up @@ -39,6 +40,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
* @property {boolean} canRefine true if refinement can be applied
* @property {Object} widgetParams all original options forwarded to rendering
* @property {InstantSearch} instantSearchInstance the instance of instantsearch on which the widget is attached
* @property {Object} currentRefinement the refinement currently applied
*/

/**
Expand Down Expand Up @@ -92,6 +94,7 @@ export default function connectMenu(renderFn) {
instantSearchInstance,
canRefine: false,
widgetParams,
currentRefinement: null,
}, true);
},

Expand All @@ -105,6 +108,7 @@ export default function connectMenu(renderFn) {
instantSearchInstance,
canRefine: items.length > 0,
widgetParams,
currentRefinement: items.find(({isRefined}) => isRefined),
}, false);
},
};
Expand Down

0 comments on commit fb7bc5e

Please sign in to comment.