6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { ComponentHarness , HarnessPredicate , TestElement } from '@angular/cdk/testing' ;
10
9
import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
10
+ import { ComponentHarness , HarnessPredicate } from '@angular/cdk/testing' ;
11
11
import { AutocompleteHarnessFilters } from './autocomplete-harness-filters' ;
12
- import { MatAutocompleteOptionHarness , MatAutocompleteOptionGroupHarness } from './option-harness' ;
12
+ import {
13
+ MatAutocompleteOptionGroupHarness ,
14
+ MatAutocompleteOptionHarness ,
15
+ OptionGroupHarnessFilters ,
16
+ OptionHarnessFilters
17
+ } from './option-harness' ;
13
18
14
19
/** Selector for the autocomplete panel. */
15
20
const PANEL_SELECTOR = '.mat-autocomplete-panel' ;
@@ -20,10 +25,7 @@ const PANEL_SELECTOR = '.mat-autocomplete-panel';
20
25
*/
21
26
export class MatAutocompleteHarness extends ComponentHarness {
22
27
private _documentRootLocator = this . documentRootLocatorFactory ( ) ;
23
- private _panel = this . _documentRootLocator . locatorFor ( PANEL_SELECTOR ) ;
24
28
private _optionalPanel = this . _documentRootLocator . locatorForOptional ( PANEL_SELECTOR ) ;
25
- private _options = this . _documentRootLocator . locatorForAll ( MatAutocompleteOptionHarness ) ;
26
- private _groups = this . _documentRootLocator . locatorForAll ( MatAutocompleteOptionGroupHarness ) ;
27
29
28
30
static hostSelector = '.mat-autocomplete-trigger' ;
29
31
@@ -35,11 +37,15 @@ export class MatAutocompleteHarness extends ComponentHarness {
35
37
* @return a `HarnessPredicate` configured with the given options.
36
38
*/
37
39
static with ( options : AutocompleteHarnessFilters = { } ) : HarnessPredicate < MatAutocompleteHarness > {
38
- return new HarnessPredicate ( MatAutocompleteHarness , options ) ;
40
+ return new HarnessPredicate ( MatAutocompleteHarness , options )
41
+ . addOption ( 'value' , options . value ,
42
+ ( harness , value ) => HarnessPredicate . stringMatches ( harness . getValue ( ) , value ) ) ;
39
43
}
40
44
41
- async getAttribute ( attributeName : string ) : Promise < string | null > {
42
- return ( await this . host ( ) ) . getAttribute ( attributeName ) ;
45
+ /** Gets the value of the autocomplete input. */
46
+ async getValue ( ) : Promise < string > {
47
+ // The "value" property of the native input is never undefined.
48
+ return ( await ( await this . host ( ) ) . getProperty ( 'value' ) ) ! ;
43
49
}
44
50
45
51
/** Gets a boolean promise indicating if the autocomplete input is disabled. */
@@ -48,11 +54,6 @@ export class MatAutocompleteHarness extends ComponentHarness {
48
54
return coerceBooleanProperty ( await disabled ) ;
49
55
}
50
56
51
- /** Gets a promise for the autocomplete's text. */
52
- async getText ( ) : Promise < string > {
53
- return ( await this . host ( ) ) . getProperty ( 'value' ) ;
54
- }
55
-
56
57
/** Focuses the input and returns a void promise that indicates when the action is complete. */
57
58
async focus ( ) : Promise < void > {
58
59
return ( await this . host ( ) ) . focus ( ) ;
@@ -68,28 +69,33 @@ export class MatAutocompleteHarness extends ComponentHarness {
68
69
return ( await this . host ( ) ) . sendKeys ( value ) ;
69
70
}
70
71
71
- /** Gets the autocomplete panel. */
72
- async getPanel ( ) : Promise < TestElement > {
73
- return this . _panel ( ) ;
74
- }
75
-
76
72
/** Gets the options inside the autocomplete panel. */
77
- async getOptions ( ) : Promise < MatAutocompleteOptionHarness [ ] > {
78
- return this . _options ( ) ;
73
+ async getOptions ( filters : OptionHarnessFilters = { } ) : Promise < MatAutocompleteOptionHarness [ ] > {
74
+ return this . _documentRootLocator . locatorForAll ( MatAutocompleteOptionHarness . with ( filters ) ) ( ) ;
79
75
}
80
76
81
77
/** Gets the groups of options inside the panel. */
82
- async getOptionGroups ( ) : Promise < MatAutocompleteOptionGroupHarness [ ] > {
83
- return this . _groups ( ) ;
78
+ async getOptionGroups ( filters : OptionGroupHarnessFilters = { } ) :
79
+ Promise < MatAutocompleteOptionGroupHarness [ ] > {
80
+ return this . _documentRootLocator . locatorForAll (
81
+ MatAutocompleteOptionGroupHarness . with ( filters ) ) ( ) ;
84
82
}
85
83
86
- /** Gets whether the autocomplete panel is visible. */
87
- async isPanelVisible ( ) : Promise < boolean > {
88
- return ( await this . _panel ( ) ) . hasClass ( 'mat-autocomplete-visible' ) ;
84
+ /** Selects the first options matching the given filters. */
85
+ async selectOption ( filters : OptionHarnessFilters = { } ) : Promise < void > {
86
+ if ( ! await this . isOpen ( ) ) {
87
+ throw Error ( 'mat-autocomplete dropdown must be open to select an option' ) ;
88
+ }
89
+ const options = await this . getOptions ( filters ) ;
90
+ if ( ! options . length ) {
91
+ throw Error ( `Could not find a mat-option matching ${ JSON . stringify ( filters ) } ` ) ;
92
+ }
93
+ await options [ 0 ] . click ( ) ;
89
94
}
90
95
91
96
/** Gets whether the autocomplete is open. */
92
97
async isOpen ( ) : Promise < boolean > {
93
- return ! ! ( await this . _optionalPanel ( ) ) ;
98
+ const panel = await this . _optionalPanel ( ) ;
99
+ return ! ! panel && await panel . hasClass ( 'mat-autocomplete-visible' ) ;
94
100
}
95
101
}
0 commit comments