/
slider-thumb-harness.ts
115 lines (96 loc) · 3.78 KB
/
slider-thumb-harness.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {ComponentHarness, HarnessPredicate, parallel} from '@angular/cdk/testing';
import {SliderThumbHarnessFilters, ThumbPosition} from './slider-harness-filters';
/** Harness for interacting with a thumb inside of a Material slider in tests. */
export class MatSliderThumbHarness extends ComponentHarness {
static hostSelector =
'input[matSliderThumb], input[matSliderStartThumb], input[matSliderEndThumb]';
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatSliderThumbHarness` that meets
* certain criteria.
* @param options Options for filtering which thumb instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options: SliderThumbHarnessFilters = {}): HarnessPredicate<MatSliderThumbHarness> {
return new HarnessPredicate(MatSliderThumbHarness, options)
.addOption('position', options.position, async (harness, value) => {
return (await harness.getPosition()) === value;
});
}
/** Gets the position of the thumb inside the slider. */
async getPosition(): Promise<ThumbPosition> {
const isEnd = (await (await this.host()).getAttribute('matSliderEndThumb')) != null;
return isEnd ? ThumbPosition.END : ThumbPosition.START;
}
/** Gets the value of the thumb. */
async getValue(): Promise<number> {
return (await (await this.host()).getProperty('valueAsNumber'));
}
/** Sets the value of the thumb. */
async setValue(newValue: number): Promise<void> {
const input = await this.host();
// Since this is a range input, we can't simulate the user interacting with it so we set the
// value directly and dispatch a couple of fake events to ensure that everything fires.
await input.setInputValue(newValue + '');
await input.dispatchEvent('input');
await input.dispatchEvent('change');
}
/** Gets the current percentage value of the slider. */
async getPercentage(): Promise<number> {
const [value, min, max] = await parallel(() => [
this.getValue(),
this.getMinValue(),
this.getMaxValue()
]);
return (value - min) / (max - min);
}
/** Gets the maximum value of the thumb. */
async getMaxValue(): Promise<number> {
return coerceNumberProperty(await (await this.host()).getProperty('max'));
}
/** Gets the minimum value of the thumb. */
async getMinValue(): Promise<number> {
return coerceNumberProperty(await (await this.host()).getProperty('min'));
}
/** Gets the text representation of the slider's value. */
async getDisplayValue(): Promise<string> {
return (await (await this.host()).getAttribute('aria-valuetext')) || '';
}
/** Whether the thumb is disabled. */
async isDisabled(): Promise<boolean> {
return (await this.host()).getProperty('disabled');
}
/** Gets the name of the thumb. */
async getName(): Promise<string> {
return (await (await this.host()).getProperty('name'));
}
/** Gets the id of the thumb. */
async getId(): Promise<string> {
return (await (await this.host()).getProperty('id'));
}
/**
* Focuses the thumb and returns a promise that indicates when the
* action is complete.
*/
async focus(): Promise<void> {
return (await this.host()).focus();
}
/**
* Blurs the thumb and returns a promise that indicates when the
* action is complete.
*/
async blur(): Promise<void> {
return (await this.host()).blur();
}
/** Whether the thumb is focused. */
async isFocused(): Promise<boolean> {
return (await this.host()).isFocused();
}
}