Skip to content

Commit 1f9709c

Browse files
committed
fixup! fix(material-experimental/mdc-slider): code review changes
1 parent d25bb91 commit 1f9709c

File tree

2 files changed

+51
-42
lines changed

2 files changed

+51
-42
lines changed

src/cdk/testing/testbed/fake-events/dispatch-events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ export function dispatchPointerEvent(node: Node, type: string, clientX = 0, clie
6565
* Shorthand to dispatch a touch event on the specified coordinates.
6666
* @docs-private
6767
*/
68-
export function dispatchTouchEvent(node: Node, type: string, x = 0, y = 0) {
69-
return dispatchEvent(node, createTouchEvent(type, x, y));
68+
export function dispatchTouchEvent(node: Node, type: string, pageX = 0, pageY = 0, clientX = 0, clientY = 0) {
69+
return dispatchEvent(node, createTouchEvent(type, pageX, pageY, clientX, clientY));
7070
}

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

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {Platform} from '@angular/cdk/platform';
910
import {
1011
dispatchMouseEvent,
1112
dispatchPointerEvent,
@@ -19,7 +20,10 @@ import {MatSliderModule} from './module';
1920
import {MatSlider, MatSliderThumb, MatSliderVisualThumb} from './slider';
2021

2122
describe('MDC-based MatSlider' , () => {
23+
let platform: Platform;
24+
2225
beforeAll(() => {
26+
platform = TestBed.inject(Platform);
2327
// Mock #setPointerCapture as it throws errors on pointerdown without a real pointerId.
2428
spyOn(Element.prototype, 'setPointerCapture');
2529
});
@@ -53,28 +57,28 @@ describe('MDC-based MatSlider' , () => {
5357
});
5458

5559
it('should update the value on mousedown', () => {
56-
setValueByClick(sliderInstance, 19);
60+
setValueByClick(sliderInstance, 19, platform.IOS);
5761
expect(inputInstance.value).toBe(19);
5862
});
5963

6064
it('should update the value on a slide', () => {
61-
slideToValue(sliderInstance, 77);
65+
slideToValue(sliderInstance, 77, Thumb.END, platform.IOS);
6266
expect(inputInstance.value).toBe(77);
6367
});
6468

6569
it('should set the value as min when sliding before the track', () => {
66-
slideToValue(sliderInstance, -1);
70+
slideToValue(sliderInstance, -1, Thumb.END, platform.IOS);
6771
expect(inputInstance.value).toBe(0);
6872
});
6973

7074
it('should set the value as max when sliding past the track', () => {
71-
slideToValue(sliderInstance, 101);
75+
slideToValue(sliderInstance, 101, Thumb.END, platform.IOS);
7276
expect(inputInstance.value).toBe(100);
7377
});
7478

7579
it('should focus the slider input when clicking on the slider', () => {
7680
expect(document.activeElement).not.toBe(inputInstance._hostElement);
77-
setValueByClick(sliderInstance, 0);
81+
setValueByClick(sliderInstance, 0, platform.IOS);
7882
expect(document.activeElement).toBe(inputInstance._hostElement);
7983
});
8084
});
@@ -103,46 +107,46 @@ describe('MDC-based MatSlider' , () => {
103107
});
104108

105109
it('should update the start value on a slide', () => {
106-
slideToValue(sliderInstance, 19, Thumb.START);
110+
slideToValue(sliderInstance, 19, Thumb.START, platform.IOS);
107111
expect(startInputInstance.value).toBe(19);
108112
});
109113

110114
it('should update the end value on a slide', () => {
111-
slideToValue(sliderInstance, 27, Thumb.END);
115+
slideToValue(sliderInstance, 27, Thumb.END, platform.IOS);
112116
expect(endInputInstance.value).toBe(27);
113117
});
114118

115119
it('should update the start value on mousedown behind the start thumb', () => {
116120
sliderInstance._setValue(19, Thumb.START);
117-
setValueByClick(sliderInstance, 12);
121+
setValueByClick(sliderInstance, 12, platform.IOS);
118122
expect(startInputInstance.value).toBe(12);
119123
});
120124

121125
it('should update the end value on mousedown in front of the end thumb', () => {
122126
sliderInstance._setValue(27, Thumb.END);
123-
setValueByClick(sliderInstance, 55);
127+
setValueByClick(sliderInstance, 55, platform.IOS);
124128
expect(endInputInstance.value).toBe(55);
125129
});
126130

127131
it('should set the start value as min when sliding before the track', () => {
128-
slideToValue(sliderInstance, -1, Thumb.START);
132+
slideToValue(sliderInstance, -1, Thumb.START, platform.IOS);
129133
expect(startInputInstance.value).toBe(0);
130134
});
131135

132136
it('should set the end value as max when sliding past the track', () => {
133-
slideToValue(sliderInstance, 101, Thumb.START);
137+
slideToValue(sliderInstance, 101, Thumb.START, platform.IOS);
134138
expect(startInputInstance.value).toBe(100);
135139
});
136140

137141
it('should not let the start thumb slide past the end thumb', () => {
138142
sliderInstance._setValue(50, Thumb.END);
139-
slideToValue(sliderInstance, 75, Thumb.START);
143+
slideToValue(sliderInstance, 75, Thumb.START, platform.IOS);
140144
expect(startInputInstance.value).toBe(50);
141145
});
142146

143147
it('should not let the end thumb slide before the start thumb', () => {
144148
sliderInstance._setValue(50, Thumb.START);
145-
slideToValue(sliderInstance, 25, Thumb.END);
149+
slideToValue(sliderInstance, 25, Thumb.END, platform.IOS);
146150
expect(startInputInstance.value).toBe(50);
147151
});
148152
});
@@ -186,11 +190,15 @@ describe('MDC-based MatSlider' , () => {
186190
}
187191

188192
function pointerdown() {
189-
dispatchPointerOrTouchEvent(thumbElement, PointerEventType.POINTER_DOWN, thumbX, thumbY);
193+
dispatchPointerOrTouchEvent(
194+
thumbElement, PointerEventType.POINTER_DOWN, thumbX, thumbY, platform.IOS
195+
);
190196
}
191197

192198
function pointerup() {
193-
dispatchPointerOrTouchEvent(thumbElement, PointerEventType.POINTER_UP, thumbX, thumbY);
199+
dispatchPointerOrTouchEvent(
200+
thumbElement, PointerEventType.POINTER_UP, thumbX, thumbY, platform.IOS
201+
);
194202
}
195203

196204
it('should show the hover ripple on mouseenter', fakeAsync(() => {
@@ -293,8 +301,22 @@ class StandardSlider {}
293301
})
294302
class StandardRangeSlider {}
295303

304+
/** The pointer event types used by the MDC Slider. */
305+
const enum PointerEventType {
306+
POINTER_DOWN = 'pointerdown',
307+
POINTER_UP = 'pointerup',
308+
POINTER_MOVE = 'pointermove',
309+
}
310+
311+
/** The touch event types used by the MDC Slider. */
312+
const enum TouchEventType {
313+
TOUCH_START = 'touchstart',
314+
TOUCH_END = 'touchend',
315+
TOUCH_MOVE = 'touchmove',
316+
}
317+
296318
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
297-
function setValueByClick(slider: MatSlider, value: number) {
319+
function setValueByClick(slider: MatSlider, value: number, isIOS: boolean) {
298320
const {min, max} = slider;
299321
const percent = (value - min) / (max - min);
300322

@@ -303,12 +325,12 @@ function setValueByClick(slider: MatSlider, value: number) {
303325
const x = left + (width * percent);
304326
const y = top + (height / 2);
305327

306-
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_DOWN, x, y);
307-
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, x, y);
328+
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_DOWN, x, y, isIOS);
329+
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, x, y, isIOS);
308330
}
309331

310332
/** Slides the MatSlider's thumb to the given value. */
311-
function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb = Thumb.END) {
333+
function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb, isIOS: boolean) {
312334
const {min, max} = slider;
313335
const percent = (value - min) / (max - min);
314336

@@ -324,34 +346,21 @@ function slideToValue(slider: MatSlider, value: number, thumbPosition: Thumb = T
324346
const endX = sliderDimensions.left + (sliderDimensions.width * percent);
325347
const endY = sliderDimensions.top + (sliderDimensions.height / 2);
326348

327-
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_DOWN, startX, startY);
328-
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_MOVE, endX, endY);
329-
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, endX, endY);
349+
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_DOWN, startX, startY, isIOS);
350+
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_MOVE, endX, endY, isIOS);
351+
dispatchPointerOrTouchEvent(sliderElement, PointerEventType.POINTER_UP, endX, endY, isIOS);
330352
}
331353

