Skip to content

Commit d87364f

Browse files
authored
feat(material-experimental/mdc-slider): add unit tests for disabled slider (#22168)
1 parent cbc9ca0 commit d87364f

File tree

1 file changed

+119
-15
lines changed

1 file changed

+119
-15
lines changed

src/material-experimental/mdc-slider/slider.spec.ts

Lines changed: 119 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,91 @@ describe('MDC-based MatSlider' , () => {
151151
});
152152
});
153153

154+
describe('disabled slider', () => {
155+
let sliderInstance: MatSlider;
156+
let inputInstance: MatSliderThumb;
157+
158+
beforeEach(waitForAsync(() => {
159+
const fixture = createComponent(DisabledSlider);
160+
fixture.detectChanges();
161+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
162+
sliderInstance = sliderDebugElement.componentInstance;
163+
inputInstance = sliderInstance._getInput(Thumb.END);
164+
}));
165+
166+
it('should be disabled', () => {
167+
expect(sliderInstance.disabled).toBeTrue();
168+
});
169+
170+
it('should have the disabled class on the root element', () => {
171+
expect(sliderInstance._elementRef.nativeElement.classList).toContain('mdc-slider--disabled');
172+
});
173+
174+
it('should set the disabled attribute on the input element', () => {
175+
expect(inputInstance._hostElement.disabled).toBeTrue();
176+
});
177+
178+
it('should not update the value on mousedown', () => {
179+
setValueByClick(sliderInstance, 19, platform.IOS);
180+
expect(inputInstance.value).toBe(0);
181+
});
182+
183+
it('should not update the value on a slide', () => {
184+
slideToValue(sliderInstance, 77, Thumb.END, platform.IOS);
185+
expect(inputInstance.value).toBe(0);
186+
});
187+
});
188+
189+
describe('disabled range slider', () => {
190+
let sliderInstance: MatSlider;
191+
let startInputInstance: MatSliderThumb;
192+
let endInputInstance: MatSliderThumb;
193+
194+
beforeEach(waitForAsync(() => {
195+
const fixture = createComponent(DisabledRangeSlider);
196+
fixture.detectChanges();
197+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
198+
sliderInstance = sliderDebugElement.componentInstance;
199+
startInputInstance = sliderInstance._getInput(Thumb.START);
200+
endInputInstance = sliderInstance._getInput(Thumb.END);
201+
}));
202+
203+
it('should be disabled', () => {
204+
expect(sliderInstance.disabled).toBeTrue();
205+
});
206+
207+
it('should have the disabled class on the root element', () => {
208+
expect(sliderInstance._elementRef.nativeElement.classList).toContain('mdc-slider--disabled');
209+
});
210+
211+
it('should set the disabled attribute on the input elements', () => {
212+
expect(startInputInstance._hostElement.disabled).toBeTrue();
213+
expect(endInputInstance._hostElement.disabled).toBeTrue();
214+
});
215+
216+
it('should not update the start value on a slide', () => {
217+
slideToValue(sliderInstance, 19, Thumb.START, platform.IOS);
218+
expect(startInputInstance.value).toBe(0);
219+
});
220+
221+
it('should not update the end value on a slide', () => {
222+
slideToValue(sliderInstance, 27, Thumb.END, platform.IOS);
223+
expect(endInputInstance.value).toBe(100);
224+
});
225+
226+
it('should not update the start value on mousedown behind the start thumb', () => {
227+
sliderInstance._setValue(19, Thumb.START);
228+
setValueByClick(sliderInstance, 12, platform.IOS);
229+
expect(startInputInstance.value).toBe(19);
230+
});
231+
232+
it('should update the end value on mousedown in front of the end thumb', () => {
233+
sliderInstance._setValue(27, Thumb.END);
234+
setValueByClick(sliderInstance, 55, platform.IOS);
235+
expect(endInputInstance.value).toBe(27);
236+
});
237+
});
238+
154239
describe('ripple states', () => {
155240
let fixture: ComponentFixture<StandardSlider>;
156241
let inputInstance: MatSliderThumb;
@@ -202,81 +287,81 @@ describe('MDC-based MatSlider' , () => {
202287
}
203288

204289
it('should show the hover ripple on mouseenter', fakeAsync(() => {
205-
expect(isRippleVisible('hover')).toBe(false);
290+
expect(isRippleVisible('hover')).toBeFalse();
206291
mouseenter();
207-
expect(isRippleVisible('hover')).toBe(true);
292+
expect(isRippleVisible('hover')).toBeTrue();
208293
}));
209294

210295
it('should hide the hover ripple on mouseleave', fakeAsync(() => {
211296
mouseenter();
212297
mouseleave();
213-
expect(isRippleVisible('hover')).toBe(false);
298+
expect(isRippleVisible('hover')).toBeFalse();
214299
}));
215300

216301
it('should show the focus ripple on pointerdown', fakeAsync(() => {
217-
expect(isRippleVisible('focus')).toBe(false);
302+
expect(isRippleVisible('focus')).toBeFalse();
218303
pointerdown();
219-
expect(isRippleVisible('focus')).toBe(true);
304+
expect(isRippleVisible('focus')).toBeTrue();
220305
}));
221306

222307
it('should continue to show the focus ripple on pointerup', fakeAsync(() => {
223308
pointerdown();
224309
pointerup();
225-
expect(isRippleVisible('focus')).toBe(true);
310+
expect(isRippleVisible('focus')).toBeTrue();
226311
}));
227312

228313
it('should hide the focus ripple on blur', fakeAsync(() => {
229314
pointerdown();
230315
pointerup();
231316
blur();
232-
expect(isRippleVisible('focus')).toBe(false);
317+
expect(isRippleVisible('focus')).toBeFalse();
233318
}));
234319

235320
it('should show the active ripple on pointerdown', fakeAsync(() => {
236-
expect(isRippleVisible('active')).toBe(false);
321+
expect(isRippleVisible('active')).toBeFalse();
237322
pointerdown();
238-
expect(isRippleVisible('active')).toBe(true);
323+
expect(isRippleVisible('active')).toBeTrue();
239324
}));
240325

241326
it('should hide the active ripple on pointerup', fakeAsync(() => {
242327
pointerdown();
243328
pointerup();
244-
expect(isRippleVisible('active')).toBe(false);
329+
expect(isRippleVisible('active')).toBeFalse();
245330
}));
246331

247332
// Edge cases.
248333

249334
it('should not show the hover ripple if the thumb is already focused', fakeAsync(() => {
250335
pointerdown();
251336
mouseenter();
252-
expect(isRippleVisible('hover')).toBe(false);
337+
expect(isRippleVisible('hover')).toBeFalse();
253338
}));
254339

255340
it('should hide the hover ripple if the thumb is focused', fakeAsync(() => {
256341
mouseenter();
257342
pointerdown();
258-
expect(isRippleVisible('hover')).toBe(false);
343+
expect(isRippleVisible('hover')).toBeFalse();
259344
}));
260345

261346
it('should not hide the focus ripple if the thumb is pressed', fakeAsync(() => {
262347
pointerdown();
263348
blur();
264-
expect(isRippleVisible('focus')).toBe(true);
349+
expect(isRippleVisible('focus')).toBeTrue();
265350
}));
266351

267352
it('should not hide the hover ripple on blur if the thumb is hovered', fakeAsync(() => {
268353
mouseenter();
269354
pointerdown();
270355
pointerup();
271356
blur();
272-
expect(isRippleVisible('hover')).toBe(true);
357+
expect(isRippleVisible('hover')).toBeTrue();
273358
}));
274359

275360
it('should hide the focus ripple on drag end if the thumb already lost focus', fakeAsync(() => {
276361
pointerdown();
277362
blur();
278363
pointerup();
279-
expect(isRippleVisible('focus')).toBe(false);
364+
expect(isRippleVisible('focus')).toBeFalse();
280365
}));
281366
});
282367
});
@@ -301,6 +386,25 @@ class StandardSlider {}
301386
})
302387
class StandardRangeSlider {}
303388

389+
@Component({
390+
template: `
391+
<mat-slider disabled>
392+
<input matSliderThumb>
393+
</mat-slider>
394+
`,
395+
})
396+
class DisabledSlider {}
397+
398+
@Component({
399+
template: `
400+
<mat-slider disabled>
401+
<input matSliderStartThumb>
402+
<input matSliderEndThumb>
403+
</mat-slider>
404+
`,
405+
})
406+
class DisabledRangeSlider {}
407+
304408
/** The pointer event types used by the MDC Slider. */
305409
const enum PointerEventType {
306410
POINTER_DOWN = 'pointerdown',

0 commit comments

Comments
 (0)