testing.mjs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { booleanAttribute } from '@angular/core';
  2. import { ContentContainerComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  3. /** Harness for interacting with a mat-button in tests. */
  4. class MatButtonHarness extends ContentContainerComponentHarness {
  5. // TODO(jelbourn) use a single class, like `.mat-button-base`
  6. static hostSelector = `[mat-button], [mat-raised-button], [mat-flat-button],
  7. [mat-icon-button], [mat-stroked-button], [mat-fab], [mat-mini-fab]`;
  8. /**
  9. * Gets a `HarnessPredicate` that can be used to search for a button with specific attributes.
  10. * @param options Options for narrowing the search:
  11. * - `selector` finds a button whose host element matches the given selector.
  12. * - `text` finds a button with specific text content.
  13. * - `variant` finds buttons matching a specific variant.
  14. * @return a `HarnessPredicate` configured with the given options.
  15. */
  16. static with(options = {}) {
  17. return new HarnessPredicate(this, options)
  18. .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
  19. .addOption('variant', options.variant, (harness, variant) => HarnessPredicate.stringMatches(harness.getVariant(), variant))
  20. .addOption('disabled', options.disabled, async (harness, disabled) => {
  21. return (await harness.isDisabled()) === disabled;
  22. });
  23. }
  24. async click(...args) {
  25. return (await this.host()).click(...args);
  26. }
  27. /** Gets a boolean promise indicating if the button is disabled. */
  28. async isDisabled() {
  29. const host = await this.host();
  30. return (booleanAttribute(await host.getAttribute('disabled')) ||
  31. (await host.hasClass('mat-mdc-button-disabled')));
  32. }
  33. /** Gets a promise for the button's label text. */
  34. async getText() {
  35. return (await this.host()).text();
  36. }
  37. /** Focuses the button and returns a void promise that indicates when the action is complete. */
  38. async focus() {
  39. return (await this.host()).focus();
  40. }
  41. /** Blurs the button and returns a void promise that indicates when the action is complete. */
  42. async blur() {
  43. return (await this.host()).blur();
  44. }
  45. /** Whether the button is focused. */
  46. async isFocused() {
  47. return (await this.host()).isFocused();
  48. }
  49. /** Gets the variant of the button. */
  50. async getVariant() {
  51. const host = await this.host();
  52. if ((await host.getAttribute('mat-raised-button')) != null) {
  53. return 'raised';
  54. }
  55. else if ((await host.getAttribute('mat-flat-button')) != null) {
  56. return 'flat';
  57. }
  58. else if ((await host.getAttribute('mat-icon-button')) != null) {
  59. return 'icon';
  60. }
  61. else if ((await host.getAttribute('mat-stroked-button')) != null) {
  62. return 'stroked';
  63. }
  64. else if ((await host.getAttribute('mat-fab')) != null) {
  65. return 'fab';
  66. }
  67. else if ((await host.getAttribute('mat-mini-fab')) != null) {
  68. return 'mini-fab';
  69. }
  70. return 'basic';
  71. }
  72. }
  73. export { MatButtonHarness };
  74. //# sourceMappingURL=testing.mjs.map