testing.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { ComponentHarness, HarnessPredicate, parallel } from '@angular/cdk/testing';
  2. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  3. /** Harness for interacting with a standard mat-button-toggle in tests. */
  4. class MatButtonToggleHarness extends ComponentHarness {
  5. /** The selector for the host element of a `MatButton` instance. */
  6. static hostSelector = '.mat-button-toggle';
  7. _label = this.locatorFor('.mat-button-toggle-label-content');
  8. _button = this.locatorFor('.mat-button-toggle-button');
  9. /**
  10. * Gets a `HarnessPredicate` that can be used to search for a `MatButtonToggleHarness` that meets
  11. * certain criteria.
  12. * @param options Options for filtering which button toggle instances are considered a match.
  13. * @return a `HarnessPredicate` configured with the given options.
  14. */
  15. static with(options = {}) {
  16. return new HarnessPredicate(MatButtonToggleHarness, options)
  17. .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
  18. .addOption('name', options.name, (harness, name) => HarnessPredicate.stringMatches(harness.getName(), name))
  19. .addOption('checked', options.checked, async (harness, checked) => (await harness.isChecked()) === checked)
  20. .addOption('disabled', options.disabled, async (harness, disabled) => {
  21. return (await harness.isDisabled()) === disabled;
  22. });
  23. }
  24. /** Gets a boolean promise indicating if the button toggle is checked. */
  25. async isChecked() {
  26. const button = await this._button();
  27. const [checked, pressed] = await parallel(() => [
  28. button.getAttribute('aria-checked'),
  29. button.getAttribute('aria-pressed'),
  30. ]);
  31. return coerceBooleanProperty(checked) || coerceBooleanProperty(pressed);
  32. }
  33. /** Gets a boolean promise indicating if the button toggle is disabled. */
  34. async isDisabled() {
  35. const host = await this.host();
  36. return host.hasClass('mat-button-toggle-disabled');
  37. }
  38. /** Gets a promise for the button toggle's name. */
  39. async getName() {
  40. return (await this._button()).getAttribute('name');
  41. }
  42. /** Gets a promise for the button toggle's aria-label. */
  43. async getAriaLabel() {
  44. return (await this._button()).getAttribute('aria-label');
  45. }
  46. /** Gets a promise for the button toggles's aria-labelledby. */
  47. async getAriaLabelledby() {
  48. return (await this._button()).getAttribute('aria-labelledby');
  49. }
  50. /** Gets a promise for the button toggle's text. */
  51. async getText() {
  52. return (await this._label()).text();
  53. }
  54. /** Gets the appearance that the button toggle is using. */
  55. async getAppearance() {
  56. const host = await this.host();
  57. const className = 'mat-button-toggle-appearance-standard';
  58. return (await host.hasClass(className)) ? 'standard' : 'legacy';
  59. }
  60. /** Focuses the toggle. */
  61. async focus() {
  62. return (await this._button()).focus();
  63. }
  64. /** Blurs the toggle. */
  65. async blur() {
  66. return (await this._button()).blur();
  67. }
  68. /** Whether the toggle is focused. */
  69. async isFocused() {
  70. return (await this._button()).isFocused();
  71. }
  72. /** Toggle the checked state of the buttons toggle. */
  73. async toggle() {
  74. return (await this._button()).click();
  75. }
  76. /**
  77. * Puts the button toggle in a checked state by toggling it if it's
  78. * currently unchecked, or doing nothing if it is already checked.
  79. */
  80. async check() {
  81. if (!(await this.isChecked())) {
  82. await this.toggle();
  83. }
  84. }
  85. /**
  86. * Puts the button toggle in an unchecked state by toggling it if it's
  87. * currently checked, or doing nothing if it's already unchecked.
  88. */
  89. async uncheck() {
  90. if (await this.isChecked()) {
  91. await this.toggle();
  92. }
  93. }
  94. }
  95. /** Harness for interacting with a standard mat-button-toggle in tests. */
  96. class MatButtonToggleGroupHarness extends ComponentHarness {
  97. /** The selector for the host element of a `MatButton` instance. */
  98. static hostSelector = '.mat-button-toggle-group';
  99. /**
  100. * Gets a `HarnessPredicate` that can be used to search for a `MatButtonToggleGroupHarness`
  101. * that meets certain criteria.
  102. * @param options Options for filtering which button toggle instances are considered a match.
  103. * @return a `HarnessPredicate` configured with the given options.
  104. */
  105. static with(options = {}) {
  106. return new HarnessPredicate(MatButtonToggleGroupHarness, options).addOption('disabled', options.disabled, async (harness, disabled) => {
  107. return (await harness.isDisabled()) === disabled;
  108. });
  109. }
  110. /**
  111. * Gets the button toggles that are inside the group.
  112. * @param filter Optionally filters which toggles are included.
  113. */
  114. async getToggles(filter = {}) {
  115. return this.locatorForAll(MatButtonToggleHarness.with(filter))();
  116. }
  117. /** Gets whether the button toggle group is disabled. */
  118. async isDisabled() {
  119. return (await (await this.host()).getAttribute('aria-disabled')) === 'true';
  120. }
  121. /** Gets whether the button toggle group is laid out vertically. */
  122. async isVertical() {
  123. return (await this.host()).hasClass('mat-button-toggle-vertical');
  124. }
  125. /** Gets the appearance that the group is using. */
  126. async getAppearance() {
  127. const host = await this.host();
  128. const className = 'mat-button-toggle-group-appearance-standard';
  129. return (await host.hasClass(className)) ? 'standard' : 'legacy';
  130. }
  131. }
  132. export { MatButtonToggleGroupHarness, MatButtonToggleHarness };
  133. //# sourceMappingURL=testing.mjs.map