forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimepicker-input-harness.ts
146 lines (126 loc) · 4.64 KB
/
timepicker-input-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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* @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.dev/license
*/
import {
ComponentHarness,
ComponentHarnessConstructor,
HarnessPredicate,
TestKey,
} from '@angular/cdk/testing';
import {MatTimepickerHarness} from './timepicker-harness';
import {
TimepickerHarnessFilters,
TimepickerInputHarnessFilters,
} from './timepicker-harness-filters';
/** Harness for interacting with a standard Material timepicker inputs in tests. */
export class MatTimepickerInputHarness extends ComponentHarness {
private _documentRootLocator = this.documentRootLocatorFactory();
static hostSelector = '.mat-timepicker-input';
/**
* Gets a `HarnessPredicate` that can be used to search for a `MatTimepickerInputHarness`
* 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<T extends MatTimepickerInputHarness>(
this: ComponentHarnessConstructor<T>,
options: TimepickerInputHarnessFilters = {},
): HarnessPredicate<T> {
return new HarnessPredicate(this, options)
.addOption('value', options.value, (harness, value) => {
return HarnessPredicate.stringMatches(harness.getValue(), value);
})
.addOption('placeholder', options.placeholder, (harness, placeholder) => {
return HarnessPredicate.stringMatches(harness.getPlaceholder(), placeholder);
});
}
/** Gets whether the timepicker associated with the input is open. */
async isTimepickerOpen(): Promise<boolean> {
const host = await this.host();
return (await host.getAttribute('aria-expanded')) === 'true';
}
/** Opens the timepicker associated with the input and returns the timepicker instance. */
async openTimepicker(): Promise<MatTimepickerHarness> {
if (!(await this.isDisabled())) {
const host = await this.host();
await host.sendKeys(TestKey.DOWN_ARROW);
}
return this.getTimepicker();
}
/** Closes the timepicker associated with the input. */
async closeTimepicker(): Promise<void> {
await this._documentRootLocator.rootElement.click();
// This is necessary so that we wait for the closing animation.
await this.forceStabilize();
}
/**
* Gets the `MatTimepickerHarness` that is associated with the input.
* @param filter Optionally filters which timepicker is included.
*/
async getTimepicker(filter: TimepickerHarnessFilters = {}): Promise<MatTimepickerHarness> {
const host = await this.host();
const timepickerId = await host.getAttribute('mat-timepicker-id');
if (!timepickerId) {
throw Error('Element is not associated with a timepicker');
}
return this._documentRootLocator.locatorFor(
MatTimepickerHarness.with({
...filter,
selector: `[mat-timepicker-panel-id="${timepickerId}"]`,
}),
)();
}
/** Whether the input is disabled. */
async isDisabled(): Promise<boolean> {
return (await this.host()).getProperty<boolean>('disabled');
}
/** Whether the input is required. */
async isRequired(): Promise<boolean> {
return (await this.host()).getProperty<boolean>('required');
}
/** Gets the value of the input. */
async getValue(): Promise<string> {
// The "value" property of the native input is always defined.
return await (await this.host()).getProperty<string>('value');
}
/**
* Sets the value of the input. The value will be set by simulating
* keypresses that correspond to the given value.
*/
async setValue(newValue: string): Promise<void> {
const inputEl = await this.host();
await inputEl.clear();
// We don't want to send keys for the value if the value is an empty
// string in order to clear the value. Sending keys with an empty string
// still results in unnecessary focus events.
if (newValue) {
await inputEl.sendKeys(newValue);
}
}
/** Gets the placeholder of the input. */
async getPlaceholder(): Promise<string> {
return await (await this.host()).getProperty<string>('placeholder');
}
/**
* Focuses the input and returns a promise that indicates when the
* action is complete.
*/
async focus(): Promise<void> {
return (await this.host()).focus();
}
/**
* Blurs the input and returns a promise that indicates when the
* action is complete.
*/
async blur(): Promise<void> {
return (await this.host()).blur();
}
/** Whether the input is focused. */
async isFocused(): Promise<boolean> {
return (await this.host()).isFocused();
}
}