Skip to content

Commit

Permalink
fix(module:slider): processing value zero correctly (#6729)
Browse files Browse the repository at this point in the history
  • Loading branch information
stygian-desolator authored Jun 7, 2021
1 parent f2e525c commit 62a86c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion components/slider/slider.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
getPrecision,
InputBoolean,
InputNumber,
isNil,
MouseTouchObserverConfig,
silentEvent
} from 'ng-zorro-antd/core/util';
Expand Down Expand Up @@ -536,7 +537,7 @@ export class NzSliderComponent implements ControlValueAccessor, OnInit, OnChange
}

private formatValue(value: NzSliderValue | null): NzSliderValue {
if (!value) {
if (isNil(value)) {
return this.nzRange ? [this.nzMin, this.nzMax] : this.nzMin;
} else if (assertValueValid(value, this.nzRange)) {
return isValueRange(value)
Expand Down
40 changes: 40 additions & 0 deletions components/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,38 @@ describe('nz-slider', () => {
});
});

describe('min and max and value is zero', () => {
let testBed: ComponentBed<SliderWithValueZeroComponent>;
let fixture: ComponentFixture<SliderWithValueZeroComponent>;
let trackFillElement: HTMLElement;

beforeEach(() => {
testBed = createComponentBed(SliderWithValueZeroComponent, {
imports: [NzSliderModule, FormsModule, ReactiveFormsModule, NoopAnimationsModule]
});
fixture = testBed.fixture;
fixture.detectChanges();

getReferenceFromFixture(fixture);
trackFillElement = sliderNativeElement.querySelector('.ant-slider-track') as HTMLElement;
});

it('should set the value equal to the middle value', () => {
fixture.whenStable().then(() => {
expect(sliderInstance.value).toBe(0);
expect(sliderInstance.nzMin).toBe(-5);
expect(sliderInstance.nzMax).toBe(5);
});
});

it('should set the fill to the middle value', () => {
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(trackFillElement.style.width).toBe('50%');
});
});
});

describe('vertical', () => {
let testBed: ComponentBed<VerticalSliderComponent>;
let fixture: ComponentFixture<VerticalSliderComponent>;
Expand Down Expand Up @@ -1040,6 +1072,14 @@ class SliderWithStepComponent {
})
class SliderWithValueSmallerThanMinComponent {}

@Component({
template: `
<nz-slider [ngModel]="0" [nzMin]="-5" [nzMax]="5"></nz-slider>
`,
styles: [styles]
})
class SliderWithValueZeroComponent {}

@Component({
template: `
<nz-slider [ngModel]="7" [nzMin]="4" [nzMax]="6"></nz-slider>
Expand Down

0 comments on commit 62a86c0

Please sign in to comment.