66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { ComponentHarness , HarnessPredicate , TestElement } from '@angular/cdk/testing' ;
109import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
10+ import { ComponentHarness , HarnessPredicate } from '@angular/cdk/testing' ;
1111import { 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' ;
1318
1419/** Selector for the autocomplete panel. */
1520const PANEL_SELECTOR = '.mat-autocomplete-panel' ;
@@ -20,10 +25,7 @@ const PANEL_SELECTOR = '.mat-autocomplete-panel';
2025 */
2126export class MatAutocompleteHarness extends ComponentHarness {
2227 private _documentRootLocator = this . documentRootLocatorFactory ( ) ;
23- private _panel = this . _documentRootLocator . locatorFor ( PANEL_SELECTOR ) ;
2428 private _optionalPanel = this . _documentRootLocator . locatorForOptional ( PANEL_SELECTOR ) ;
25- private _options = this . _documentRootLocator . locatorForAll ( MatAutocompleteOptionHarness ) ;
26- private _groups = this . _documentRootLocator . locatorForAll ( MatAutocompleteOptionGroupHarness ) ;
2729
2830 static hostSelector = '.mat-autocomplete-trigger' ;
2931
@@ -35,11 +37,15 @@ export class MatAutocompleteHarness extends ComponentHarness {
3537 * @return a `HarnessPredicate` configured with the given options.
3638 */
3739 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 ) ) ;
3943 }
4044
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' ) ) ! ;
4349 }
4450
4551 /** Gets a boolean promise indicating if the autocomplete input is disabled. */
@@ -48,11 +54,6 @@ export class MatAutocompleteHarness extends ComponentHarness {
4854 return coerceBooleanProperty ( await disabled ) ;
4955 }
5056
51- /** Gets a promise for the autocomplete's text. */
52- async getText ( ) : Promise < string > {
53- return ( await this . host ( ) ) . getProperty ( 'value' ) ;
54- }
55-
5657 /** Focuses the input and returns a void promise that indicates when the action is complete. */
5758 async focus ( ) : Promise < void > {
5859 return ( await this . host ( ) ) . focus ( ) ;
@@ -68,28 +69,33 @@ export class MatAutocompleteHarness extends ComponentHarness {
6869 return ( await this . host ( ) ) . sendKeys ( value ) ;
6970 }
7071
71- /** Gets the autocomplete panel. */
72- async getPanel ( ) : Promise < TestElement > {
73- return this . _panel ( ) ;
74- }
75-
7672 /** 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 ) ) ( ) ;
7975 }
8076
8177 /** 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 ) ) ( ) ;
8482 }
8583
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 ( ) ;
8994 }
9095
9196 /** Gets whether the autocomplete is open. */
9297 async isOpen ( ) : Promise < boolean > {
93- return ! ! ( await this . _optionalPanel ( ) ) ;
98+ const panel = await this . _optionalPanel ( ) ;
99+ return ! ! panel && await panel . hasClass ( 'mat-autocomplete-visible' ) ;
94100 }
95101}
0 commit comments