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,14 @@ 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
+ return ( await this . host ( ) ) . getProperty ( 'value' ) ;
43
48
}
44
49
45
50
/** Gets a boolean promise indicating if the autocomplete input is disabled. */
@@ -48,11 +53,6 @@ export class MatAutocompleteHarness extends ComponentHarness {
48
53
return coerceBooleanProperty ( await disabled ) ;
49
54
}
50
55
51
- /** Gets a promise for the autocomplete's text. */
52
- async getText ( ) : Promise < string > {
53
- return ( await this . host ( ) ) . getProperty ( 'value' ) ;
54
- }
55
-
56
56
/** Focuses the input and returns a void promise that indicates when the action is complete. */
57
57
async focus ( ) : Promise < void > {
58
58
return ( await this . host ( ) ) . focus ( ) ;
@@ -68,28 +68,31 @@ export class MatAutocompleteHarness extends ComponentHarness {
68
68
return ( await this . host ( ) ) . sendKeys ( value ) ;
69
69
}
70
70
71
- /** Gets the autocomplete panel. */
72
- async getPanel ( ) : Promise < TestElement > {
73
- return this . _panel ( ) ;
74
- }
75
-
76
71
/** Gets the options inside the autocomplete panel. */
77
- async getOptions ( ) : Promise < MatAutocompleteOptionHarness [ ] > {
78
- return this . _options ( ) ;
72
+ async getOptions ( filters : OptionHarnessFilters = { } ) : Promise < MatAutocompleteOptionHarness [ ] > {
73
+ return this . _documentRootLocator . locatorForAll ( MatAutocompleteOptionHarness . with ( filters ) ) ( ) ;
79
74
}
80
75
81
76
/** Gets the groups of options inside the panel. */
82
- async getOptionGroups ( ) : Promise < MatAutocompleteOptionGroupHarness [ ] > {
83
- return this . _groups ( ) ;
77
+ async getOptionGroups ( filters : OptionGroupHarnessFilters = { } ) :
78
+ Promise < MatAutocompleteOptionGroupHarness [ ] > {
79
+ return this . _documentRootLocator . locatorForAll (
80
+ MatAutocompleteOptionGroupHarness . with ( filters ) ) ( ) ;
84
81
}
85
82
86
- /** Gets whether the autocomplete panel is visible. */
87
- async isPanelVisible ( ) : Promise < boolean > {
88
- return ( await this . _panel ( ) ) . hasClass ( 'mat-autocomplete-visible' ) ;
83
+ /** Selects the first option matching the given filters. */
84
+ async selectOption ( filters : OptionHarnessFilters ) : Promise < void > {
85
+ await this . focus ( ) ; // Focus the input to make sure the autocomplete panel is shown.
86
+ const options = await this . getOptions ( filters ) ;
87
+ if ( ! options . length ) {
88
+ throw Error ( `Could not find a mat-option matching ${ JSON . stringify ( filters ) } ` ) ;
89
+ }
90
+ await options [ 0 ] . select ( ) ;
89
91
}
90
92
91
93
/** Gets whether the autocomplete is open. */
92
94
async isOpen ( ) : Promise < boolean > {
93
- return ! ! ( await this . _optionalPanel ( ) ) ;
95
+ const panel = await this . _optionalPanel ( ) ;
96
+ return ! ! panel && await panel . hasClass ( 'mat-autocomplete-visible' ) ;
94
97
}
95
98
}
0 commit comments