Skip to content

Commit 50476ee

Browse files
authored
Merge pull request #14056 from IgniteUI/ganastasov/fix-13999-master
fix(slider): correct behavior of igx-slider in synchronized two-way binding - master
2 parents 5382a67 + 47e5792 commit 50476ee

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

projects/igniteui-angular/src/lib/slider/slider.component.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,61 @@ describe('IgxSlider', () => {
330330
fixture.detectChanges();
331331
expect(Math.round(slider.value as number)).toBe(60);
332332
});
333+
334+
it('should not set value if value is nullish but not zero', () => {
335+
spyOn(slider as any, 'isNullishButNotZero').and.returnValue(true);
336+
const setValueSpy = spyOn(slider, 'setValue');
337+
const positionHandlersAndUpdateTrackSpy = spyOn(slider as any, 'positionHandlersAndUpdateTrack');
338+
339+
slider.writeValue(null);
340+
fixture.detectChanges();
341+
342+
expect(setValueSpy).not.toHaveBeenCalled();
343+
expect(positionHandlersAndUpdateTrackSpy).not.toHaveBeenCalled();
344+
345+
slider.writeValue(undefined);
346+
fixture.detectChanges();
347+
348+
expect(setValueSpy).not.toHaveBeenCalled();
349+
expect(positionHandlersAndUpdateTrackSpy).not.toHaveBeenCalled();
350+
});
351+
352+
it('should set value and update track when value is not nullish and not zero', () => {
353+
spyOn(slider as any, 'isNullishButNotZero').and.returnValue(false);
354+
const setValueSpy = spyOn(slider, 'setValue');
355+
const positionHandlersAndUpdateTrackSpy = spyOn(slider as any, 'positionHandlersAndUpdateTrack');
356+
357+
const value = 10;
358+
slider.writeValue(value);
359+
fixture.detectChanges();
360+
361+
expect(setValueSpy).toHaveBeenCalledWith(value, false);
362+
expect(positionHandlersAndUpdateTrackSpy).toHaveBeenCalled();
363+
});
364+
365+
it('should normalize value by step', () => {
366+
spyOn(slider as any, 'isNullishButNotZero').and.returnValue(false);
367+
const normalizeByStepSpy = spyOn(slider as any, 'normalizeByStep');
368+
369+
const value = 10;
370+
slider.writeValue(value);
371+
fixture.detectChanges();
372+
373+
expect(normalizeByStepSpy).toHaveBeenCalledWith(value);
374+
});
375+
376+
it('should return true if value is null or undefined', () => {
377+
expect((slider as any).isNullishButNotZero(null)).toBe(true);
378+
expect((slider as any).isNullishButNotZero(undefined)).toBe(true);
379+
});
380+
381+
it('should return false if value is zero', () => {
382+
expect((slider as any).isNullishButNotZero(0)).toBe(false);
383+
});
384+
385+
it('should return false if value is not nullish and not zero', () => {
386+
expect((slider as any).isNullishButNotZero(10)).toBe(false);
387+
});
333388
});
334389

335390
describe('Slider: with set min and max value', () => {

projects/igniteui-angular/src/lib/slider/slider.component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ export class IgxSliderComponent implements
10721072
* @hidden
10731073
*/
10741074
public writeValue(value: IRangeSliderValue | number): void {
1075-
if (!value) {
1075+
if (this.isNullishButNotZero(value)) {
10761076
return;
10771077
}
10781078

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

1377+
private isNullishButNotZero(value: any): boolean {
1378+
return !value && value !== 0;
1379+
}
1380+
13771381
/**
13781382
* @hidden
13791383
* Normalizе the value when two-way data bind is used and {@link this.step} is set.

0 commit comments

Comments
 (0)