/
slider-harness.ts
68 lines (58 loc) · 2.59 KB
/
slider-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
/**
* @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 {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {SliderHarnessFilters, ThumbPosition} from './slider-harness-filters';
import {MatSliderThumbHarness} from './slider-thumb-harness';
/** Harness for interacting with a MDC mat-slider in tests. */
export class MatSliderHarness extends ComponentHarness {
static hostSelector = '.mat-mdc-slider';
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatSliderHarness` that meets
* certain criteria.
* @param options Options for filtering which input instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options: SliderHarnessFilters = {}): HarnessPredicate<MatSliderHarness> {
return new HarnessPredicate(MatSliderHarness, options)
.addOption('isRange', options.isRange, async (harness, value) => {
return (await harness.isRange()) === value;
});
}
/** Gets the start/primary thumb of the slider. */
async getStartThumb(): Promise<MatSliderThumbHarness> {
return this.locatorFor(MatSliderThumbHarness.with({position: ThumbPosition.START}))();
}
/** Gets the end thumb of the slider. Will throw an error for a non-range slider. */
async getEndThumb(): Promise<MatSliderThumbHarness> {
return this.locatorFor(MatSliderThumbHarness.with({position: ThumbPosition.END}))();
}
/** Gets whether the slider is a range slider. */
async isRange(): Promise<boolean> {
return (await (await this.host()).hasClass('mdc-slider--range'));
}
/** Gets whether the slider is disabled. */
async isDisabled(): Promise<boolean> {
return (await (await this.host()).hasClass('mdc-slider--disabled'));
}
/** Gets the value step increments of the slider. */
async getStep(): Promise<number> {
// The same step value is forwarded to both thumbs.
const startHost = await (await this.getEndThumb()).host();
return coerceNumberProperty(await startHost.getProperty('step'));
}
/** Gets the maximum value of the slider. */
async getMaxValue(): Promise<number> {
return (await this.getEndThumb()).getMaxValue();
}
/** Gets the minimum value of the slider. */
async getMinValue(): Promise<number> {
const startThumb = await this.isRange() ? await this.getStartThumb() : await this.getEndThumb();
return startThumb.getMinValue();
}
}