Skip to content

Commit 1f8f7bf

Browse files
authored
[CI-4406] Bugfix: Range filters not correctly rendering if a range filter has already been applied (#159)
* bugifx: use the selected ranged filter values on refresh, rebase the track length if facets.min > 0 * add tests
1 parent d97466f commit 1f8f7bf

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

spec/components/Filters/Filters.test.jsx

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,101 @@ describe('Testing Component: Filters', () => {
116116
expect(mockChildren).toHaveBeenCalled();
117117
expect(getByText('Custom Filters')).toBeInTheDocument();
118118
});
119+
120+
it('Should render ranged filters correctly', async () => {
121+
const mockPriceFacet = {
122+
displayName: 'Price',
123+
name: 'price',
124+
type: 'range',
125+
data: {},
126+
hidden: false,
127+
min: 1,
128+
max: 100,
129+
status: {},
130+
};
131+
132+
const { getByText, container } = render(
133+
<CioPlp apiKey={DEMO_API_KEY}>
134+
<Filters facets={[mockPriceFacet]} />
135+
</CioPlp>,
136+
);
137+
138+
await waitFor(() => {
139+
expect(getByText(mockPriceFacet.displayName).toBeInTheDocument);
140+
141+
const minInputValue = container.querySelector('.cio-slider-input-min input');
142+
const maxInputValue = container.querySelector('.cio-slider-input-max input');
143+
144+
expect(minInputValue.value).toBe(mockPriceFacet.min.toString());
145+
expect(maxInputValue.value).toBe(mockPriceFacet.max.toString());
146+
147+
const selectableTrack = container.querySelector(
148+
'.cio-doubly-ended-slider .cio-slider-track-selected',
149+
);
150+
const minInputSlider = container.querySelector('.cio-doubly-ended-slider .cio-min-slider');
151+
const maxInputSlider = container.querySelector('.cio-doubly-ended-slider .cio-max-slider');
152+
153+
expect(selectableTrack.style.width).toBe('100.00%');
154+
expect(selectableTrack.style.left).toBe('0.00%');
155+
156+
expect(minInputSlider.min).toBe(mockPriceFacet.min.toString());
157+
expect(minInputSlider.max).toBe(mockPriceFacet.max.toString());
158+
expect(minInputSlider.value).toBe(mockPriceFacet.min.toString());
159+
160+
expect(maxInputSlider.min).toBe(mockPriceFacet.min.toString());
161+
expect(maxInputSlider.max).toBe(mockPriceFacet.max.toString());
162+
expect(maxInputSlider.value).toBe(mockPriceFacet.max.toString());
163+
});
164+
});
165+
166+
it('Should render ranged filters that have already been applied correctly', async () => {
167+
const mockPriceFacet = {
168+
displayName: 'Price',
169+
name: 'price',
170+
type: 'range',
171+
data: {},
172+
hidden: false,
173+
min: 1,
174+
max: 100,
175+
status: {
176+
min: 30,
177+
max: 75,
178+
},
179+
};
180+
181+
const { getByText, container } = render(
182+
<CioPlp apiKey={DEMO_API_KEY}>
183+
<Filters facets={[mockPriceFacet]} />
184+
</CioPlp>,
185+
);
186+
187+
await waitFor(() => {
188+
expect(getByText(mockPriceFacet.displayName).toBeInTheDocument);
189+
190+
const minInputValue = container.querySelector('.cio-slider-input-min input');
191+
const maxInputValue = container.querySelector('.cio-slider-input-max input');
192+
193+
expect(minInputValue.value).toBe(mockPriceFacet.status.min.toString());
194+
expect(maxInputValue.value).toBe(mockPriceFacet.status.max.toString());
195+
196+
const selectableTrack = container.querySelector(
197+
'.cio-doubly-ended-slider .cio-slider-track-selected',
198+
);
199+
const minInputSlider = container.querySelector('.cio-doubly-ended-slider .cio-min-slider');
200+
const maxInputSlider = container.querySelector('.cio-doubly-ended-slider .cio-max-slider');
201+
202+
expect(selectableTrack.style.width).toBe('45.45%');
203+
expect(selectableTrack.style.left).toBe('29.29%');
204+
205+
expect(minInputSlider.min).toBe(mockPriceFacet.min.toString());
206+
expect(minInputSlider.max).toBe(mockPriceFacet.max.toString());
207+
expect(minInputSlider.value).toBe(mockPriceFacet.status.min.toString());
208+
209+
expect(maxInputSlider.min).toBe(mockPriceFacet.min.toString());
210+
expect(maxInputSlider.max).toBe(mockPriceFacet.max.toString());
211+
expect(maxInputSlider.value).toBe(mockPriceFacet.status.max.toString());
212+
});
213+
});
119214
});
120215

121216
describe(' - Behavior Tests', () => {

src/components/Filters/FilterRangeSlider.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export default function FilterRangeSlider(props: FilterRangeSliderProps) {
3030

3131
const [minValue, setMinValue] = useState(facet.min);
3232
const [maxValue, setMaxValue] = useState(facet.max);
33-
const [inputMinValue, setInputMinValue] = useState<number | ''>(facet.min);
34-
const [inputMaxValue, setInputMaxValue] = useState<number | ''>(facet.max);
33+
const [inputMinValue, setInputMinValue] = useState<number | ''>(facet.status?.min || facet.min);
34+
const [inputMaxValue, setInputMaxValue] = useState<number | ''>(facet.status?.max || facet.max);
3535
const [filterRange, setFilterRange] = useState('');
3636

3737
const [isModified, setIsModified] = useState(false);
@@ -101,7 +101,7 @@ export default function FilterRangeSlider(props: FilterRangeSliderProps) {
101101
const totalWidth = visibleTrack.current!.offsetWidth;
102102
const clickedX = event.nativeEvent.offsetX;
103103

104-
const selectedValue = Math.round((clickedX / totalWidth) * facet.max);
104+
const selectedValue = Math.round((clickedX / totalWidth) * (facet.max - facet.min)) + facet.min;
105105
const distMinToClicked = Math.abs(selectedValue - minValue);
106106
const distMaxToClicked = Math.abs(selectedValue - maxValue);
107107

@@ -118,6 +118,8 @@ export default function FilterRangeSlider(props: FilterRangeSliderProps) {
118118
setInputMaxValue(selectedValue);
119119
modifyRequestRangeFilter(newRange);
120120
}
121+
122+
setIsModified(true);
121123
};
122124

123125
// Update internal state
@@ -136,8 +138,10 @@ export default function FilterRangeSlider(props: FilterRangeSliderProps) {
136138

137139
// Update selected track styles
138140
useEffect(() => {
139-
const startPercentage = ((100 * minValue) / facet.max).toFixed(2);
140-
const widthPercentage = ((100 * (maxValue - minValue)) / facet.max).toFixed(2);
141+
const trackLen = facet.max - facet.min;
142+
const rebasedStartValue = minValue - facet.min;
143+
const startPercentage = ((100 * rebasedStartValue) / trackLen).toFixed(2);
144+
const widthPercentage = ((100 * (maxValue - minValue)) / trackLen).toFixed(2);
141145

142146
setSelectedTrackStyles({ left: `${startPercentage}%`, width: `${widthPercentage}%` });
143147
}, [minValue, maxValue, facet]);

0 commit comments

Comments
 (0)