Skip to content

Commit a99ab6f

Browse files
authoredMar 30, 2021
feat(range): implement canRefine (#4686)
DX-1307
1 parent 4e8d793 commit a99ab6f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
 

‎src/connectors/range/__tests__/connectRange-test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
15991599
max: 0,
16001600
min: 0,
16011601
},
1602+
canRefine: false,
16021603
refine: expect.any(Function),
16031604
sendEvent: expect.any(Function),
16041605
start: [0, 1000],
@@ -1633,6 +1634,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
16331634
min: 10,
16341635
max: 30,
16351636
},
1637+
canRefine: true,
16361638
refine: expect.any(Function),
16371639
sendEvent: expect.any(Function),
16381640
start: [0, 1000],
@@ -1676,6 +1678,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
16761678
max: 0,
16771679
min: 0,
16781680
},
1681+
canRefine: false,
16791682
refine: expect.any(Function),
16801683
sendEvent: expect.any(Function),
16811684
start: [0, 1000],
@@ -1707,6 +1710,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
17071710
max: 30,
17081711
min: 10,
17091712
},
1713+
canRefine: true,
17101714
refine: expect.any(Function),
17111715
sendEvent: expect.any(Function),
17121716
start: [0, 1000],
@@ -1716,6 +1720,50 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
17161720
},
17171721
});
17181722
});
1723+
1724+
it('canRefine returns false when the result range is empty', () => {
1725+
const renderFn = jest.fn();
1726+
const unmountFn = jest.fn();
1727+
const createRange = connectRange(renderFn, unmountFn);
1728+
const rangeWidget = createRange({
1729+
attribute: 'price',
1730+
});
1731+
const helper = jsHelper(createSearchClient(), 'indexName', {
1732+
disjunctiveFacets: ['price'],
1733+
});
1734+
1735+
const renderState = rangeWidget.getWidgetRenderState(
1736+
createRenderOptions({
1737+
helper,
1738+
state: helper.state,
1739+
results: createFacetStatsResults({
1740+
min: 1000,
1741+
max: 1000,
1742+
helper,
1743+
attribute: 'price',
1744+
}),
1745+
})
1746+
);
1747+
1748+
expect(renderState).toEqual({
1749+
format: {
1750+
from: expect.any(Function),
1751+
to: expect.any(Function),
1752+
},
1753+
range: {
1754+
max: 1000,
1755+
min: 1000,
1756+
},
1757+
canRefine: false,
1758+
refine: expect.any(Function),
1759+
sendEvent: expect.any(Function),
1760+
start: [-Infinity, Infinity],
1761+
widgetParams: {
1762+
attribute: 'price',
1763+
precision: 0,
1764+
},
1765+
});
1766+
});
17191767
});
17201768

17211769
describe('getWidgetSearchParameters', () => {

‎src/connectors/range/connectRange.ts

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export type RangeRendererOptions = {
4141
*/
4242
refine(rangeValue: RangeBoundaries): void;
4343

44+
/**
45+
* Indicates whether this widget can be refined
46+
*/
47+
canRefine: boolean;
48+
4449
/**
4550
* Send an event to the insights middleware
4651
*/
@@ -401,6 +406,7 @@ const connectRange: ConnectRange = function connectRange(
401406

402407
return {
403408
refine,
409+
canRefine: currentRange.min !== currentRange.max,
404410
format: rangeFormatter,
405411
range: currentRange,
406412
sendEvent: createSendEvent(

0 commit comments

Comments
 (0)
Please sign in to comment.