testing.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. export { M as MatInputHarness } from '../input-harness-oQzj5EsQ.mjs';
  2. import { ComponentHarness, HarnessPredicate, parallel } from '@angular/cdk/testing';
  3. import { MatFormFieldControlHarness } from '../form-field/testing/control.mjs';
  4. import '@angular/cdk/coercion';
  5. /** Harness for interacting with a native `option` in tests. */
  6. class MatNativeOptionHarness extends ComponentHarness {
  7. /** Selector used to locate option instances. */
  8. static hostSelector = 'select[matNativeControl] option';
  9. /**
  10. * Gets a `HarnessPredicate` that can be used to search for a `MatNativeOptionHarness` that meets
  11. * certain criteria.
  12. * @param options Options for filtering which option instances are considered a match.
  13. * @return a `HarnessPredicate` configured with the given options.
  14. */
  15. static with(options = {}) {
  16. return new HarnessPredicate(MatNativeOptionHarness, options)
  17. .addOption('text', options.text, async (harness, title) => HarnessPredicate.stringMatches(await harness.getText(), title))
  18. .addOption('index', options.index, async (harness, index) => (await harness.getIndex()) === index)
  19. .addOption('isSelected', options.isSelected, async (harness, isSelected) => (await harness.isSelected()) === isSelected);
  20. }
  21. /** Gets the option's label text. */
  22. async getText() {
  23. return (await this.host()).getProperty('label');
  24. }
  25. /** Index of the option within the native `select` element. */
  26. async getIndex() {
  27. return (await this.host()).getProperty('index');
  28. }
  29. /** Gets whether the option is disabled. */
  30. async isDisabled() {
  31. return (await this.host()).getProperty('disabled');
  32. }
  33. /** Gets whether the option is selected. */
  34. async isSelected() {
  35. return (await this.host()).getProperty('selected');
  36. }
  37. }
  38. /** Harness for interacting with a native `select` in tests. */
  39. class MatNativeSelectHarness extends MatFormFieldControlHarness {
  40. static hostSelector = 'select[matNativeControl]';
  41. /**
  42. * Gets a `HarnessPredicate` that can be used to search for a `MatNativeSelectHarness` that meets
  43. * certain criteria.
  44. * @param options Options for filtering which select instances are considered a match.
  45. * @return a `HarnessPredicate` configured with the given options.
  46. */
  47. static with(options = {}) {
  48. return new HarnessPredicate(MatNativeSelectHarness, options);
  49. }
  50. /** Gets a boolean promise indicating if the select is disabled. */
  51. async isDisabled() {
  52. return (await this.host()).getProperty('disabled');
  53. }
  54. /** Gets a boolean promise indicating if the select is required. */
  55. async isRequired() {
  56. return (await this.host()).getProperty('required');
  57. }
  58. /** Gets a boolean promise indicating if the select is in multi-selection mode. */
  59. async isMultiple() {
  60. return (await this.host()).getProperty('multiple');
  61. }
  62. /** Gets the name of the select. */
  63. async getName() {
  64. // The "name" property of the native select is never undefined.
  65. return await (await this.host()).getProperty('name');
  66. }
  67. /** Gets the id of the select. */
  68. async getId() {
  69. // We're guaranteed to have an id, because the `matNativeControl` always assigns one.
  70. return await (await this.host()).getProperty('id');
  71. }
  72. /** Focuses the select and returns a void promise that indicates when the action is complete. */
  73. async focus() {
  74. return (await this.host()).focus();
  75. }
  76. /** Blurs the select and returns a void promise that indicates when the action is complete. */
  77. async blur() {
  78. return (await this.host()).blur();
  79. }
  80. /** Whether the select is focused. */
  81. async isFocused() {
  82. return (await this.host()).isFocused();
  83. }
  84. /** Gets the options inside the select panel. */
  85. async getOptions(filter = {}) {
  86. return this.locatorForAll(MatNativeOptionHarness.with(filter))();
  87. }
  88. /**
  89. * Selects the options that match the passed-in filter. If the select is in multi-selection
  90. * mode all options will be clicked, otherwise the harness will pick the first matching option.
  91. */
  92. async selectOptions(filter = {}) {
  93. const [isMultiple, options] = await parallel(() => {
  94. return [this.isMultiple(), this.getOptions(filter)];
  95. });
  96. if (options.length === 0) {
  97. throw Error('Select does not have options matching the specified filter');
  98. }
  99. const [host, optionIndexes] = await parallel(() => [
  100. this.host(),
  101. parallel(() => options.slice(0, isMultiple ? undefined : 1).map(option => option.getIndex())),
  102. ]);
  103. await host.selectOptions(...optionIndexes);
  104. }
  105. }
  106. export { MatNativeOptionHarness, MatNativeSelectHarness };
  107. //# sourceMappingURL=testing.mjs.map