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,14 @@ 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+ return ( await this . host ( ) ) . getProperty ( 'value' ) ;
4348 }
4449
4550 /** Gets a boolean promise indicating if the autocomplete input is disabled. */
@@ -48,11 +53,6 @@ export class MatAutocompleteHarness extends ComponentHarness {
4853 return coerceBooleanProperty ( await disabled ) ;
4954 }
5055
51- /** Gets a promise for the autocomplete's text. */
52- async getText ( ) : Promise < string > {
53- return ( await this . host ( ) ) . getProperty ( 'value' ) ;
54- }
55-
5656 /** Focuses the input and returns a void promise that indicates when the action is complete. */
5757 async focus ( ) : Promise < void > {
5858 return ( await this . host ( ) ) . focus ( ) ;
@@ -68,28 +68,31 @@ export class MatAutocompleteHarness extends ComponentHarness {
6868 return ( await this . host ( ) ) . sendKeys ( value ) ;
6969 }
7070
71- /** Gets the autocomplete panel. */
72- async getPanel ( ) : Promise < TestElement > {
73- return this . _panel ( ) ;
74- }
75-
7671 /** 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 ) ) ( ) ;
7974 }
8075
8176 /** 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 ) ) ( ) ;
8481 }
8582
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 ( ) ;
8991 }
9092
9193 /** Gets whether the autocomplete is open. */
9294 async isOpen ( ) : Promise < boolean > {
93- return ! ! ( await this . _optionalPanel ( ) ) ;
95+ const panel = await this . _optionalPanel ( ) ;
96+ return ! ! panel && await panel . hasClass ( 'mat-autocomplete-visible' ) ;
9497 }
9598}
0 commit comments