testing.mjs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { ContentContainerComponentHarness, HarnessPredicate, parallel } from '@angular/cdk/testing';
  2. /** Harness for interacting with a mat-snack-bar in tests. */
  3. class MatSnackBarHarness extends ContentContainerComponentHarness {
  4. // Developers can provide a custom component or template for the
  5. // snackbar. The canonical snack-bar parent is the "MatSnackBarContainer".
  6. /** The selector for the host element of a `MatSnackBar` instance. */
  7. static hostSelector = '.mat-mdc-snack-bar-container:not([mat-exit])';
  8. _messageSelector = '.mdc-snackbar__label';
  9. _actionButtonSelector = '.mat-mdc-snack-bar-action';
  10. _snackBarLiveRegion = this.locatorFor('[aria-live]');
  11. /**
  12. * Gets a `HarnessPredicate` that can be used to search for a `MatSnackBarHarness` that meets
  13. * certain criteria.
  14. * @param options Options for filtering which snack bar instances are considered a match.
  15. * @return a `HarnessPredicate` configured with the given options.
  16. */
  17. static with(options = {}) {
  18. return new HarnessPredicate(MatSnackBarHarness, options);
  19. }
  20. /**
  21. * Gets the role of the snack-bar. The role of a snack-bar is determined based
  22. * on the ARIA politeness specified in the snack-bar config.
  23. * @deprecated Use `getAriaLive` instead.
  24. * @breaking-change 13.0.0
  25. */
  26. async getRole() {
  27. return (await this.host()).getAttribute('role');
  28. }
  29. /**
  30. * Gets the aria-live of the snack-bar's live region. The aria-live of a snack-bar is
  31. * determined based on the ARIA politeness specified in the snack-bar config.
  32. */
  33. async getAriaLive() {
  34. return (await this._snackBarLiveRegion()).getAttribute('aria-live');
  35. }
  36. /**
  37. * Whether the snack-bar has an action. Method cannot be used for snack-bar's with custom content.
  38. */
  39. async hasAction() {
  40. return (await this._getActionButton()) !== null;
  41. }
  42. /**
  43. * Gets the description of the snack-bar. Method cannot be used for snack-bar's without action or
  44. * with custom content.
  45. */
  46. async getActionDescription() {
  47. await this._assertHasAction();
  48. return (await this._getActionButton()).text();
  49. }
  50. /**
  51. * Dismisses the snack-bar by clicking the action button. Method cannot be used for snack-bar's
  52. * without action or with custom content.
  53. */
  54. async dismissWithAction() {
  55. await this._assertHasAction();
  56. await (await this._getActionButton()).click();
  57. }
  58. /**
  59. * Gets the message of the snack-bar. Method cannot be used for snack-bar's with custom content.
  60. */
  61. async getMessage() {
  62. return (await this.locatorFor(this._messageSelector)()).text();
  63. }
  64. /** Gets whether the snack-bar has been dismissed. */
  65. async isDismissed() {
  66. // We consider the snackbar dismissed if it's not in the DOM. We can assert that the
  67. // element isn't in the DOM by seeing that its width and height are zero.
  68. const host = await this.host();
  69. const [exit, dimensions] = await parallel(() => [
  70. // The snackbar container is marked with the "exit" attribute after it has been dismissed
  71. // but before the animation has finished (after which it's removed from the DOM).
  72. host.getAttribute('mat-exit'),
  73. host.getDimensions(),
  74. ]);
  75. return exit != null || (!!dimensions && dimensions.height === 0 && dimensions.width === 0);
  76. }
  77. /**
  78. * Asserts that the current snack-bar has an action defined. Otherwise the
  79. * promise will reject.
  80. */
  81. async _assertHasAction() {
  82. if (!(await this.hasAction())) {
  83. throw Error('Method cannot be used for a snack-bar without an action.');
  84. }
  85. }
  86. /** Gets the simple snack bar action button. */
  87. async _getActionButton() {
  88. return this.locatorForOptional(this._actionButtonSelector)();
  89. }
  90. }
  91. export { MatSnackBarHarness };
  92. //# sourceMappingURL=testing.mjs.map