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(slider): should round value when dateField=true #1968

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ui/Misc/Slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ export class SliderButton {

public getValue() {
const value = this.getPercent() * (this.slider.options.end - this.slider.options.start) + this.slider.options.start;
if (this.slider.options.dateField) {
return Math.round(value);
}
return value;
}

Expand Down
31 changes: 31 additions & 0 deletions unitTests/ui/SliderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ export function SliderTest() {
expect(slider.getValues()).toEqual([25, 75]);
});

it('should return round values when using dateField', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests here are a bit lazy and do not really respect encapsulation, but I think it's fine because this allow the test to get to the point and test actually what's wrong without having over the top setup.

slider = new Slider(
el,
{
start: 0,
end: 100,
rangeSlider: true,
dateField: true
},
root
);
slider.element.style.width = '100px';
slider['sliderRange']['firstButton'].getPercent = () => 1 / 3;
expect(slider.getValues()[0]).toEqual(33);
});

it('should not round values when using dateField', () => {
slider = new Slider(
el,
{
start: 0,
end: 100,
rangeSlider: true
},
root
);
slider.element.style.width = '100px';
slider['sliderRange']['firstButton'].getPercent = () => 1 / 3;
expect(slider.getValues()[0]).toBeCloseTo(33.33, 2);
});

it('should give position when initializing state with no value', () => {
expect(() => slider.initializeState()).not.toThrow();
expect(() => slider.getPosition()).not.toThrow();
Expand Down