Skip to content

Commit 1169198

Browse files
committed
fixup! feat(material-experimental): add test harness for slider
Support setting value
1 parent 27d9893 commit 1169198

File tree

13 files changed

+145
-22
lines changed

13 files changed

+145
-22
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ rbe_autoconfig(
107107
# We can't use the default "ubuntu16_04" RBE image provided by the autoconfig because we need
108108
# a specific Linux kernel that comes with "libx11" in order to run headless browser tests.
109109
repository = "google/rbe-ubuntu16-04-webtest",
110-
)
110+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* Dimensions for element size and its position relative to the viewport.
11+
*/
12+
export interface ElementDimensions {
13+
top: number;
14+
left: number;
15+
width: number;
16+
height: number;
17+
}

src/cdk-experimental/testing/protractor/protractor-element.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {ModifierKeys} from '@angular/cdk/testing';
1010
import {browser, ElementFinder, Key} from 'protractor';
11+
import {ElementDimensions} from '../element-dimensions';
1112
import {TestElement, TestKey} from '../test-element';
1213

1314
/** Maps the `TestKey` constants to Protractor's `Key` constants. */
@@ -74,8 +75,11 @@ export class ProtractorElement implements TestElement {
7475
return this.element.clear();
7576
}
7677

77-
async click(): Promise<void> {
78-
return this.element.click();
78+
async click(relativeX = 0, relativeY = 0): Promise<void> {
79+
await browser.actions()
80+
.mouseMove(await this.element.getWebElement(), {x: relativeX, y: relativeY})
81+
.click()
82+
.perform();
7983
}
8084

8185
async focus(): Promise<void> {
@@ -126,4 +130,10 @@ export class ProtractorElement implements TestElement {
126130
const classes = (await this.getAttribute('class')) || '';
127131
return new Set(classes.split(/\s+/).filter(c => c)).has(name);
128132
}
133+
134+
async getDimensions(): Promise<ElementDimensions> {
135+
const {width, height} = await this.element.getSize();
136+
const {x: left, y: top} = await this.element.getLocation();
137+
return {width, height, left, top};
138+
}
129139
}

src/cdk-experimental/testing/test-element.ts

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

9-
/** Keyboard keys that do not result in input characters. */
109
import {ModifierKeys} from '@angular/cdk/testing';
10+
import {ElementDimensions} from './element-dimensions';
1111

1212
/** An enum of non-text keys that can be used with the `sendKeys` method. */
1313
// NOTE: This is a separate enum from `@angular/cdk/keycodes` because we don't necessarily want to
@@ -60,7 +60,7 @@ export interface TestElement {
6060
clear(): Promise<void>;
6161

6262
/** Click the element. */
63-
click(): Promise<void>;
63+
click(relativeX?: number, relativeY?: number): Promise<void>;
6464

6565
/** Focus the element. */
6666
focus(): Promise<void>;
@@ -94,4 +94,7 @@ export interface TestElement {
9494

9595
/** Checks whether the element has the given class. */
9696
hasClass(name: string): Promise<boolean>;
97+
98+
/** Gets the dimensions of the element. */
99+
getDimensions(): Promise<ElementDimensions>;
97100
}

src/cdk-experimental/testing/testbed/unit-test-element.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
typeInElement
1818
} from '@angular/cdk/testing';
1919
import {TestElement, TestKey} from '../test-element';
20+
import {ElementDimensions} from '../element-dimensions';
2021

2122
/** Maps `TestKey` constants to the `keyCode` and `key` values used by native browser events. */
2223
const keyMap = {
@@ -71,9 +72,14 @@ export class UnitTestElement implements TestElement {
7172
await this._stabilize();
7273
}
7374

74-
async click(): Promise<void> {
75+
async click(relativeX = 0, relativeY = 0): Promise<void> {
7576
await this._stabilize();
76-
dispatchMouseEvent(this.element, 'click');
77+
const {left, top} = this.element.getBoundingClientRect();
78+
const pageX = left + relativeX;
79+
const pageY = top + relativeY;
80+
dispatchMouseEvent(this.element, 'mousedown', pageX, pageY);
81+
dispatchMouseEvent(this.element, 'mouseup', pageX, pageY);
82+
dispatchMouseEvent(this.element, 'click', pageX, pageY);
7783
await this._stabilize();
7884
}
7985

@@ -126,4 +132,9 @@ export class UnitTestElement implements TestElement {
126132
await this._stabilize();
127133
return this.element.classList.contains(name);
128134
}
135+
136+
async getDimensions(): Promise<ElementDimensions> {
137+
await this._stabilize();
138+
return this.element.getBoundingClientRect();
139+
}
129140
}

src/cdk-experimental/testing/tests/protractor.e2e.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ describe('ProtractorHarnessEnvironment', () => {
208208
.toBe(await browser.driver.switchTo().activeElement().getAttribute('id'));
209209
});
210210

211+
it('should be able to retrieve dimensions', async () => {
212+
const dimensions = await (await harness.title()).getDimensions();
213+
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));
214+
});
215+
211216
it('should be able to hover', async () => {
212217
const host = await harness.host();
213218
let classAttr = await host.getAttribute('class');
@@ -229,7 +234,7 @@ describe('ProtractorHarnessEnvironment', () => {
229234

230235
it('should be able to getCssValue', async () => {
231236
const title = await harness.title();
232-
expect(await title.getCssValue('height')).toBe('50px');
237+
expect(await title.getCssValue('height')).toBe('100px');
233238
});
234239

235240
it('should focus and blur element', async () => {

src/cdk-experimental/testing/tests/test-main-component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h1 style="height: 50px">Main Component</h1>
1+
<h1 style="height: 100px; width: 200px;">Main Component</h1>
22
<div id="username">Hello {{username}} from Angular 2!</div>
33
<div class="counters">
44
<button (click)="click()">Up</button><br>

src/cdk-experimental/testing/tests/testbed.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ describe('TestbedHarnessEnvironment', () => {
227227
expect(await input.getAttribute('id')).toBe(document.activeElement!.id);
228228
});
229229

230+
it('should be able to retrieve dimensions', async () => {
231+
const dimensions = await (await harness.title()).getDimensions();
232+
expect(dimensions).toEqual(jasmine.objectContaining({height: 100, width: 200}));
233+
});
234+
230235
it('should be able to hover', async () => {
231236
const host = await harness.host();
232237
let classAttr = await host.getAttribute('class');
@@ -248,7 +253,7 @@ describe('TestbedHarnessEnvironment', () => {
248253

249254
it('should be able to getCssValue', async () => {
250255
const title = await harness.title();
251-
expect(await title.getCssValue('height')).toBe('50px');
256+
expect(await title.getCssValue('height')).toBe('100px');
252257
});
253258

254259
it('should focus and blur element', async () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ ng_test_library(
2828
ng_web_test_suite(
2929
name = "tests",
3030
deps = [":harness_tests"],
31-
)
31+
)

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {HarnessLoader} from '@angular/cdk-experimental/testing';
22
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
33
import {Component} from '@angular/core';
44
import {ComponentFixture, TestBed} from '@angular/core/testing';
5-
import {ReactiveFormsModule} from '@angular/forms';
65
import {MatSliderModule} from '@angular/material/slider';
76
import {MatSliderHarness} from './slider-harness';
87

@@ -15,7 +14,7 @@ describe('MatSliderHarness', () => {
1514
beforeEach(async () => {
1615
await TestBed
1716
.configureTestingModule({
18-
imports: [MatSliderModule, ReactiveFormsModule],
17+
imports: [MatSliderModule],
1918
declarations: [SliderHarnessTest],
2019
})
2120
.compileComponents();
@@ -113,6 +112,37 @@ function runTests() {
113112
expect(getActiveElementTagName()).not.toBe('mat-slider');
114113
});
115114

115+
it('should be able to set value of slider', async () => {
116+
const sliders = await loader.getAllHarnesses(sliderHarness);
117+
expect(await sliders[1].getValue()).toBe(0);
118+
expect(await sliders[2].getValue()).toBe(225);
119+
120+
await sliders[1].setValue(33);
121+
await sliders[2].setValue(300);
122+
123+
expect(await sliders[1].getValue()).toBe(33);
124+
// value should be clamped to the maximum.
125+
expect(await sliders[2].getValue()).toBe(250);
126+
127+
// should not retrieve incorrect values in case slider is inverted
128+
// due to RTL page layout.
129+
fixture.componentInstance.dir = 'rtl';
130+
fixture.detectChanges();
131+
132+
await sliders[1].setValue(80);
133+
expect(await sliders[1].getValue()).toBe(80);
134+
135+
// should not retrieve incorrect values in case sliders are inverted.
136+
fixture.componentInstance.invertSliders = true;
137+
fixture.detectChanges();
138+
139+
await sliders[1].setValue(75);
140+
await sliders[2].setValue(210);
141+
142+
expect(await sliders[1].getValue()).toBe(75);
143+
expect(await sliders[2].getValue()).toBe(210);
144+
});
145+
116146
it('should get disabled state of slider', async () => {
117147
const sliders = await loader.getAllHarnesses(sliderHarness);
118148
expect(await sliders[0].isDisabled()).toBe(true);
@@ -128,12 +158,19 @@ function getActiveElementTagName() {
128158
@Component({
129159
template: `
130160
<mat-slider value="50" disabled></mat-slider>
131-
<mat-slider [id]="sliderId" [displayWith]="displayFn"></mat-slider>
132-
<mat-slider min="200" max="250" value="225" [displayWith]="displayFn" vertical></mat-slider>
133-
`
161+
<div [dir]="dir">
162+
<mat-slider [id]="sliderId" [displayWith]="displayFn"
163+
[invert]="invertSliders"></mat-slider>
164+
</div>
165+
<mat-slider min="200" max="250" value="225" [displayWith]="displayFn" vertical
166+
[invert]="invertSliders">
167+
</mat-slider>
168+
`
134169
})
135170
class SliderHarnessTest {
136171
sliderId = 'my-slider';
172+
invertSliders = false;
173+
dir = 'ltr';
137174

138175
displayFn(value: number|null) {
139176
if (!value) {

0 commit comments

Comments
 (0)