|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
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'; |
13 | 12 |
|
14 | 13 | 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 | + }); |
16 | 80 | });
|
| 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