Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(range): don't go out of bounds with min or max given #4627

Merged
merged 8 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/connectors/range/__tests__/connectRange-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { createSearchClient } from '../../../../test/mock/createSearchClient';
import { createSingleSearchResponse } from '../../../../test/mock/createAPIResponse';
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
import instantsearch from '../../../lib/main';

function createFacetStatsResults({
helper,
Expand Down Expand Up @@ -1941,6 +1942,54 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
});
});

test('returns the `SearchParameters` with the correct price range', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
const helper = jsHelper(createSearchClient(), 'indexName', {
disjunctiveFacets: ['price'],
});
const widget = makeWidget({
attribute: 'price',
min: 0,
max: 500,
});

const actual = widget.getWidgetSearchParameters!(helper.state, {
uiState: {
range: {
price: '0:400',
},
},
});

expect(actual.numericRefinements.price['>=']).toEqual([0]);
expect(actual.numericRefinements.price['<=']).toEqual([400]);
});

test('ignores min or max from uiState if they are out of bound', () => {
const render = jest.fn();
const makeWidget = connectRange(render);
const helper = jsHelper(createSearchClient(), 'indexName', {
disjunctiveFacets: ['price'],
});
const widget = makeWidget({
attribute: 'price',
min: 0,
max: 500,
});

const actual = widget.getWidgetSearchParameters!(helper.state, {
uiState: {
range: {
price: '-20:600',
},
},
});

expect(actual.numericRefinements.price['>=']).toEqual([0]);
expect(actual.numericRefinements.price['<=']).toEqual([500]);
});

const attribute = 'price';

it('expect to return default configuration', () => {
Expand Down Expand Up @@ -2063,6 +2112,39 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input

expect(actual).toEqual(expectation);
});

it('passes the correct range set by initialUiState', () => {
const searchClient = createSearchClient();
const search = instantsearch({
indexName: 'test-index',
searchClient,
initialUiState: {
'test-index': {
range: {
price: '100:200',
},
},
},
});
const renderer = jest.fn();
const customRangeInput = connectRange(renderer);

search.addWidgets([
customRangeInput({
attribute: 'price',
min: 0,
max: 500,
}),
]);
search.start();

expect(renderer).toHaveBeenCalledWith(
expect.objectContaining({
start: [100, 200],
}),
true
);
});
});

describe('insights', () => {
Expand Down
18 changes: 16 additions & 2 deletions src/connectors/range/connectRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,29 @@ const connectRange: ConnectRange = function connectRange(

const [lowerBound, upperBound] = value.split(':').map(parseFloat);

if (isFiniteNumber(lowerBound)) {
if (
isFiniteNumber(lowerBound) &&
(!isFiniteNumber(minBound) || minBound < lowerBound)
) {
widgetSearchParameters = widgetSearchParameters.removeNumericRefinement(
attribute,
'>='
);
widgetSearchParameters = widgetSearchParameters.addNumericRefinement(
attribute,
'>=',
lowerBound
);
}

if (isFiniteNumber(upperBound)) {
if (
isFiniteNumber(upperBound) &&
(!isFiniteNumber(maxBound) || upperBound < maxBound)
) {
widgetSearchParameters = widgetSearchParameters.removeNumericRefinement(
attribute,
'<='
);
widgetSearchParameters = widgetSearchParameters.addNumericRefinement(
attribute,
'<=',
Expand Down