332354
/** Dispatch a pointerdown or pointerup event if supported, otherwise dispatch the touch event. */
333-
function dispatchPointerOrTouchEvent(node: Node, type: PointerEventType, x?: number, y?: number) {
334-
if (typeof PointerEvent !== 'undefined' && PointerEvent) {
335-
dispatchPointerEvent(node, type, x, y);
355+
function dispatchPointerOrTouchEvent(
356+
node: Node, type: PointerEventType, x: number, y: number, isIOS: boolean) {
357+
if (isIOS) {
358+
dispatchTouchEvent(node, pointerEventTypeToTouchEventType(type), x, y, x, y);
336359
} else {
337-
dispatchTouchEvent(node, pointerEventTypeToTouchEventType(type), x, y);
360+
dispatchPointerEvent(node, type, x, y);
338361
}
339362
}
340363

341-
/** The pointer event types used by the MDC Slider. */
342-
const enum PointerEventType {
343-
POINTER_DOWN = 'pointerdown',
344-
POINTER_UP = 'pointerup',
345-
POINTER_MOVE = 'pointermove',
346-
}
347-
348-
/** The touch event types used by the MDC Slider. */
349-
const enum TouchEventType {
350-
TOUCH_START = 'touchstart',
351-
TOUCH_END = 'touchend',
352-
TOUCH_MOVE = 'touchmove',
353-
}
354-
355364
/** Returns the touch event equivalent of the given pointer event. */
356365
function pointerEventTypeToTouchEventType(pointerEventType: PointerEventType) {
357366
switch (pointerEventType) {

0 commit comments

Comments
 (0)