Skip to content

Commit 2c05a01

Browse files
samoussHaroenv
authored andcommitted
fix(numericMenu): take array into account for empty state (#4084)
This PR fixes a regression we introduced with the new behaviour of the Helper. We now preserve the empty state to avoid the parent to override the refinement when the widget is mounted. The `numericMenu` was relying on the previous behaviour to pick the "no value" element. We now check that every operator is empty. **Before** ![before](https://user-images.githubusercontent.com/6513513/64005002-c0235600-cb0f-11e9-80a1-74382cb47ebc.gif) **After** ![after](https://user-images.githubusercontent.com/6513513/64005037-cca7ae80-cb0f-11e9-888d-527676bff6b2.gif) You check the behaviour on the Storybook with [the `currentRefinement` stories](https://deploy-preview-4084--instantsearchjs.netlify.com/stories/?path=/story/currentrefinements--with-numericmenu).
1 parent 1d2a816 commit 2c05a01

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

src/connectors/numeric-menu/__tests__/connectNumericMenu-test.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,70 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
397397
});
398398
});
399399

400-
it('when the state is cleared, the "no value" value should be refined', () => {
400+
it('when only the refinement is cleared, the "no value" value should be refined', () => {
401+
const rendering = jest.fn();
402+
const makeWidget = connectNumericMenu(rendering);
403+
const listOptions = [
404+
{ label: 'below 10', end: 10 },
405+
{ label: '10 - 20', start: 10, end: 20 },
406+
{ label: 'more than 20', start: 20 },
407+
{ label: '42', start: 42, end: 42 },
408+
{ label: 'void' },
409+
];
410+
const widget = makeWidget({
411+
attribute: 'numerics',
412+
items: listOptions,
413+
});
414+
415+
const helper = jsHelper({});
416+
helper.search = jest.fn();
417+
418+
widget.init({
419+
helper,
420+
state: helper.state,
421+
createURL: () => '#',
422+
});
423+
424+
const refine =
425+
rendering.mock.calls[rendering.mock.calls.length - 1][0].refine;
426+
// a user selects a value in the refinement list
427+
refine(encodeValue(listOptions[0].start, listOptions[0].end));
428+
429+
widget.render({
430+
results: new SearchResults(helper.state, [{}]),
431+
state: helper.state,
432+
helper,
433+
createURL: () => '#',
434+
});
435+
436+
// No option should be selected
437+
const expectedResults0 = [...listOptions].map(mapOptionsToItems);
438+
expectedResults0[0].isRefined = true;
439+
440+
const renderingParameters0 =
441+
rendering.mock.calls[rendering.mock.calls.length - 1][0];
442+
expect(renderingParameters0.items).toEqual(expectedResults0);
443+
444+
// Only the current refinement is cleared by a third party
445+
helper.removeNumericRefinement('numerics', '<=', 10);
446+
447+
widget.render({
448+
results: new SearchResults(helper.state, [{}]),
449+
state: helper.state,
450+
helper,
451+
createURL: () => '#',
452+
});
453+
454+
// No option should be selected
455+
const expectedResults1 = [...listOptions].map(mapOptionsToItems);
456+
expectedResults1[4].isRefined = true;
457+
458+
const renderingParameters1 =
459+
rendering.mock.calls[rendering.mock.calls.length - 1][0];
460+
expect(renderingParameters1.items).toEqual(expectedResults1);
461+
});
462+
463+
it('when all the refinements are cleared, the "no value" value should be refined', () => {
401464
const rendering = jest.fn();
402465
const makeWidget = connectNumericMenu(rendering);
403466
const listOptions = [
@@ -442,11 +505,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
442505
expect(renderingParameters0.items).toEqual(expectedResults0);
443506

444507
// All the refinements are cleared by a third party
445-
helper.setState(
446-
helper.state.setQueryParameter('numericRefinements', {
447-
numerics: {},
448-
})
449-
);
508+
helper.clearRefinements('numerics');
450509

451510
widget.render({
452511
results: new SearchResults(helper.state, [{}]),

src/connectors/numeric-menu/connectNumericMenu.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export default function connectNumericMenu(renderFn, unmountFn = noop) {
130130

131131
this._createURL = state => facetValue =>
132132
createURL(refine(state, attribute, items, facetValue));
133+
133134
this._prepareItems = state =>
134135
items.map(({ start, end, label }) => ({
135136
label,
@@ -254,7 +255,9 @@ function isRefined(state, attribute, option) {
254255
}
255256

256257
if (option.start === undefined && option.end === undefined) {
257-
return Object.keys(currentRefinements).length === 0;
258+
return Object.keys(currentRefinements).every(
259+
operator => (currentRefinements[operator] || []).length === 0
260+
);
258261
}
259262

260263
return undefined;

0 commit comments

Comments
 (0)