6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { ComponentHarness , HarnessPredicate } from '@angular/cdk/testing' ;
10
- import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
11
- import { CheckboxHarnessFilters } from '@angular/material/checkbox/testing' ;
9
+ import { HarnessPredicate } from '@angular/cdk/testing' ;
10
+ import {
11
+ CheckboxHarnessFilters ,
12
+ MatCheckboxHarness as NonMdcCheckboxHarness ,
13
+ } from '@angular/material/checkbox/testing' ;
12
14
13
15
/** Harness for interacting with a MDC-based mat-checkbox in tests. */
14
- export class MatCheckboxHarness extends ComponentHarness {
16
+ export class MatCheckboxHarness extends NonMdcCheckboxHarness {
15
17
static hostSelector = '.mat-mdc-checkbox' ;
16
18
17
19
/**
@@ -22,8 +24,8 @@ export class MatCheckboxHarness extends ComponentHarness {
22
24
* - `name` finds a checkbox with specific name.
23
25
* @return a `HarnessPredicate` configured with the given options.
24
26
*/
25
- static with ( options : CheckboxHarnessFilters = { } ) : HarnessPredicate < MatCheckboxHarness > {
26
- return new HarnessPredicate ( MatCheckboxHarness , options )
27
+ static with ( options : CheckboxHarnessFilters = { } ) : HarnessPredicate < NonMdcCheckboxHarness > {
28
+ return new HarnessPredicate < NonMdcCheckboxHarness > ( MatCheckboxHarness , options )
27
29
. addOption (
28
30
'label' , options . label ,
29
31
( harness , label ) => HarnessPredicate . stringMatches ( harness . getLabelText ( ) , label ) )
@@ -33,83 +35,11 @@ export class MatCheckboxHarness extends ComponentHarness {
33
35
. addOption ( 'name' , options . name , async ( harness , name ) => await harness . getName ( ) === name ) ;
34
36
}
35
37
36
- private _label = this . locatorFor ( 'label' ) ;
37
- private _input = this . locatorFor ( 'input' ) ;
38
- private _inputContainer = this . locatorFor ( '.mdc-checkbox' ) ;
39
-
40
- /** Gets a boolean promise indicating if the checkbox is checked. */
41
- async isChecked ( ) : Promise < boolean > {
42
- const checked = ( await this . _input ( ) ) . getProperty ( 'checked' ) ;
43
- return coerceBooleanProperty ( await checked ) ;
44
- }
45
-
46
- /** Gets a boolean promise indicating if the checkbox is in an indeterminate state. */
47
- async isIndeterminate ( ) : Promise < boolean > {
48
- const indeterminate = ( await this . _input ( ) ) . getProperty ( 'indeterminate' ) ;
49
- return coerceBooleanProperty ( await indeterminate ) ;
50
- }
51
-
52
- /** Gets a boolean promise indicating if the checkbox is disabled. */
53
- async isDisabled ( ) : Promise < boolean > {
54
- const disabled = ( await this . _input ( ) ) . getAttribute ( 'disabled' ) ;
55
- return coerceBooleanProperty ( await disabled ) ;
56
- }
57
-
58
- /** Gets a boolean promise indicating if the checkbox is required. */
59
- async isRequired ( ) : Promise < boolean > {
60
- const required = ( await this . _input ( ) ) . getAttribute ( 'required' ) ;
61
- return coerceBooleanProperty ( await required ) ;
62
- }
63
-
64
- /** Gets a boolean promise indicating if the checkbox is valid. */
65
- async isValid ( ) : Promise < boolean > {
66
- const invalid = ( await this . host ( ) ) . hasClass ( 'ng-invalid' ) ;
67
- return ! ( await invalid ) ;
68
- }
69
-
70
- /** Gets a promise for the checkbox's name. */
71
- async getName ( ) : Promise < string | null > {
72
- return ( await this . _input ( ) ) . getAttribute ( 'name' ) ;
73
- }
74
-
75
- /** Gets a promise for the checkbox's value. */
76
- async getValue ( ) : Promise < string | null > {
77
- return ( await this . _input ( ) ) . getProperty ( 'value' ) ;
78
- }
79
-
80
- /** Gets a promise for the checkbox's aria-label. */
81
- async getAriaLabel ( ) : Promise < string | null > {
82
- return ( await this . _input ( ) ) . getAttribute ( 'aria-label' ) ;
83
- }
84
-
85
- /** Gets a promise for the checkbox's aria-labelledby. */
86
- async getAriaLabelledby ( ) : Promise < string | null > {
87
- return ( await this . _input ( ) ) . getAttribute ( 'aria-labelledby' ) ;
88
- }
89
-
90
- /** Gets a promise for the checkbox's label text. */
91
- async getLabelText ( ) : Promise < string > {
92
- return ( await this . _label ( ) ) . text ( ) ;
93
- }
94
-
95
- /** Focuses the checkbox and returns a void promise that indicates when the action is complete. */
96
- async focus ( ) : Promise < void > {
97
- return ( await this . _input ( ) ) . focus ( ) ;
98
- }
99
-
100
- /** Blurs the checkbox and returns a void promise that indicates when the action is complete. */
101
- async blur ( ) : Promise < void > {
102
- return ( await this . _input ( ) ) . blur ( ) ;
103
- }
104
-
105
- /** Whether the checkbox is focused. */
106
- async isFocused ( ) : Promise < boolean > {
107
- return ( await this . _input ( ) ) . isFocused ( ) ;
108
- }
38
+ protected _label = this . locatorFor ( 'label' ) ;
39
+ protected _inputContainer = this . locatorFor ( '.mdc-checkbox' ) ;
109
40
110
41
/**
111
- * Toggle the checked state of the checkbox and returns a void promise that indicates when the
112
- * action is complete.
42
+ * Toggles the checked state of the checkbox.
113
43
*
114
44
* Note: This attempts to toggle the checkbox as a user would, by clicking it. Therefore if you
115
45
* are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
@@ -119,34 +49,4 @@ export class MatCheckboxHarness extends ComponentHarness {
119
49
const elToClick = await this . isDisabled ( ) ? this . _inputContainer ( ) : this . _input ( ) ;
120
50
return ( await elToClick ) . click ( ) ;
121
51
}
122
-
123
- /**
124
- * Puts the checkbox in a checked state by toggling it if it is currently unchecked, or doing
125
- * nothing if it is already checked. Returns a void promise that indicates when the action is
126
- * complete.
127
- *
128
- * Note: This attempts to check the checkbox as a user would, by clicking it. Therefore if you
129
- * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
130
- * might not have the expected result.
131
- */
132
- async check ( ) : Promise < void > {
133
- if ( ! ( await this . isChecked ( ) ) ) {
134
- await this . toggle ( ) ;
135
- }
136
- }
137
-
138
- /**
139
- * Puts the checkbox in an unchecked state by toggling it if it is currently checked, or doing
140
- * nothing if it is already unchecked. Returns a void promise that indicates when the action is
141
- * complete.
142
- *
143
- * Note: This attempts to uncheck the checkbox as a user would, by clicking it. Therefore if you
144
- * are using `MAT_CHECKBOX_DEFAULT_OPTIONS` to change the behavior on click, calling this method
145
- * might not have the expected result.
146
- */
147
- async uncheck ( ) : Promise < void > {
148
- if ( await this . isChecked ( ) ) {
149
- await this . toggle ( ) ;
150
- }
151
- }
152
52
}
0 commit comments