input-harness-oQzj5EsQ.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { HarnessPredicate, parallel } from '@angular/cdk/testing';
  2. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  3. import { MatFormFieldControlHarness } from './form-field/testing/control.mjs';
  4. /** Harness for interacting with a standard Material inputs in tests. */
  5. class MatInputHarness extends MatFormFieldControlHarness {
  6. // TODO: We do not want to handle `select` elements with `matNativeControl` because
  7. // not all methods of this harness work reasonably for native select elements.
  8. // For more details. See: https://github.com/angular/components/pull/18221.
  9. static hostSelector = '[matInput], input[matNativeControl], textarea[matNativeControl]';
  10. /**
  11. * Gets a `HarnessPredicate` that can be used to search for a `MatInputHarness` that meets
  12. * certain criteria.
  13. * @param options Options for filtering which input instances are considered a match.
  14. * @return a `HarnessPredicate` configured with the given options.
  15. */
  16. static with(options = {}) {
  17. return new HarnessPredicate(MatInputHarness, options)
  18. .addOption('value', options.value, (harness, value) => {
  19. return HarnessPredicate.stringMatches(harness.getValue(), value);
  20. })
  21. .addOption('placeholder', options.placeholder, (harness, placeholder) => {
  22. return HarnessPredicate.stringMatches(harness.getPlaceholder(), placeholder);
  23. });
  24. }
  25. /** Whether the input is disabled. */
  26. async isDisabled() {
  27. const host = await this.host();
  28. const disabled = await host.getAttribute('disabled');
  29. if (disabled !== null) {
  30. return coerceBooleanProperty(disabled);
  31. }
  32. return (await host.getAttribute('aria-disabled')) === 'true';
  33. }
  34. /** Whether the input is required. */
  35. async isRequired() {
  36. return (await this.host()).getProperty('required');
  37. }
  38. /** Whether the input is readonly. */
  39. async isReadonly() {
  40. return (await this.host()).getProperty('readOnly');
  41. }
  42. /** Gets the value of the input. */
  43. async getValue() {
  44. // The "value" property of the native input is never undefined.
  45. return await (await this.host()).getProperty('value');
  46. }
  47. /** Gets the name of the input. */
  48. async getName() {
  49. // The "name" property of the native input is never undefined.
  50. return await (await this.host()).getProperty('name');
  51. }
  52. /**
  53. * Gets the type of the input. Returns "textarea" if the input is
  54. * a textarea.
  55. */
  56. async getType() {
  57. // The "type" property of the native input is never undefined.
  58. return await (await this.host()).getProperty('type');
  59. }
  60. /** Gets the placeholder of the input. */
  61. async getPlaceholder() {
  62. const host = await this.host();
  63. const [nativePlaceholder, fallback] = await parallel(() => [
  64. host.getProperty('placeholder'),
  65. host.getAttribute('data-placeholder'),
  66. ]);
  67. return nativePlaceholder || fallback || '';
  68. }
  69. /** Gets the id of the input. */
  70. async getId() {
  71. // The input directive always assigns a unique id to the input in
  72. // case no id has been explicitly specified.
  73. return await (await this.host()).getProperty('id');
  74. }
  75. /**
  76. * Focuses the input and returns a promise that indicates when the
  77. * action is complete.
  78. */
  79. async focus() {
  80. return (await this.host()).focus();
  81. }
  82. /**
  83. * Blurs the input and returns a promise that indicates when the
  84. * action is complete.
  85. */
  86. async blur() {
  87. return (await this.host()).blur();
  88. }
  89. /** Whether the input is focused. */
  90. async isFocused() {
  91. return (await this.host()).isFocused();
  92. }
  93. /**
  94. * Sets the value of the input. The value will be set by simulating
  95. * keypresses that correspond to the given value.
  96. */
  97. async setValue(newValue) {
  98. const inputEl = await this.host();
  99. await inputEl.clear();
  100. // We don't want to send keys for the value if the value is an empty
  101. // string in order to clear the value. Sending keys with an empty string
  102. // still results in unnecessary focus events.
  103. if (newValue) {
  104. await inputEl.sendKeys(newValue);
  105. }
  106. // Some input types won't respond to key presses (e.g. `color`) so to be sure that the
  107. // value is set, we also set the property after the keyboard sequence. Note that we don't
  108. // want to do it before, because it can cause the value to be entered twice.
  109. await inputEl.setInputValue(newValue);
  110. }
  111. }
  112. export { MatInputHarness as M };
  113. //# sourceMappingURL=input-harness-oQzj5EsQ.mjs.map