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): constraints value between min and max #1567

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a53a129
fix(slider): constraints value between min and max
feloy Oct 22, 2016
da2af1e
chore: add gulp-cli as dev dep (#1587)
jelbourn Oct 24, 2016
c9ef34c
docs(dialog): document MdDialog (#1569)
feloy Oct 25, 2016
920c875
fix(radio): only call change callback with user input (#1521)
kara Oct 25, 2016
ffbc295
feat(overlay): set overlay size (#1583)
kara Oct 25, 2016
1ad457b
chore: update sauce connect binary to latest version (#1563)
jelbourn Oct 25, 2016
43786b8
chore(getting-started): make theming section more attention-grabbing.…
jelbourn Oct 25, 2016
35f0044
chore(packages): add http module to package.json (#1579)
DAB0mB Oct 25, 2016
333b11e
feat(checkbox): add color attribute. (#1463)
mathebox Oct 25, 2016
6f322cf
feat(overlay): support all overlay config properties (#1591)
kara Oct 25, 2016
c0e6f83
chore: update @angular/compiler-cli version (#1597)
jelbourn Oct 25, 2016
b56f520
feat(overlay): support rtl in overlays (#1593)
kara Oct 25, 2016
c6b4c22
chore(menu): move common styles into core (#1600)
kara Oct 25, 2016
9422793
chore(select): finish scaffolding (#1601)
kara Oct 25, 2016
dbfd235
fix(slider): clamp displayed thumb instead of value between min and max
feloy Oct 25, 2016
2e651e7
test(radio): add e2e test for radio button (#1582)
tinayuangao Oct 25, 2016
95b2a34
feat(a11y): manager for list keyboard events (#1599)
kara Oct 25, 2016
8e16992
chore: fix firebase dev instance (#1606)
jelbourn Oct 25, 2016
2ebb46f
chore(build): fix build to update components (#1604)
kara Oct 25, 2016
f0e3e26
fix(slider): tests
feloy Oct 26, 2016
fdc0ac8
fix(slider): remove unused test class
feloy Oct 26, 2016
65401a3
chore: snackbar tests should clean up afterwards (#1609)
jelbourn Oct 26, 2016
9eaf7e4
chore(checkbox): add e2e tests (#1602)
tinayuangao Oct 26, 2016
c63b9f4
chore: remove BooleanFieldValue (#1290)
jelbourn Oct 26, 2016
f10ac7c
fix(button): set vertical alignment for md-button and md-raised-butto…
tinayuangao Oct 26, 2016
8e7f80d
fix(slider): update thumb position when value changes. Closes #1386 (…
mmalerba Oct 26, 2016
8c1a791
fix(slider): constraints value between min and max
feloy Oct 22, 2016
e33360d
fix(slider): clamp displayed thumb instead of value between min and max
feloy Oct 25, 2016
86758f3
fix(slider): tests
feloy Oct 26, 2016
f43bacf
fix(slider): remove unused test class
feloy Oct 26, 2016
4570998
Merge branch 'slider-1557' of https://github.com/feloy/material2 into…
feloy Oct 26, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/lib/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('MdSlider', () => {
SliderWithSetTickInterval,
SliderWithThumbLabel,
SliderWithTwoWayBinding,
SliderWithValueSmallerThanMin,
SliderWithValueGreaterThanMax,
SliderWithValueBetweenMinAndMax
],
providers: [
{provide: HAMMER_GESTURE_CONFIG, useFactory: () => {
Expand Down Expand Up @@ -621,6 +624,102 @@ describe('MdSlider', () => {

// TODO: Add tests for ng-pristine, ng-touched, ng-invalid.
});

describe('slider with set min and max and a value smaller than min', () => {
let fixture: ComponentFixture<SliderWithValueSmallerThanMin>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MdSlider;
let sliderTrackElement: HTMLElement;
let sliderDimensions: ClientRect;
let thumbElement: HTMLElement;
let thumbDimensions: ClientRect;

beforeEach(() => {

fixture = TestBed.createComponent(SliderWithValueSmallerThanMin);
fixture.detectChanges();

sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider));
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;

sliderTrackElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-track');
sliderDimensions = sliderTrackElement.getBoundingClientRect();

thumbElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-thumb-position');
thumbDimensions = thumbElement.getBoundingClientRect();
});

it('should set the value smaller than the min value', () => {
expect(sliderInstance.value).toBe(3);
expect(sliderInstance.min).toBe(4);
expect(sliderInstance.max).toBe(6);
});

it('should place the thumb on the min value', () => {
thumbDimensions = thumbElement.getBoundingClientRect();
expect(thumbDimensions.left).toBe(sliderDimensions.width * 0.0 + sliderDimensions.left);
Copy link
Member

Choose a reason for hiding this comment

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

This can just be

expect(thumbDimensions.left).toBe(sliderDimensions.left);

});
});

describe('slider with set min and max and a value greater than max', () => {
let fixture: ComponentFixture<SliderWithValueSmallerThanMin>;
let sliderDebugElement: DebugElement;
let sliderNativeElement: HTMLElement;
let sliderInstance: MdSlider;
let sliderTrackElement: HTMLElement;
let sliderDimensions: ClientRect;
let thumbElement: HTMLElement;
let thumbDimensions: ClientRect;

beforeEach(() => {
fixture = TestBed.createComponent(SliderWithValueGreaterThanMax);
fixture.detectChanges();

sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider));
sliderNativeElement = sliderDebugElement.nativeElement;
sliderInstance = sliderDebugElement.componentInstance;

sliderTrackElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-track');
sliderDimensions = sliderTrackElement.getBoundingClientRect();

thumbElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-thumb-position');
thumbDimensions = thumbElement.getBoundingClientRect();

});

it('should set the value greater than the max value', () => {
expect(sliderInstance.value).toBe(7);
expect(sliderInstance.min).toBe(4);
expect(sliderInstance.max).toBe(6);
});

it('should place the thumb on the max value', () => {
thumbDimensions = thumbElement.getBoundingClientRect();
expect(thumbDimensions.left).toBe(sliderDimensions.width * 1.0 + sliderDimensions.left);
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to do

expect(thumbDimensions.left).toBe(sliderDimensions.right);

});
});

describe('slider with set min and max and a value between min and max', () => {
Copy link
Member

Choose a reason for hiding this comment

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

This behavior is already captured by existing unit tests in the "slider with set min and max" section.

let fixture: ComponentFixture<SliderWithValueBetweenMinAndMax>;
let sliderDebugElement: DebugElement;
let sliderInstance: MdSlider;

beforeEach(() => {
fixture = TestBed.createComponent(SliderWithValueBetweenMinAndMax);
fixture.detectChanges();

sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider));
sliderInstance = sliderDebugElement.injector.get(MdSlider);
});

it('should set the value to the max value', () => {
expect(sliderInstance.value).toBe(5);
expect(sliderInstance.min).toBe(4);
expect(sliderInstance.max).toBe(6);
});
});
});

// The transition has to be removed in order to test the updated positions without setTimeout.
Expand Down Expand Up @@ -678,6 +777,27 @@ class SliderWithTwoWayBinding {
control = new FormControl('');
}

@Component({
template: `<md-slider value="3" min="4" max="6"></md-slider>`,
styles: [noTransitionStyle],
encapsulation: ViewEncapsulation.None
})
class SliderWithValueSmallerThanMin { }

@Component({
template: `<md-slider value="7" min="4" max="6"></md-slider>`,
styles: [noTransitionStyle],
encapsulation: ViewEncapsulation.None
})
class SliderWithValueGreaterThanMax { }

@Component({
template: `<md-slider value="5" min="4" max="6"></md-slider>`,
styles: [noTransitionStyle],
encapsulation: ViewEncapsulation.None
})
class SliderWithValueBetweenMinAndMax { }
Copy link
Member

Choose a reason for hiding this comment

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

You don't need this component any more.


/**
* Dispatches a click event from an element.
* Note: The mouse event truncates the position for the click.
Expand Down
3 changes: 2 additions & 1 deletion src/lib/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
*/
snapThumbToValue() {
this.updatePercentFromValue();
this._renderer.updateThumbAndFillPosition(this._percent, this._sliderDimensions.width);
let renderedPercent = this.clamp(this._percent);
this._renderer.updateThumbAndFillPosition(renderedPercent, this._sliderDimensions.width);
}

/**
Expand Down