testing.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { HarnessPredicate, ContentContainerComponentHarness } from '@angular/cdk/testing';
  2. /**
  3. * Base class for the drawer harness functionality.
  4. * @docs-private
  5. */
  6. class MatDrawerHarnessBase extends ContentContainerComponentHarness {
  7. /** Whether the drawer is open. */
  8. async isOpen() {
  9. return (await this.host()).hasClass('mat-drawer-opened');
  10. }
  11. /** Gets the position of the drawer inside its container. */
  12. async getPosition() {
  13. const host = await this.host();
  14. return (await host.hasClass('mat-drawer-end')) ? 'end' : 'start';
  15. }
  16. /** Gets the mode that the drawer is in. */
  17. async getMode() {
  18. const host = await this.host();
  19. if (await host.hasClass('mat-drawer-push')) {
  20. return 'push';
  21. }
  22. if (await host.hasClass('mat-drawer-side')) {
  23. return 'side';
  24. }
  25. return 'over';
  26. }
  27. }
  28. /** Harness for interacting with a standard mat-drawer in tests. */
  29. class MatDrawerHarness extends MatDrawerHarnessBase {
  30. /** The selector for the host element of a `MatDrawer` instance. */
  31. static hostSelector = '.mat-drawer';
  32. /**
  33. * Gets a `HarnessPredicate` that can be used to search for a `MatDrawerHarness` that meets
  34. * certain criteria.
  35. * @param options Options for filtering which drawer instances are considered a match.
  36. * @return a `HarnessPredicate` configured with the given options.
  37. */
  38. static with(options = {}) {
  39. return new HarnessPredicate(MatDrawerHarness, options).addOption('position', options.position, async (harness, position) => (await harness.getPosition()) === position);
  40. }
  41. }
  42. /** Harness for interacting with a standard mat-drawer-content in tests. */
  43. class MatDrawerContentHarness extends ContentContainerComponentHarness {
  44. /** The selector for the host element of a `MatDrawerContent` instance. */
  45. static hostSelector = '.mat-drawer-content';
  46. /**
  47. * Gets a `HarnessPredicate` that can be used to search for a `MatDrawerContentHarness` that
  48. * meets certain criteria.
  49. * @param options Options for filtering which drawer content instances are considered a match.
  50. * @return a `HarnessPredicate` configured with the given options.
  51. */
  52. static with(options = {}) {
  53. return new HarnessPredicate(MatDrawerContentHarness, options);
  54. }
  55. }
  56. /** Harness for interacting with a standard mat-drawer-container in tests. */
  57. class MatDrawerContainerHarness extends ContentContainerComponentHarness {
  58. /** The selector for the host element of a `MatDrawerContainer` instance. */
  59. static hostSelector = '.mat-drawer-container';
  60. /**
  61. * Gets a `HarnessPredicate` that can be used to search for a `MatDrawerContainerHarness` that
  62. * meets certain criteria.
  63. * @param options Options for filtering which container instances are considered a match.
  64. * @return a `HarnessPredicate` configured with the given options.
  65. */
  66. static with(options = {}) {
  67. return new HarnessPredicate(MatDrawerContainerHarness, options);
  68. }
  69. /**
  70. * Gets drawers that match particular criteria within the container.
  71. * @param filter Optionally filters which chips are included.
  72. */
  73. async getDrawers(filter = {}) {
  74. return this.locatorForAll(MatDrawerHarness.with(filter))();
  75. }
  76. /** Gets the element that has the container's content. */
  77. async getContent() {
  78. return this.locatorFor(MatDrawerContentHarness)();
  79. }
  80. }
  81. /** Harness for interacting with a standard mat-sidenav-content in tests. */
  82. class MatSidenavContentHarness extends ContentContainerComponentHarness {
  83. /** The selector for the host element of a `MatSidenavContent` instance. */
  84. static hostSelector = '.mat-sidenav-content';
  85. /**
  86. * Gets a `HarnessPredicate` that can be used to search for a `MatSidenavContentHarness` that
  87. * meets certain criteria.
  88. * @param options Options for filtering which sidenav content instances are considered a match.
  89. * @return a `HarnessPredicate` configured with the given options.
  90. */
  91. static with(options = {}) {
  92. return new HarnessPredicate(MatSidenavContentHarness, options);
  93. }
  94. }
  95. /** Harness for interacting with a standard mat-sidenav in tests. */
  96. class MatSidenavHarness extends MatDrawerHarnessBase {
  97. /** The selector for the host element of a `MatSidenav` instance. */
  98. static hostSelector = '.mat-sidenav';
  99. /**
  100. * Gets a `HarnessPredicate` that can be used to search for a `MatSidenavHarness` that meets
  101. * certain criteria.
  102. * @param options Options for filtering which sidenav instances are considered a match.
  103. * @return a `HarnessPredicate` configured with the given options.
  104. */
  105. static with(options = {}) {
  106. return new HarnessPredicate(MatSidenavHarness, options).addOption('position', options.position, async (harness, position) => (await harness.getPosition()) === position);
  107. }
  108. /** Whether the sidenav is fixed in the viewport. */
  109. async isFixedInViewport() {
  110. return (await this.host()).hasClass('mat-sidenav-fixed');
  111. }
  112. }
  113. /** Harness for interacting with a standard mat-sidenav-container in tests. */
  114. class MatSidenavContainerHarness extends ContentContainerComponentHarness {
  115. /** The selector for the host element of a `MatSidenavContainer` instance. */
  116. static hostSelector = '.mat-sidenav-container';
  117. /**
  118. * Gets a `HarnessPredicate` that can be used to search for a `MatSidenavContainerHarness` that
  119. * meets certain criteria.
  120. * @param options Options for filtering which container instances are considered a match.
  121. * @return a `HarnessPredicate` configured with the given options.
  122. */
  123. static with(options = {}) {
  124. return new HarnessPredicate(MatSidenavContainerHarness, options);
  125. }
  126. /**
  127. * Gets sidenavs that match particular criteria within the container.
  128. * @param filter Optionally filters which chips are included.
  129. */
  130. async getSidenavs(filter = {}) {
  131. return this.locatorForAll(MatSidenavHarness.with(filter))();
  132. }
  133. /** Gets the element that has the container's content. */
  134. async getContent() {
  135. return this.locatorFor(MatSidenavContentHarness)();
  136. }
  137. }
  138. export { MatDrawerContainerHarness, MatDrawerContentHarness, MatDrawerHarness, MatSidenavContainerHarness, MatSidenavContentHarness, MatSidenavHarness };
  139. //# sourceMappingURL=testing.mjs.map