-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
button-harness.ts
100 lines (88 loc) · 3.64 KB
/
button-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
/**
* @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 {ContentContainerComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {ButtonHarnessFilters, ButtonVariant} from '@angular/material/button/testing';
/**
* Harness for interacting with a standard mat-button in tests.
* @deprecated Use `MatButtonHarness` from `@angular/material/button/testing` instead. See https://material.angular.io/guide/mdc-migration for information about migrating.
* @breaking-change 17.0.0
*/
export class MatLegacyButtonHarness extends ContentContainerComponentHarness {
// TODO(jelbourn) use a single class, like `.mat-button-base`
/** The selector for the host element of a button instance. */
static hostSelector = `[mat-button], [mat-raised-button], [mat-flat-button], [mat-icon-button],
[mat-stroked-button], [mat-fab], [mat-mini-fab]`;
/**
* Gets a `HarnessPredicate` that can be used to search for a button harness that meets
* certain criteria.
* @param options Options for filtering which button instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options: ButtonHarnessFilters = {}): HarnessPredicate<MatLegacyButtonHarness> {
return new HarnessPredicate(MatLegacyButtonHarness, options)
.addOption('text', options.text, (harness, text) =>
HarnessPredicate.stringMatches(harness.getText(), text),
)
.addOption('variant', options.variant, (harness, variant) =>
HarnessPredicate.stringMatches(harness.getVariant(), variant),
);
}
/**
* Clicks the button at the given position relative to its top-left.
* @param relativeX The relative x position of the click.
* @param relativeY The relative y position of the click.
*/
click(relativeX: number, relativeY: number): Promise<void>;
/** Clicks the button at its center. */
click(location: 'center'): Promise<void>;
/** Clicks the button. */
click(): Promise<void>;
async click(...args: [] | ['center'] | [number, number]): Promise<void> {
return (await this.host()).click(...(args as []));
}
/** Whether the button is disabled. */
async isDisabled(): Promise<boolean> {
const disabled = (await this.host()).getAttribute('disabled');
return coerceBooleanProperty(await disabled);
}
/** Gets the button's label text. */
async getText(): Promise<string> {
return (await this.host()).text();
}
/** Focuses the button. */
async focus(): Promise<void> {
return (await this.host()).focus();
}
/** Blurs the button. */
async blur(): Promise<void> {
return (await this.host()).blur();
}
/** Whether the button is focused. */
async isFocused(): Promise<boolean> {
return (await this.host()).isFocused();
}
/** Gets the variant of the button. */
async getVariant(): Promise<ButtonVariant> {
const host = await this.host();
if ((await host.getAttribute('mat-raised-button')) != null) {
return 'raised';
} else if ((await host.getAttribute('mat-flat-button')) != null) {
return 'flat';
} else if ((await host.getAttribute('mat-icon-button')) != null) {
return 'icon';
} else if ((await host.getAttribute('mat-stroked-button')) != null) {
return 'stroked';
} else if ((await host.getAttribute('mat-fab')) != null) {
return 'fab';
} else if ((await host.getAttribute('mat-mini-fab')) != null) {
return 'mini-fab';
}
return 'basic';
}
}