Skip to content

Commit 7c7e474

Browse files
committed
Add tests.
1 parent b083280 commit 7c7e474

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
@@ -248,18 +248,23 @@ describe('MdSlider', () => {
248248
let sliderDimensions: ClientRect;
249249
let trackFillElement: HTMLElement;
250250
let thumbElement: HTMLElement;
251+
let tickContainerElement: HTMLElement;
252+
let testComponent: SliderWithMinAndMax;
251253

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

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

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

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

@@ -640,11 +671,14 @@ class StandardSlider { }
640671
class DisabledSlider { }
641672

642673
@Component({
643-
template: `<md-slider min="4" max="6"></md-slider>`,
674+
template: `<md-slider [min]="min" [max]="max" tick-interval="6"></md-slider>`,
644675
styles: [noTransitionStyle],
645676
encapsulation: ViewEncapsulation.None
646677
})
647-
class SliderWithMinAndMax { }
678+
class SliderWithMinAndMax {
679+
min = 4;
680+
max = 6;
681+
}
648682

649683
@Component({
650684
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()
@@ -172,7 +174,8 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
172174
// This needs to be called after content init because the value can be set to the min if the
173175
// value itself isn't set. If this happens, the control value accessor needs to be updated.
174176
this._controlValueAccessorChangeFn(this.value);
175-
this._initTicksAndSlider();
177+
this.snapThumbToValue();
178+
this._updateTickSeparation();
176179
}
177180

178181
/** TODO: internal */
@@ -271,15 +274,6 @@ export class MdSlider implements AfterContentInit, ControlValueAccessor {
271274
}
272275
}
273276

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

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

0 commit comments

Comments
 (0)