testing.mjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  2. import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  3. import { M as MatOptionHarness } from '../option-harness-BFcc-M_4.mjs';
  4. import { MatOptgroupHarness } from '../core/testing.mjs';
  5. class MatAutocompleteHarness extends ComponentHarness {
  6. _documentRootLocator = this.documentRootLocatorFactory();
  7. /** The selector for the host element of a `MatAutocomplete` instance. */
  8. static hostSelector = '.mat-mdc-autocomplete-trigger';
  9. /**
  10. * Gets a `HarnessPredicate` that can be used to search for an autocomplete with specific
  11. * attributes.
  12. * @param options Options for filtering which autocomplete instances are considered a match.
  13. * @return a `HarnessPredicate` configured with the given options.
  14. */
  15. static with(options = {}) {
  16. return new HarnessPredicate(this, options)
  17. .addOption('value', options.value, (harness, value) => HarnessPredicate.stringMatches(harness.getValue(), value))
  18. .addOption('disabled', options.disabled, async (harness, disabled) => {
  19. return (await harness.isDisabled()) === disabled;
  20. });
  21. }
  22. /** Gets the value of the autocomplete input. */
  23. async getValue() {
  24. return (await this.host()).getProperty('value');
  25. }
  26. /** Whether the autocomplete input is disabled. */
  27. async isDisabled() {
  28. const disabled = (await this.host()).getAttribute('disabled');
  29. return coerceBooleanProperty(await disabled);
  30. }
  31. /** Focuses the autocomplete input. */
  32. async focus() {
  33. return (await this.host()).focus();
  34. }
  35. /** Blurs the autocomplete input. */
  36. async blur() {
  37. return (await this.host()).blur();
  38. }
  39. /** Whether the autocomplete input is focused. */
  40. async isFocused() {
  41. return (await this.host()).isFocused();
  42. }
  43. /** Enters text into the autocomplete. */
  44. async enterText(value) {
  45. return (await this.host()).sendKeys(value);
  46. }
  47. /** Clears the input value. */
  48. async clear() {
  49. return (await this.host()).clear();
  50. }
  51. /** Gets the options inside the autocomplete panel. */
  52. async getOptions(filters) {
  53. if (!(await this.isOpen())) {
  54. throw new Error('Unable to retrieve options for autocomplete. Autocomplete panel is closed.');
  55. }
  56. return this._documentRootLocator.locatorForAll(MatOptionHarness.with({
  57. ...(filters || {}),
  58. ancestor: await this._getPanelSelector(),
  59. }))();
  60. }
  61. /** Gets the option groups inside the autocomplete panel. */
  62. async getOptionGroups(filters) {
  63. if (!(await this.isOpen())) {
  64. throw new Error('Unable to retrieve option groups for autocomplete. Autocomplete panel is closed.');
  65. }
  66. return this._documentRootLocator.locatorForAll(MatOptgroupHarness.with({
  67. ...(filters || {}),
  68. ancestor: await this._getPanelSelector(),
  69. }))();
  70. }
  71. /** Selects the first option matching the given filters. */
  72. async selectOption(filters) {
  73. await this.focus(); // Focus the input to make sure the autocomplete panel is shown.
  74. const options = await this.getOptions(filters);
  75. if (!options.length) {
  76. throw Error(`Could not find a mat-option matching ${JSON.stringify(filters)}`);
  77. }
  78. await options[0].click();
  79. }
  80. /** Whether the autocomplete is open. */
  81. async isOpen() {
  82. const panel = await this._getPanel();
  83. return !!panel && (await panel.hasClass(`mat-mdc-autocomplete-visible`));
  84. }
  85. /** Gets the panel associated with this autocomplete trigger. */
  86. async _getPanel() {
  87. // Technically this is static, but it needs to be in a
  88. // function, because the autocomplete's panel ID can changed.
  89. return this._documentRootLocator.locatorForOptional(await this._getPanelSelector())();
  90. }
  91. /** Gets the selector that can be used to find the autocomplete trigger's panel. */
  92. async _getPanelSelector() {
  93. return `#${await (await this.host()).getAttribute('aria-controls')}`;
  94. }
  95. }
  96. export { MatAutocompleteHarness };
  97. //# sourceMappingURL=testing.mjs.map