testing.mjs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { ContentContainerComponentHarness, HarnessPredicate, ComponentHarness, parallel } from '@angular/cdk/testing';
  2. import { ɵ as _TileCoordinator } from '../public-api-BoO5eSq-.mjs';
  3. /** Selectors for the various `mat-grid-tile` sections that may contain user content. */
  4. var MatGridTileSection;
  5. (function (MatGridTileSection) {
  6. MatGridTileSection["HEADER"] = ".mat-grid-tile-header";
  7. MatGridTileSection["FOOTER"] = ".mat-grid-tile-footer";
  8. })(MatGridTileSection || (MatGridTileSection = {}));
  9. /** Harness for interacting with a standard `MatGridTitle` in tests. */
  10. class MatGridTileHarness extends ContentContainerComponentHarness {
  11. /** The selector for the host element of a `MatGridTile` instance. */
  12. static hostSelector = '.mat-grid-tile';
  13. /**
  14. * Gets a `HarnessPredicate` that can be used to search for a `MatGridTileHarness`
  15. * that meets certain criteria.
  16. * @param options Options for filtering which dialog instances are considered a match.
  17. * @return a `HarnessPredicate` configured with the given options.
  18. */
  19. static with(options = {}) {
  20. return new HarnessPredicate(MatGridTileHarness, options)
  21. .addOption('headerText', options.headerText, (harness, pattern) => HarnessPredicate.stringMatches(harness.getHeaderText(), pattern))
  22. .addOption('footerText', options.footerText, (harness, pattern) => HarnessPredicate.stringMatches(harness.getFooterText(), pattern));
  23. }
  24. _header = this.locatorForOptional(MatGridTileSection.HEADER);
  25. _footer = this.locatorForOptional(MatGridTileSection.FOOTER);
  26. _avatar = this.locatorForOptional('.mat-grid-avatar');
  27. /** Gets the amount of rows that the grid-tile takes up. */
  28. async getRowspan() {
  29. return Number(await (await this.host()).getAttribute('rowspan'));
  30. }
  31. /** Gets the amount of columns that the grid-tile takes up. */
  32. async getColspan() {
  33. return Number(await (await this.host()).getAttribute('colspan'));
  34. }
  35. /** Whether the grid-tile has a header. */
  36. async hasHeader() {
  37. return (await this._header()) !== null;
  38. }
  39. /** Whether the grid-tile has a footer. */
  40. async hasFooter() {
  41. return (await this._footer()) !== null;
  42. }
  43. /** Whether the grid-tile has an avatar. */
  44. async hasAvatar() {
  45. return (await this._avatar()) !== null;
  46. }
  47. /** Gets the text of the header if present. */
  48. async getHeaderText() {
  49. // For performance reasons, we do not use "hasHeader" as
  50. // we would then need to query twice for the header.
  51. const headerEl = await this._header();
  52. return headerEl ? headerEl.text() : null;
  53. }
  54. /** Gets the text of the footer if present. */
  55. async getFooterText() {
  56. // For performance reasons, we do not use "hasFooter" as
  57. // we would then need to query twice for the footer.
  58. const headerEl = await this._footer();
  59. return headerEl ? headerEl.text() : null;
  60. }
  61. }
  62. /** Harness for interacting with a standard `MatGridList` in tests. */
  63. class MatGridListHarness extends ComponentHarness {
  64. /** The selector for the host element of a `MatGridList` instance. */
  65. static hostSelector = '.mat-grid-list';
  66. /**
  67. * Gets a `HarnessPredicate` that can be used to search for a `MatGridListHarness`
  68. * that meets certain criteria.
  69. * @param options Options for filtering which dialog instances are considered a match.
  70. * @return a `HarnessPredicate` configured with the given options.
  71. */
  72. static with(options = {}) {
  73. return new HarnessPredicate(MatGridListHarness, options);
  74. }
  75. /**
  76. * Tile coordinator that is used by the "MatGridList" for computing
  77. * positions of tiles. We leverage the coordinator to provide an API
  78. * for retrieving tiles based on visual tile positions.
  79. */
  80. _tileCoordinator = new _TileCoordinator();
  81. /** Gets all tiles of the grid-list. */
  82. async getTiles(filters = {}) {
  83. return await this.locatorForAll(MatGridTileHarness.with(filters))();
  84. }
  85. /** Gets the amount of columns of the grid-list. */
  86. async getColumns() {
  87. return Number(await (await this.host()).getAttribute('cols'));
  88. }
  89. /**
  90. * Gets a tile of the grid-list that is located at the given location.
  91. * @param row Zero-based row index.
  92. * @param column Zero-based column index.
  93. */
  94. async getTileAtPosition({ row, column, }) {
  95. const [tileHarnesses, columns] = await parallel(() => [this.getTiles(), this.getColumns()]);
  96. const tileSpans = tileHarnesses.map(t => parallel(() => [t.getColspan(), t.getRowspan()]));
  97. const tiles = (await parallel(() => tileSpans)).map(([colspan, rowspan]) => ({
  98. colspan,
  99. rowspan,
  100. }));
  101. // Update the tile coordinator to reflect the current column amount and
  102. // rendered tiles. We update upon every call of this method since we do not
  103. // know if tiles have been added, removed or updated (in terms of rowspan/colspan).
  104. this._tileCoordinator.update(columns, tiles);
  105. // The tile coordinator respects the colspan and rowspan for calculating the positions
  106. // of tiles, but it does not create multiple position entries if a tile spans over multiple
  107. // columns or rows. We want to provide an API where developers can retrieve a tile based on
  108. // any position that lies within the visual tile boundaries. For example: If a tile spans
  109. // over two columns, then the same tile should be returned for either column indices.
  110. for (let i = 0; i < this._tileCoordinator.positions.length; i++) {
  111. const position = this._tileCoordinator.positions[i];
  112. const { rowspan, colspan } = tiles[i];
  113. // Return the tile harness if the given position visually resolves to the tile.
  114. if (column >= position.col &&
  115. column <= position.col + colspan - 1 &&
  116. row >= position.row &&
  117. row <= position.row + rowspan - 1) {
  118. return tileHarnesses[i];
  119. }
  120. }
  121. throw Error('Could not find tile at given position.');
  122. }
  123. }
  124. export { MatGridListHarness, MatGridTileHarness, MatGridTileSection };
  125. //# sourceMappingURL=testing.mjs.map