testing.mjs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  2. /** Harness for interacting with a mat-tooltip in tests. */
  3. class MatTooltipHarness extends ComponentHarness {
  4. static hostSelector = '.mat-mdc-tooltip-trigger';
  5. _optionalPanel = this.documentRootLocatorFactory().locatorForOptional('.mat-mdc-tooltip');
  6. _hiddenClass = 'mat-mdc-tooltip-hide';
  7. _disabledClass = 'mat-mdc-tooltip-disabled';
  8. _showAnimationName = 'mat-mdc-tooltip-show';
  9. _hideAnimationName = 'mat-mdc-tooltip-hide';
  10. /**
  11. * Gets a `HarnessPredicate` that can be used to search for a tooltip trigger with specific
  12. * attributes.
  13. * @param options Options for narrowing the search.
  14. * @return a `HarnessPredicate` configured with the given options.
  15. */
  16. static with(options = {}) {
  17. return new HarnessPredicate(this, options);
  18. }
  19. /** Shows the tooltip. */
  20. async show() {
  21. const host = await this.host();
  22. // We need to dispatch both `touchstart` and a hover event, because the tooltip binds
  23. // different events depending on the device. The `changedTouches` is there in case the
  24. // element has ripples.
  25. await host.dispatchEvent('touchstart', { changedTouches: [] });
  26. await host.hover();
  27. const panel = await this._optionalPanel();
  28. await panel?.dispatchEvent('animationend', { animationName: this._showAnimationName });
  29. }
  30. /** Hides the tooltip. */
  31. async hide() {
  32. const host = await this.host();
  33. // We need to dispatch both `touchstart` and a hover event, because
  34. // the tooltip binds different events depending on the device.
  35. await host.dispatchEvent('touchend');
  36. await host.mouseAway();
  37. const panel = await this._optionalPanel();
  38. await panel?.dispatchEvent('animationend', { animationName: this._hideAnimationName });
  39. }
  40. /** Gets whether the tooltip is open. */
  41. async isOpen() {
  42. const panel = await this._optionalPanel();
  43. return !!panel && !(await panel.hasClass(this._hiddenClass));
  44. }
  45. /** Gets whether the tooltip is disabled */
  46. async isDisabled() {
  47. const host = await this.host();
  48. return host.hasClass(this._disabledClass);
  49. }
  50. /** Gets a promise for the tooltip panel's text. */
  51. async getTooltipText() {
  52. const panel = await this._optionalPanel();
  53. return panel ? panel.text() : '';
  54. }
  55. }
  56. export { MatTooltipHarness };
  57. //# sourceMappingURL=testing.mjs.map