Skip to content

Commit

Permalink
Merge pull request #14055 from IgniteUI/ganastasov/fix-13999-16.1
Browse files Browse the repository at this point in the history
fix(slider): correct behavior of igx-slider in synchronized two-way binding - 16.1
  • Loading branch information
kdinev committed Apr 5, 2024
2 parents 61e20bb + 0ecff4c commit 6c77177
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
55 changes: 55 additions & 0 deletions projects/igniteui-angular/src/lib/slider/slider.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,61 @@ describe('IgxSlider', () => {
fixture.detectChanges();
expect(Math.round(slider.value as number)).toBe(60);
});

it('should not set value if value is nullish but not zero', () => {
spyOn(slider as any, 'isNullishButNotZero').and.returnValue(true);
const setValueSpy = spyOn(slider, 'setValue');
const positionHandlersAndUpdateTrackSpy = spyOn(slider as any, 'positionHandlersAndUpdateTrack');

slider.writeValue(null);
fixture.detectChanges();

expect(setValueSpy).not.toHaveBeenCalled();
expect(positionHandlersAndUpdateTrackSpy).not.toHaveBeenCalled();

slider.writeValue(undefined);
fixture.detectChanges();

expect(setValueSpy).not.toHaveBeenCalled();
expect(positionHandlersAndUpdateTrackSpy).not.toHaveBeenCalled();
});

it('should set value and update track when value is not nullish and not zero', () => {
spyOn(slider as any, 'isNullishButNotZero').and.returnValue(false);
const setValueSpy = spyOn(slider, 'setValue');
const positionHandlersAndUpdateTrackSpy = spyOn(slider as any, 'positionHandlersAndUpdateTrack');

const value = 10;
slider.writeValue(value);
fixture.detectChanges();

expect(setValueSpy).toHaveBeenCalledWith(value, false);
expect(positionHandlersAndUpdateTrackSpy).toHaveBeenCalled();
});

it('should normalize value by step', () => {
spyOn(slider as any, 'isNullishButNotZero').and.returnValue(false);
const normalizeByStepSpy = spyOn(slider as any, 'normalizeByStep');

const value = 10;
slider.writeValue(value);
fixture.detectChanges();

expect(normalizeByStepSpy).toHaveBeenCalledWith(value);
});

it('should return true if value is null or undefined', () => {
expect((slider as any).isNullishButNotZero(null)).toBe(true);
expect((slider as any).isNullishButNotZero(undefined)).toBe(true);
});

it('should return false if value is zero', () => {
expect((slider as any).isNullishButNotZero(0)).toBe(false);
});

it('should return false if value is not nullish and not zero', () => {
expect((slider as any).isNullishButNotZero(10)).toBe(false);
});
});

describe('Slider: with set min and max value', () => {
Expand Down
6 changes: 5 additions & 1 deletion projects/igniteui-angular/src/lib/slider/slider.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ export class IgxSliderComponent implements
* @hidden
*/
public writeValue(value: IRangeSliderValue | number): void {
if (!value) {
if (this.isNullishButNotZero(value)) {
return;
}

Expand Down Expand Up @@ -1374,6 +1374,10 @@ export class IgxSliderComponent implements
return this.valueInRange((value - this.minValue) / (this.maxValue - this.minValue), pMin, pMax);
}

private isNullishButNotZero(value: any): boolean {
return !value && value !== 0;
}

/**
* @hidden
* Normalizе the value when two-way data bind is used and {@link this.step} is set.
Expand Down

0 comments on commit 6c77177

Please sign in to comment.