This repository has been archived by the owner on Nov 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
slider-driver.ts
123 lines (114 loc) · 3.79 KB
/
slider-driver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as keycode from 'keycode';
import {DriverBase, simulate} from 'test-drive-react';
import {ContextProvider, Slider} from '../../src';
import {noop} from '../../src/utils';
import {WindowStub} from '../../test-kit';
import {simulateMouseEvent, simulateTouchEvent} from '../utils';
export interface SliderEventCoordinates {
clientX: number;
clientY: number;
}
export class BaseSliderDriver extends DriverBase {
public find(selector: string): HTMLElement {
return this.select(selector);
}
public get slider(): HTMLElement {
return this.select('SLIDER');
}
public get handle(): HTMLElement {
return this.getHandle(0);
}
public getHandle(index: number): HTMLElement {
return this.select(`SLIDER-HANDLE-${index}`);
}
public get progress(): HTMLElement {
return this.select('SLIDER-PROGRESS');
}
public getInput(index: number): HTMLElement {
return this.select(`NATIVE-INPUT-${index}`);
}
public getTooltip(index: number): HTMLElement {
const portal = this.select(`SLIDER-HANDLE-${index}`, 'PORTAL_REF');
if (!portal) {
return portal;
}
const id = (portal as HTMLElement).dataset.id;
return document.querySelector(`[data-automation-id="${id}"]`) as HTMLElement;
}
public getMark(index: number): HTMLElement {
return this.select(`SLIDER-MARKS-${index}`);
}
public getBounds() {
return this.slider.getBoundingClientRect();
}
public focus(index: number = 0): void {
simulate.focus(this.getHandle(index));
}
public mouseDown(event: SliderEventCoordinates) {
const element = this.slider;
simulate.mouseDown(element, {
preventDefault: noop,
currentTarget: element!,
clientX: event.clientX,
clientY: event.clientY
});
}
public mouseMove(event: SliderEventCoordinates, environment: WindowStub) {
this.mouseEvent('mousemove', event, environment);
}
public mouseUp(event: SliderEventCoordinates, environment: WindowStub) {
this.mouseEvent('mouseup', event, environment);
}
public touchStart(event: SliderEventCoordinates) {
const element = this.slider;
simulate.touchStart(element, {
preventDefault: noop,
currentTarget: element,
touches: {
0: {
clientX: event.clientX,
clientY: event.clientY
}
} as any as TouchList
});
}
public touchMove(event: SliderEventCoordinates, environment: WindowStub) {
this.touchEvent('touchmove', event, environment);
}
public touchEnd(event: SliderEventCoordinates, environment: WindowStub) {
this.touchEvent('touchend', event, environment);
}
public keyDown(key: string, opts?: object) {
simulate.keyDown(this.handle, {
keyCode: keycode(key),
...opts
});
}
private mouseEvent(name: string, event: SliderEventCoordinates, environment: WindowStub) {
simulateMouseEvent(
environment,
name,
{
preventDefault: noop,
clientX: event.clientX,
clientY: event.clientY
}
);
}
private touchEvent(name: string, event: SliderEventCoordinates, environment: WindowStub) {
simulateTouchEvent(
environment,
name,
{
clientX: event.clientX,
clientY: event.clientY
}
);
}
}
export class SliderDriver extends BaseSliderDriver {
public static ComponentClass = Slider;
}
export class SliderContextProvierDriver extends BaseSliderDriver {
public static ComponentClass = ContextProvider;
}