testing.mjs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { ContentContainerComponentHarness, HarnessPredicate, ComponentHarness } from '@angular/cdk/testing';
  2. /** Selectors for the various `mat-expansion-panel` sections that may contain user content. */
  3. var MatExpansionPanelSection;
  4. (function (MatExpansionPanelSection) {
  5. MatExpansionPanelSection["HEADER"] = ".mat-expansion-panel-header";
  6. MatExpansionPanelSection["TITLE"] = ".mat-expansion-panel-header-title";
  7. MatExpansionPanelSection["DESCRIPTION"] = ".mat-expansion-panel-header-description";
  8. MatExpansionPanelSection["CONTENT"] = ".mat-expansion-panel-content";
  9. })(MatExpansionPanelSection || (MatExpansionPanelSection = {}));
  10. /** Harness for interacting with a standard mat-expansion-panel in tests. */
  11. class MatExpansionPanelHarness extends ContentContainerComponentHarness {
  12. static hostSelector = '.mat-expansion-panel';
  13. _header = this.locatorFor(MatExpansionPanelSection.HEADER);
  14. _title = this.locatorForOptional(MatExpansionPanelSection.TITLE);
  15. _description = this.locatorForOptional(MatExpansionPanelSection.DESCRIPTION);
  16. _expansionIndicator = this.locatorForOptional('.mat-expansion-indicator');
  17. _content = this.locatorFor(MatExpansionPanelSection.CONTENT);
  18. /**
  19. * Gets a `HarnessPredicate` that can be used to search for an expansion-panel
  20. * with specific attributes.
  21. * @param options Options for narrowing the search:
  22. * - `title` finds an expansion-panel with a specific title text.
  23. * - `description` finds an expansion-panel with a specific description text.
  24. * - `expanded` finds an expansion-panel that is currently expanded.
  25. * - `disabled` finds an expansion-panel that is disabled.
  26. * @return a `HarnessPredicate` configured with the given options.
  27. */
  28. static with(options = {}) {
  29. return new HarnessPredicate(MatExpansionPanelHarness, options)
  30. .addOption('title', options.title, (harness, title) => HarnessPredicate.stringMatches(harness.getTitle(), title))
  31. .addOption('description', options.description, (harness, description) => HarnessPredicate.stringMatches(harness.getDescription(), description))
  32. .addOption('content', options.content, (harness, content) => HarnessPredicate.stringMatches(harness.getTextContent(), content))
  33. .addOption('expanded', options.expanded, async (harness, expanded) => (await harness.isExpanded()) === expanded)
  34. .addOption('disabled', options.disabled, async (harness, disabled) => (await harness.isDisabled()) === disabled);
  35. }
  36. /** Whether the panel is expanded. */
  37. async isExpanded() {
  38. return (await this.host()).hasClass('mat-expanded');
  39. }
  40. /**
  41. * Gets the title text of the panel.
  42. * @returns Title text or `null` if no title is set up.
  43. */
  44. async getTitle() {
  45. const titleEl = await this._title();
  46. return titleEl ? titleEl.text() : null;
  47. }
  48. /**
  49. * Gets the description text of the panel.
  50. * @returns Description text or `null` if no description is set up.
  51. */
  52. async getDescription() {
  53. const descriptionEl = await this._description();
  54. return descriptionEl ? descriptionEl.text() : null;
  55. }
  56. /** Whether the panel is disabled. */
  57. async isDisabled() {
  58. return (await (await this._header()).getAttribute('aria-disabled')) === 'true';
  59. }
  60. /**
  61. * Toggles the expanded state of the panel by clicking on the panel
  62. * header. This method will not work if the panel is disabled.
  63. */
  64. async toggle() {
  65. await (await this._header()).click();
  66. }
  67. /** Expands the expansion panel if collapsed. */
  68. async expand() {
  69. if (!(await this.isExpanded())) {
  70. await this.toggle();
  71. }
  72. }
  73. /** Collapses the expansion panel if expanded. */
  74. async collapse() {
  75. if (await this.isExpanded()) {
  76. await this.toggle();
  77. }
  78. }
  79. /** Gets the text content of the panel. */
  80. async getTextContent() {
  81. return (await this._content()).text();
  82. }
  83. /**
  84. * Gets a `HarnessLoader` that can be used to load harnesses for
  85. * components within the panel's content area.
  86. * @deprecated Use either `getChildLoader(MatExpansionPanelSection.CONTENT)`, `getHarness` or
  87. * `getAllHarnesses` instead.
  88. * @breaking-change 12.0.0
  89. */
  90. async getHarnessLoaderForContent() {
  91. return this.getChildLoader(MatExpansionPanelSection.CONTENT);
  92. }
  93. /** Focuses the panel. */
  94. async focus() {
  95. return (await this._header()).focus();
  96. }
  97. /** Blurs the panel. */
  98. async blur() {
  99. return (await this._header()).blur();
  100. }
  101. /** Whether the panel is focused. */
  102. async isFocused() {
  103. return (await this._header()).isFocused();
  104. }
  105. /** Whether the panel has a toggle indicator displayed. */
  106. async hasToggleIndicator() {
  107. return (await this._expansionIndicator()) !== null;
  108. }
  109. /** Gets the position of the toggle indicator. */
  110. async getToggleIndicatorPosition() {
  111. // By default the expansion indicator will show "after" the panel header content.
  112. if (await (await this._header()).hasClass('mat-expansion-toggle-indicator-before')) {
  113. return 'before';
  114. }
  115. return 'after';
  116. }
  117. }
  118. /** Harness for interacting with a standard mat-accordion in tests. */
  119. class MatAccordionHarness extends ComponentHarness {
  120. static hostSelector = '.mat-accordion';
  121. /**
  122. * Gets a `HarnessPredicate` that can be used to search for an accordion
  123. * with specific attributes.
  124. * @param options Options for narrowing the search.
  125. * @return a `HarnessPredicate` configured with the given options.
  126. */
  127. static with(options = {}) {
  128. return new HarnessPredicate(MatAccordionHarness, options);
  129. }
  130. /** Gets all expansion panels which are part of the accordion. */
  131. async getExpansionPanels(filter = {}) {
  132. return this.locatorForAll(MatExpansionPanelHarness.with(filter))();
  133. }
  134. /** Whether the accordion allows multiple expanded panels simultaneously. */
  135. async isMulti() {
  136. return (await this.host()).hasClass('mat-accordion-multi');
  137. }
  138. }
  139. export { MatAccordionHarness, MatExpansionPanelHarness, MatExpansionPanelSection };
  140. //# sourceMappingURL=testing.mjs.map