Skip to content

Commit b314160

Browse files
Alexandre Stanislawskivvo
Alexandre Stanislawski
authored and
vvo
committed
fix(numeric widgets): synchronizes rounded value between widgets
1 parent ad6f5c2 commit b314160

File tree

4 files changed

+88
-53
lines changed

4 files changed

+88
-53
lines changed

npm-shrinkwrap.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

widgets/price-ranges/price-ranges.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ function priceRanges({
152152
let newState = state.clearRefinements(attributeName);
153153
if (!isRefined) {
154154
if (typeof from !== 'undefined') {
155-
newState = newState.addNumericRefinement(attributeName, '>', from - 1);
155+
newState = newState.addNumericRefinement(attributeName, '>=', Math.floor(from));
156156
}
157157
if (typeof to !== 'undefined') {
158-
newState = newState.addNumericRefinement(attributeName, '<', to + 1);
158+
newState = newState.addNumericRefinement(attributeName, '<=', Math.ceil(to));
159159
}
160160
}
161161
return createURL(newState);

widgets/range-slider/__tests__/range-slider-test.js

+73-33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import jsdom from 'mocha-jsdom';
88
import expectJSX from 'expect-jsx';
99
expect.extend(expectJSX);
1010

11+
import AlgoliasearchHelper from 'algoliasearch-helper';
12+
1113
describe('rangeSlider()', () => {
1214
jsdom({useEach: true});
1315

@@ -35,21 +37,47 @@ describe('rangeSlider()', () => {
3537
container = document.createElement('div');
3638
widget = rangeSlider({container, attributeName: 'aNumAttr'});
3739
results = {
38-
getFacetStats: sinon.stub().returns({
39-
min: 1.99,
40-
max: 4999.98,
41-
avg: 243.349,
42-
sum: 2433490.0
43-
})
44-
};
45-
helper = {
46-
state: {
47-
getNumericRefinement: sinon.stub().returns([])
48-
},
49-
addNumericRefinement: sinon.spy(),
50-
clearRefinements: sinon.spy(),
51-
search: sinon.spy()
40+
disjunctiveFacets: [{
41+
name: 'aNumAttr',
42+
data: {
43+
19.99: 610,
44+
39.99: 593,
45+
29.99: 488,
46+
49.99: 486,
47+
99.99: 430,
48+
14.99: 376,
49+
59.99: 323,
50+
34.99: 286,
51+
79.99: 282,
52+
9.99: 277,
53+
599.99: 105,
54+
999.99: 104,
55+
799.99: 96,
56+
899.99: 88,
57+
699.99: 84,
58+
1099.99: 53,
59+
1199.99: 49,
60+
649.99: 48,
61+
1299.99: 46,
62+
749.99: 34
63+
},
64+
exhaustive: true,
65+
stats: {
66+
min: 1.99,
67+
max: 4999.98,
68+
avg: 243.349,
69+
sum: 2433490
70+
}
71+
}]
5272
};
73+
helper = new AlgoliasearchHelper(
74+
{search: function() {}},
75+
'indexName',
76+
{disjunctiveFacets: ['aNumAttr']}
77+
);
78+
sinon.spy(helper, 'addNumericRefinement');
79+
sinon.spy(helper, 'clearRefinements');
80+
sinon.spy(helper, 'search');
5381
});
5482

5583
it('configures the disjunctiveFacets', () => {
@@ -87,38 +115,50 @@ describe('rangeSlider()', () => {
87115
});
88116

89117
it('doesn\'t call the refinement functions if not refined', () => {
118+
let state0 = helper.state;
90119
widget.render({results, helper});
91-
expect(helper.state.getNumericRefinement.calledTwice).toBe(true, 'getNumericRefinement called once');
92-
expect(helper.clearRefinements.called).toBe(false, 'clearRefinements never called');
93-
expect(helper.addNumericRefinement.called).toBe(false, 'addNumericRefinement never called');
120+
let state1 = helper.state;
121+
expect(state1).toEqual(state0);
94122
expect(helper.search.called).toBe(false, 'search never called');
95123
});
96124

97125
it('calls the refinement functions if refined with min+1', () => {
98-
let stats = results.getFacetStats();
99-
widget._refine(helper, stats, [stats.min + 1, stats.max]);
100-
expect(helper.clearRefinements.calledOnce).toBe(true, 'clearRefinements called once');
101-
expect(helper.addNumericRefinement.calledOnce).toBe(true, 'clearRefinements called once');
102-
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aNumAttr', '>=', stats.min + 1]);
126+
let stats = results.disjunctiveFacets[0].stats;
127+
let targetValue = Math.floor(stats.min) + 1;
128+
129+
let state0 = helper.state;
130+
widget._refine(helper, stats, [targetValue, stats.max]);
131+
let state1 = helper.state;
132+
103133
expect(helper.search.calledOnce).toBe(true, 'search called once');
134+
expect(state1).toEqual(state0.addNumericRefinement('aNumAttr', '>=', targetValue));
104135
});
105136

106137
it('calls the refinement functions if refined with max-1', () => {
107-
let stats = results.getFacetStats();
108-
widget._refine(helper, stats, [stats.min, stats.max - 1]);
109-
expect(helper.clearRefinements.calledOnce).toBe(true, 'clearRefinements called once');
110-
expect(helper.addNumericRefinement.calledOnce).toBe(true, 'addNumericRefinement called once');
111-
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aNumAttr', '<=', stats.max - 1]);
138+
let stats = results.disjunctiveFacets[0].stats;
139+
let targetValue = Math.ceil(stats.max) - 1;
140+
141+
let state0 = helper.state;
142+
widget._refine(helper, stats, [stats.min, targetValue]);
143+
let state1 = helper.state;
144+
112145
expect(helper.search.calledOnce).toBe(true, 'search called once');
146+
expect(state1).toEqual(state0.addNumericRefinement('aNumAttr', '<=', targetValue));
113147
});
114148

115149
it('calls the refinement functions if refined with min+1 and max-1', () => {
116-
let stats = results.getFacetStats();
117-
widget._refine(helper, stats, [stats.min + 1, stats.max - 1]);
118-
expect(helper.clearRefinements.calledOnce).toBe(true, 'clearRefinements called once');
119-
expect(helper.addNumericRefinement.calledTwice).toBe(true, 'addNumericRefinement called twice');
120-
expect(helper.addNumericRefinement.getCall(0).args).toEqual(['aNumAttr', '>=', stats.min + 1]);
121-
expect(helper.addNumericRefinement.getCall(1).args).toEqual(['aNumAttr', '<=', stats.max - 1]);
150+
let stats = results.disjunctiveFacets[0].stats;
151+
let targetValue = [Math.floor(stats.min) + 1, Math.ceil(stats.max) - 1];
152+
153+
let state0 = helper.state;
154+
widget._refine(helper, stats, targetValue);
155+
let state1 = helper.state;
156+
157+
let expectedState = state0.
158+
addNumericRefinement('aNumAttr', '>=', targetValue[0]).
159+
addNumericRefinement('aNumAttr', '<=', targetValue[1]);
160+
161+
expect(state1).toEqual(expectedState);
122162
expect(helper.search.calledOnce).toBe(true, 'search called once');
123163
});
124164

widgets/range-slider/range-slider.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,18 @@ function rangeSlider({
7676
_refine(helper, stats, newValues) {
7777
helper.clearRefinements(attributeName);
7878
if (newValues[0] > stats.min) {
79-
helper.addNumericRefinement(attributeName, '>=', newValues[0]);
79+
helper.addNumericRefinement(attributeName, '>=', Math.round(newValues[0]));
8080
}
8181
if (newValues[1] < stats.max) {
82-
helper.addNumericRefinement(attributeName, '<=', newValues[1]);
82+
helper.addNumericRefinement(attributeName, '<=', Math.round(newValues[1]));
8383
}
8484
helper.search();
8585
},
86-
getDisjunctiveFacetPosition(results, facetName) {
87-
let facets = results.disjunctiveFacets;
88-
for (let i = 0; facets.length; i++) {
89-
if (facets[i].name === facetName) return i;
90-
}
91-
return -1;
92-
},
9386
render({results, helper, templatesConfig}) {
94-
let facetPosition = this.getDisjunctiveFacetPosition(results, attributeName);
95-
let stats = results.disjunctiveFacets[facetPosition].stats;
87+
let facet = results.disjunctiveFacets.find(
88+
function(f) { return f.name === attributeName; }
89+
);
90+
let stats = facet.stats;
9691
let currentRefinement = this._getCurrentRefinement(helper);
9792

9893
if (stats === undefined) {

0 commit comments

Comments
 (0)