Skip to content

Commit ef56d2d

Browse files
committed
Add tests.
1 parent 9e896fb commit ef56d2d

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

src/lib/slider/slider.spec.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,23 @@ describe('MdSlider', () => {
249249
let sliderDimensions: ClientRect;
250250
let trackFillElement: HTMLElement;
251251
let thumbElement: HTMLElement;
252+
let tickContainerElement: HTMLElement;
253+
let testComponent: SliderWithMinAndMax;
252254

253255
beforeEach(() => {
254256
fixture = TestBed.createComponent(SliderWithMinAndMax);
255257
fixture.detectChanges();
256258

257259
sliderDebugElement = fixture.debugElement.query(By.directive(MdSlider));
260+
testComponent = fixture.debugElement.componentInstance;
258261
sliderNativeElement = sliderDebugElement.nativeElement;
259262
sliderInstance = sliderDebugElement.injector.get(MdSlider);
260263
sliderTrackElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-track');
261264
sliderDimensions = sliderTrackElement.getBoundingClientRect();
262265
trackFillElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-track-fill');
263266
thumbElement = <HTMLElement>sliderNativeElement.querySelector('.md-slider-thumb-position');
267+
tickContainerElement =
268+
<HTMLElement>sliderNativeElement.querySelector('.md-slider-tick-container');
264269
});
265270

266271
it('should set the default values from the attributes', () => {
@@ -312,7 +317,33 @@ describe('MdSlider', () => {
312317
// The closest snap is at the halfway point on the slider.
313318
expect(thumbDimensions.left).toBe(sliderDimensions.left + sliderDimensions.width * 0.5);
314319
expect(trackFillDimensions.width).toBe(thumbPosition);
320+
});
321+
322+
it('should adjust thumb and ticks when min changes', () => {
323+
testComponent.min = -2;
324+
fixture.detectChanges();
315325

326+
let trackFillDimensions = trackFillElement.getBoundingClientRect();
327+
let tickContainerDimensions = tickContainerElement.getBoundingClientRect();
328+
329+
expect(trackFillDimensions.width).toBe(sliderDimensions.width * 6 / 8);
330+
expect(tickContainerDimensions.width)
331+
.toBe(sliderDimensions.width - sliderDimensions.width * 6 / 8);
332+
});
333+
334+
it('should adjust thumb and ticks when max changes', () => {
335+
testComponent.min = -2;
336+
fixture.detectChanges();
337+
338+
testComponent.max = 10;
339+
fixture.detectChanges();
340+
341+
let trackFillDimensions = trackFillElement.getBoundingClientRect();
342+
let tickContainerDimensions = tickContainerElement.getBoundingClientRect();
343+
344+
expect(trackFillDimensions.width).toBe(sliderDimensions.width * 6 / 12);
345+
expect(tickContainerDimensions.width)
346+
.toBe(sliderDimensions.width - sliderDimensions.width * 6 / 12);
316347
});
317348
});
318349

@@ -689,11 +720,14 @@ class StandardSlider { }
689720
class DisabledSlider { }
690721

691722
@Component({
692-
template: `<md-slider min="4" max="6"></md-slider>`,
723+
template: `<md-slider [min]="min" [max]="max" tick-interval="6"></md-slider>`,
693724
styles: [noTransitionStyle],
694725
encapsulation: ViewEncapsulation.None
695726
})
696-
class SliderWithMinAndMax { }
727+
class SliderWithMinAndMax {
728+
min = 4;
729+
max = 6;
730+
}
697731

698732
@Component({
699733
template: `<md-slider value="26"></md-slider>`

src/lib/slider/slider.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
126126
if (!this._isInitialized) {
127127
this.value = this._min;
128128
}
129-
this._initTicksAndSlider();
129+
this.snapThumbToValue();
130+
this._updateTickSeparation();
130131
}
131132

132133
@Input()
@@ -137,7 +138,8 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
137138

138139
set max(v: number) {
139140
this._max = Number(v);
140-
this._initTicksAndSlider();
141+
this.snapThumbToValue();
142+
this._updateTickSeparation();
141143
}
142144

143145
@Input()
@@ -173,7 +175,8 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
173175
// This needs to be called after content init because the value can be set to the min if the
174176
// value itself isn't set. If this happens, the control value accessor needs to be updated.
175177
this._controlValueAccessorChangeFn(this.value);
176-
this._initTicksAndSlider();
178+
this.snapThumbToValue();
179+
this._updateTickSeparation();
177180
}
178181

179182
/** TODO: internal */
@@ -272,15 +275,6 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
272275
}
273276
}
274277

275-
/**
276-
* Initializes the tick and slider positions.
277-
* @private
278-
*/
279-
private _initTicksAndSlider() {
280-
this.snapThumbToValue();
281-
this._updateTickSeparation();
282-
}
283-
284278
/**
285279
* Calculates the separation in pixels of tick marks. If there is no tick interval or the interval
286280
* is set to something other than a number or 'auto', nothing happens.
@@ -458,11 +452,13 @@ export class SliderRenderer {
458452
lastTickContainer.style.background = `linear-gradient(to left, black, black 2px, transparent ` +
459453
`2px, transparent)`;
460454

461-
// If the second to last tick is too close (a separation of less than half the normal
462-
// separation), don't show it by decreasing the width of the tick container element.
463455
if (tickContainerWidth % tickSeparation < (tickSeparation / 2)) {
456+
// If the second to last tick is too close (a separation of less than half the normal
457+
// separation), don't show it by decreasing the width of the tick container element.
464458
tickContainer.style.width = tickContainerWidth - tickSeparation + 'px';
465459
} else {
460+
// If there is enough space for the second-to-last tick, restore the default width of the
461+
// tick container.
466462
tickContainer.style.width = '';
467463
}
468464
}

0 commit comments

Comments
 (0)