Skip to content

Commit 33607dc

Browse files
devversionjelbourn
authored andcommitted
test: emit pointer event in mdc-slider unit tests
Similarly to what we did for the harness click method, we should emit pointer events in the unit tests for the MDC-based slider. This is necessary because on browsers that support pointer events, MDC slider will no longer listen to mousedown/up.
1 parent 3969676 commit 33607dc

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {Platform} from '@angular/cdk/platform';
1414
import {
1515
createKeyboardEvent,
1616
createMouseEvent,
17+
createPointerEvent,
1718
dispatchEvent,
1819
dispatchFakeEvent,
1920
dispatchKeyboardEvent,
@@ -718,10 +719,10 @@ describe('MDC-based MatSlider', () => {
718719
it('should emit an input event while sliding', () => {
719720
expect(testComponent.onChange).not.toHaveBeenCalled();
720721

721-
dispatchSliderMouseEvent(sliderNativeElement, 'mousedown', 0);
722-
dispatchSliderMouseEvent(sliderNativeElement, 'mousemove', 0.5);
723-
dispatchSliderMouseEvent(sliderNativeElement, 'mousemove', 1);
724-
dispatchSliderMouseEvent(sliderNativeElement, 'mouseup', 1);
722+
dispatchSliderMouseEvent(sliderNativeElement, 'down', 0);
723+
dispatchSliderMouseEvent(sliderNativeElement, 'move', 0.5);
724+
dispatchSliderMouseEvent(sliderNativeElement, 'move', 1);
725+
dispatchSliderMouseEvent(sliderNativeElement, 'up', 1);
725726

726727
fixture.detectChanges();
727728

@@ -1409,8 +1410,8 @@ class SliderWithTwoWayBinding {
14091410
*/
14101411
function dispatchMousedownEventSequence(sliderElement: HTMLElement, percentage: number,
14111412
button = 0): void {
1412-
dispatchSliderMouseEvent(sliderElement, 'mousedown', percentage, button);
1413-
dispatchSliderMouseEvent(sliderElement, 'mouseup', percentage, button);
1413+
dispatchSliderMouseEvent(sliderElement, 'down', percentage, button);
1414+
dispatchSliderMouseEvent(sliderElement, 'up', percentage, button);
14141415
}
14151416

14161417
/**
@@ -1421,25 +1422,32 @@ function dispatchMousedownEventSequence(sliderElement: HTMLElement, percentage:
14211422
*/
14221423
function dispatchSlideEventSequence(sliderElement: HTMLElement, startPercent: number,
14231424
endPercent: number): void {
1424-
dispatchSliderMouseEvent(sliderElement, 'mousedown', startPercent);
1425-
dispatchSliderMouseEvent(sliderElement, 'mousemove', startPercent);
1426-
dispatchSliderMouseEvent(sliderElement, 'mousemove', endPercent);
1427-
dispatchSliderMouseEvent(sliderElement, 'mouseup', endPercent);
1425+
dispatchSliderMouseEvent(sliderElement, 'down', startPercent);
1426+
dispatchSliderMouseEvent(sliderElement, 'move', startPercent);
1427+
dispatchSliderMouseEvent(sliderElement, 'move', endPercent);
1428+
dispatchSliderMouseEvent(sliderElement, 'up', endPercent);
14281429
}
14291430

14301431
/**
14311432
* Dispatches a mouse event from an element at a given position based on the percentage.
14321433
* @param sliderElement The mat-slider element from which the event will be dispatched.
1433-
* @param type Type of the mouse event that should be dispatched.
1434+
* @param type Type of mouse interaction.
14341435
* @param percent The percentage of the slider where the event will happen.
14351436
* @param button Button that should be held for this event.
14361437
*/
1437-
function dispatchSliderMouseEvent(sliderElement: HTMLElement, type: string, percent: number,
1438-
button = 0): void {
1439-
let trackElement = sliderElement.querySelector('.mdc-slider__track-container')!;
1440-
let dimensions = trackElement.getBoundingClientRect();
1441-
let x = dimensions.left + (dimensions.width * percent);
1442-
let y = dimensions.top + (dimensions.height * percent);
1443-
1444-
dispatchEvent(sliderElement, createMouseEvent(type, x, y, 0));
1438+
function dispatchSliderMouseEvent(sliderElement: HTMLElement, type: 'up' | 'down' | 'move',
1439+
percent: number, button = 0): void {
1440+
const trackElement = sliderElement.querySelector('.mdc-slider__track-container')!;
1441+
const dimensions = trackElement.getBoundingClientRect();
1442+
const clientX = dimensions.left + (dimensions.width * percent);
1443+
const clientY = dimensions.top + (dimensions.height * percent);
1444+
1445+
// The latest versions of all browsers we support have the new `PointerEvent` API.
1446+
// Though since we capture the two most recent versions of these browsers, we also
1447+
// need to support Safari 12 at time of writing. Safari 12 does not have support for this,
1448+
// so we need to conditionally create and dispatch these events based on feature detection.
1449+
if (window.PointerEvent !== undefined) {
1450+
dispatchEvent(sliderElement, createPointerEvent(`pointer${type}`, clientX, clientY));
1451+
}
1452+
dispatchEvent(sliderElement, createMouseEvent(`mouse${type}`, clientX, clientY, 0));
14451453
}

0 commit comments

Comments
 (0)