Skip to content

Commit

Permalink
fix(connectRangeSlider): correctly refine when no userSet min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Sep 20, 2017
1 parent 4602e05 commit 0dd17d3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/connectors/range-slider/__tests__/connectRangeSlider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,45 @@ describe('connectRangeSlider', () => {
expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined);
}
});

it('should refine on boundaries when no min/max defined', () => {
const rendering = sinon.stub();
const makeWidget = connectRangeSlider(rendering);

const attributeName = 'price';
const widget = makeWidget({ attributeName });

const helper = jsHelper(fakeClient, '', widget.getConfiguration());
helper.search = sinon.stub();

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

{
expect(helper.getNumericRefinement('price', '>=')).toEqual(undefined);
expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined);

const renderOptions = rendering.lastCall.args[0];
const { refine } = renderOptions;

refine([undefined, 100]);
expect(helper.getNumericRefinement('price', '>=')).toEqual(undefined);
expect(helper.getNumericRefinement('price', '<=')).toEqual([100]);
expect(helper.search.callCount).toBe(1);

refine([0, undefined]);
expect(helper.getNumericRefinement('price', '>=')).toEqual([0]);
expect(helper.getNumericRefinement('price', '<=')).toEqual(undefined);
expect(helper.search.callCount).toBe(2);

refine([0, 100]);
expect(helper.getNumericRefinement('price', '>=')).toEqual([0]);
expect(helper.getNumericRefinement('price', '<=')).toEqual([100]);
expect(helper.search.callCount).toBe(3);
}
});
});
20 changes: 14 additions & 6 deletions src/connectors/range-slider/connectRangeSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,29 @@ export default function connectRangeSlider(renderFn) {
currentValues[1] !== newValues[1]
) {
helper.clearRefinements(attributeName);

const hasMin = bounds.min !== null && bounds.min !== undefined;
const minValueChanged =
newValues[0] !== null && newValues[0] !== undefined;

if (
bounds.min === null ||
bounds.min === undefined ||
newValues[0] > bounds.min
(hasMin && minValueChanged && bounds.min < newValues[0]) ||
(!hasMin && minValueChanged)
) {
helper.addNumericRefinement(
attributeName,
'>=',
formatToNumber(newValues[0])
);
}

const hasMax = bounds.max !== null && bounds.max !== undefined;
const maxValueChanged =
newValues[1] !== null && newValues[1] !== undefined;

if (
bounds.max === null ||
bounds.max === undefined ||
newValues[1] < bounds.max
(hasMax && maxValueChanged && bounds.max > newValues[1]) ||
(!hasMax && maxValueChanged)
) {
helper.addNumericRefinement(
attributeName,
Expand Down

0 comments on commit 0dd17d3

Please sign in to comment.