Skip to content

Commit

Permalink
feat(connectors): provide currentRefinement on numeric refinement list
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 27, 2017
1 parent 2bcf991 commit 91f7928
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,55 @@ describe('connectNumericRefinementList', () => {
const renderingParameters1 = rendering.lastCall.args[0];
expect(renderingParameters1.items).toEqual(expectedResults1);
});

it('provides the correct `currentRefinement` value', () => {
const rendering = jest.fn();
const makeWidget = connectNumericRefinementList(rendering);
const listOptions = [
{name: 'below 10', end: 10},
{name: '10 - 20', start: 10, end: 20},
{name: 'more than 20', start: 20},
{name: '42', start: 42, end: 42},
{name: 'void'},
];
const widget = makeWidget({
attributeName: 'numerics',
options: listOptions,
});

const helper = jsHelper(fakeClient);
helper.search = jest.fn();

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

const [[firstRenderingOptions]] = rendering.mock.calls;

expect(rendering).toBeCalled();
expect(firstRenderingOptions.currentRefinement).toEqual({
attributeName: 'numerics',
isRefined: true,
name: 'void',
});

firstRenderingOptions.refine(listOptions[0].name);

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

const [, [secondRenderingOptions]] = rendering.mock.calls;
expect(secondRenderingOptions.currentRefinement).toEqual({
attributeName: 'numerics',
isRefined: true,
...listOptions[0],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var customNumericRefinementList = connectNumericRefinementList(function renderFn
// refine,
// instantSearchInstance,
// widgetParams,
// currentRefinement,
// }
});
search.addWidget(
Expand Down Expand Up @@ -45,6 +46,7 @@ Full documentation available at https://community.algolia.com/instantsearch.js/c
* @property {function(string)} refine set the selected value and trigger a new search
* @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 @@ -91,8 +93,10 @@ export default function connectNumericRefinementList(renderFn) {
refine: this._refine,
instantSearchInstance,
widgetParams,
currentRefinement: this._findCurrentRefinement(items),
}, true);
},

render({results, state, instantSearchInstance}) {
const items = options.map(facetValue =>
({
Expand All @@ -109,8 +113,13 @@ export default function connectNumericRefinementList(renderFn) {
refine: this._refine,
instantSearchInstance,
widgetParams,
currentRefinement: this._findCurrentRefinement(items),
}, false);
},

_findCurrentRefinement(items) {
return items.find(({isRefined: itemIsRefined}) => itemIsRefined);
},
};
};
}
Expand Down

0 comments on commit 91f7928

Please sign in to comment.