Skip to content

Commit 204a432

Browse files
authored
test(material-experimental/mdc-slider): create e2e tests (#22463)
* create basic tests for standard, disabled, and range sliders
1 parent 6079cd1 commit 204a432

File tree

3 files changed

+134
-6
lines changed

3 files changed

+134
-6
lines changed

src/e2e-app/mdc-slider/mdc-slider-e2e.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,20 @@ import {Component} from '@angular/core';
1010

1111
@Component({
1212
selector: 'mdc-slider-e2e',
13-
template: `<mat-slider></mat-slider>`,
13+
template: `
14+
<mat-slider id="standard-slider">
15+
<input aria-label="Standard slider" matSliderThumb>
16+
</mat-slider>
17+
18+
<mat-slider id="disabled-slider" disabled>
19+
<input aria-label="Disabled slider" matSliderThumb>
20+
</mat-slider>
21+
22+
<mat-slider id="range-slider">
23+
<input aria-label="Range slider start thumb" matSliderStartThumb>
24+
<input aria-label="Range slider end thumb" matSliderEndThumb>
25+
</mat-slider>
26+
`,
1427
})
1528
export class MdcSliderE2e {
1629
}

src/material-experimental/mdc-slider/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ ng_e2e_test_library(
9797
deps = [
9898
":mdc-slider",
9999
"//src/cdk/testing/private/e2e",
100+
"@npm//@material/slider",
100101
],
101102
)
102103

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

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

9-
/* tslint:disable-next-line:no-unused-variable */
10-
import {MatSlider} from './index';
11-
12-
// TODO(wagnermaciel): Implement this in a separate PR
9+
import {clickElementAtPoint, getElement, Point} from '@angular/cdk/testing/private/e2e';
10+
import {Thumb} from '@material/slider';
11+
import {browser, by, element, ElementFinder} from 'protractor';
1312

1413
describe('MDC-based MatSlider' , () => {
15-
it('does nothing yet', async () => {});
14+
const getStandardSlider = () => element(by.id('standard-slider'));
15+
const getDisabledSlider = () => element(by.id('disabled-slider'));
16+
const getRangeSlider = () => element(by.id('range-slider'));
17+
18+
beforeEach(async () => await browser.get('mdc-slider'));
19+
20+
describe('standard slider', async () => {
21+
let slider: ElementFinder;
22+
beforeEach(() => { slider = getStandardSlider(); });
23+
24+
it('should update the value on click', async () => {
25+
await setValueByClick(slider, 15);
26+
expect(await getSliderValue(slider, Thumb.END)).toBe(15);
27+
});
28+
29+
it('should update the value on slide', async () => {
30+
await slideToValue(slider, 35, Thumb.END);
31+
expect(await getSliderValue(slider, Thumb.END)).toBe(35);
32+
});
33+
});
34+
35+
describe('disabled slider', async () => {
36+
let slider: ElementFinder;
37+
beforeEach(() => { slider = getDisabledSlider(); });
38+
39+
it('should not update the value on click', async () => {
40+
await setValueByClick(slider, 15);
41+
expect(await getSliderValue(slider, Thumb.END)).not.toBe(15);
42+
});
43+
44+
it('should not update the value on slide', async () => {
45+
await slideToValue(slider, 35, Thumb.END);
46+
expect(await getSliderValue(slider, Thumb.END)).not.toBe(35);
47+
});
48+
});
49+
50+
describe('range slider', async () => {
51+
let slider: ElementFinder;
52+
beforeEach(() => { slider = getRangeSlider(); });
53+
54+
it('should update the start thumb value on slide', async () => {
55+
await slideToValue(slider, 35, Thumb.START);
56+
expect(await getSliderValue(slider, Thumb.START)).toBe(35);
57+
});
58+
59+
it('should update the end thumb value on slide', async () => {
60+
console.log('value:', await getSliderValue(slider, Thumb.END));
61+
await slideToValue(slider, 55, Thumb.END);
62+
console.log('value:', await getSliderValue(slider, Thumb.END));
63+
expect(await getSliderValue(slider, Thumb.END)).toBe(55);
64+
});
65+
66+
it('should update the start thumb value on click between thumbs '
67+
+ 'but closer to the start thumb', async () => {
68+
await setValueByClick(slider, 49);
69+
expect(await getSliderValue(slider, Thumb.START)).toBe(49);
70+
expect(await getSliderValue(slider, Thumb.END)).toBe(100);
71+
});
72+
73+
it('should update the end thumb value on click between thumbs '
74+
+ 'but closer to the end thumb', async () => {
75+
await setValueByClick(slider, 51);
76+
expect(await getSliderValue(slider, Thumb.START)).toBe(0);
77+
expect(await getSliderValue(slider, Thumb.END)).toBe(51);
78+
});
79+
});
1680
});
81+
82+
/** Returns the current value of the slider. */
83+
async function getSliderValue(slider: ElementFinder, thumbPosition: Thumb): Promise<number> {
84+
const inputs = await slider.all(by.css('.mdc-slider__input'));
85+
return thumbPosition === Thumb.END
86+
? Number(await inputs[inputs.length - 1].getAttribute('value'))
87+
: Number(await inputs[0].getAttribute('value'));
88+
}
89+
90+
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
91+
async function setValueByClick(slider: ElementFinder, value: number): Promise<void> {
92+
return clickElementAtPoint(slider, await getCoordsForValue(slider, value));
93+
}
94+
95+
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
96+
async function slideToValue
97+
(slider: ElementFinder, value: number, thumbPosition: Thumb): Promise<void> {
98+
const webElement = await getElement(slider).getWebElement();
99+
const startCoords = await getCoordsForValue(
100+
slider,
101+
await getSliderValue(slider, thumbPosition),
102+
);
103+
const endCoords = await getCoordsForValue(slider, value);
104+
return await browser.actions()
105+
.mouseMove(webElement, startCoords)
106+
.mouseDown()
107+
.mouseMove(webElement, endCoords)
108+
.mouseUp()
109+
.perform();
110+
}
111+
112+
/** Returns the x and y coordinates for the given slider value. */
113+
async function getCoordsForValue(slider: ElementFinder, value: number): Promise<Point> {
114+
const inputs = await slider.all(by.css('.mdc-slider__input'));
115+
116+
const min = Number(await inputs[0].getAttribute('min'));
117+
const max = Number(await inputs[inputs.length - 1].getAttribute('max'));
118+
const percent = (value - min) / (max - min);
119+
120+
const {width, height} = await slider.getSize();
121+
122+
// NOTE: We use Math.round here because protractor silently breaks if you pass in an imprecise
123+
// floating point number with lots of decimals. This allows us to avoid the headache but it may
124+
// cause some innaccuracies in places where these decimals mean the difference between values.
125+
126+
const x = Math.round(width * percent);
127+
const y = Math.round(height / 2);
128+
129+
return {x, y};
130+
}

0 commit comments

Comments
 (0)