testing.mjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { ContentContainerComponentHarness, HarnessPredicate, ComponentHarness, parallel } from '@angular/cdk/testing';
  2. class _MatCellHarnessBase extends ContentContainerComponentHarness {
  3. /** Gets the cell's text. */
  4. async getText() {
  5. return (await this.host()).text();
  6. }
  7. /** Gets the name of the column that the cell belongs to. */
  8. async getColumnName() {
  9. const host = await this.host();
  10. const classAttribute = await host.getAttribute('class');
  11. if (classAttribute) {
  12. const prefix = 'mat-column-';
  13. const name = classAttribute
  14. .split(' ')
  15. .map(c => c.trim())
  16. .find(c => c.startsWith(prefix));
  17. if (name) {
  18. return name.split(prefix)[1];
  19. }
  20. }
  21. throw Error('Could not determine column name of cell.');
  22. }
  23. static _getCellPredicate(type, options) {
  24. return new HarnessPredicate(type, options)
  25. .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
  26. .addOption('columnName', options.columnName, (harness, name) => HarnessPredicate.stringMatches(harness.getColumnName(), name));
  27. }
  28. }
  29. /** Harness for interacting with an Angular Material table cell. */
  30. class MatCellHarness extends _MatCellHarnessBase {
  31. /** The selector for the host element of a `MatCellHarness` instance. */
  32. static hostSelector = '.mat-mdc-cell';
  33. /**
  34. * Gets a `HarnessPredicate` that can be used to search for a table cell with specific attributes.
  35. * @param options Options for narrowing the search
  36. * @return a `HarnessPredicate` configured with the given options.
  37. */
  38. static with(options = {}) {
  39. return _MatCellHarnessBase._getCellPredicate(this, options);
  40. }
  41. }
  42. /** Harness for interacting with an Angular Material table header cell. */
  43. class MatHeaderCellHarness extends _MatCellHarnessBase {
  44. /** The selector for the host element of a `MatHeaderCellHarness` instance. */
  45. static hostSelector = '.mat-mdc-header-cell';
  46. /**
  47. * Gets a `HarnessPredicate` that can be used to search for a table header cell with specific
  48. * attributes.
  49. * @param options Options for narrowing the search
  50. * @return a `HarnessPredicate` configured with the given options.
  51. */
  52. static with(options = {}) {
  53. return _MatCellHarnessBase._getCellPredicate(this, options);
  54. }
  55. }
  56. /** Harness for interacting with an Angular Material table footer cell. */
  57. class MatFooterCellHarness extends _MatCellHarnessBase {
  58. /** The selector for the host element of a `MatFooterCellHarness` instance. */
  59. static hostSelector = '.mat-mdc-footer-cell';
  60. /**
  61. * Gets a `HarnessPredicate` that can be used to search for a table footer cell with specific
  62. * attributes.
  63. * @param options Options for narrowing the search
  64. * @return a `HarnessPredicate` configured with the given options.
  65. */
  66. static with(options = {}) {
  67. return _MatCellHarnessBase._getCellPredicate(this, options);
  68. }
  69. }
  70. class _MatRowHarnessBase extends ComponentHarness {
  71. /** Gets a list of `MatCellHarness` for all cells in the row. */
  72. async getCells(filter = {}) {
  73. return this.locatorForAll(this._cellHarness.with(filter))();
  74. }
  75. /** Gets the text of the cells in the row. */
  76. async getCellTextByIndex(filter = {}) {
  77. const cells = await this.getCells(filter);
  78. return parallel(() => cells.map(cell => cell.getText()));
  79. }
  80. /** Gets the text inside the row organized by columns. */
  81. async getCellTextByColumnName() {
  82. const output = {};
  83. const cells = await this.getCells();
  84. const cellsData = await parallel(() => cells.map(cell => {
  85. return parallel(() => [cell.getColumnName(), cell.getText()]);
  86. }));
  87. cellsData.forEach(([columnName, text]) => (output[columnName] = text));
  88. return output;
  89. }
  90. }
  91. /** Harness for interacting with an Angular Material table row. */
  92. class MatRowHarness extends _MatRowHarnessBase {
  93. /** The selector for the host element of a `MatRowHarness` instance. */
  94. static hostSelector = '.mat-mdc-row';
  95. _cellHarness = MatCellHarness;
  96. /**
  97. * Gets a `HarnessPredicate` that can be used to search for a table row with specific attributes.
  98. * @param options Options for narrowing the search
  99. * @return a `HarnessPredicate` configured with the given options.
  100. */
  101. static with(options = {}) {
  102. return new HarnessPredicate(this, options);
  103. }
  104. }
  105. /** Harness for interacting with an Angular Material table header row. */
  106. class MatHeaderRowHarness extends _MatRowHarnessBase {
  107. /** The selector for the host element of a `MatHeaderRowHarness` instance. */
  108. static hostSelector = '.mat-mdc-header-row';
  109. _cellHarness = MatHeaderCellHarness;
  110. /**
  111. * Gets a `HarnessPredicate` that can be used to search for a table header row with specific
  112. * attributes.
  113. * @param options Options for narrowing the search
  114. * @return a `HarnessPredicate` configured with the given options.
  115. */
  116. static with(options = {}) {
  117. return new HarnessPredicate(this, options);
  118. }
  119. }
  120. /** Harness for interacting with an Angular Material table footer row. */
  121. class MatFooterRowHarness extends _MatRowHarnessBase {
  122. /** The selector for the host element of a `MatFooterRowHarness` instance. */
  123. static hostSelector = '.mat-mdc-footer-row';
  124. _cellHarness = MatFooterCellHarness;
  125. /**
  126. * Gets a `HarnessPredicate` that can be used to search for a table footer row cell with specific
  127. * attributes.
  128. * @param options Options for narrowing the search
  129. * @return a `HarnessPredicate` configured with the given options.
  130. */
  131. static with(options = {}) {
  132. return new HarnessPredicate(this, options);
  133. }
  134. }
  135. /** Harness for interacting with a mat-table in tests. */
  136. class MatTableHarness extends ContentContainerComponentHarness {
  137. /** The selector for the host element of a `MatTableHarness` instance. */
  138. static hostSelector = '.mat-mdc-table';
  139. _headerRowHarness = MatHeaderRowHarness;
  140. _rowHarness = MatRowHarness;
  141. _footerRowHarness = MatFooterRowHarness;
  142. /**
  143. * Gets a `HarnessPredicate` that can be used to search for a table with specific attributes.
  144. * @param options Options for narrowing the search
  145. * @return a `HarnessPredicate` configured with the given options.
  146. */
  147. static with(options = {}) {
  148. return new HarnessPredicate(this, options);
  149. }
  150. /** Gets all the header rows in a table. */
  151. async getHeaderRows(filter = {}) {
  152. return this.locatorForAll(this._headerRowHarness.with(filter))();
  153. }
  154. /** Gets all the regular data rows in a table. */
  155. async getRows(filter = {}) {
  156. return this.locatorForAll(this._rowHarness.with(filter))();
  157. }
  158. /** Gets all the footer rows in a table. */
  159. async getFooterRows(filter = {}) {
  160. return this.locatorForAll(this._footerRowHarness.with(filter))();
  161. }
  162. /** Gets the text inside the entire table organized by rows. */
  163. async getCellTextByIndex() {
  164. const rows = await this.getRows();
  165. return parallel(() => rows.map(row => row.getCellTextByIndex()));
  166. }
  167. /** Gets the text inside the entire table organized by columns. */
  168. async getCellTextByColumnName() {
  169. const [headerRows, footerRows, dataRows] = await parallel(() => [
  170. this.getHeaderRows(),
  171. this.getFooterRows(),
  172. this.getRows(),
  173. ]);
  174. const text = {};
  175. const [headerData, footerData, rowsData] = await parallel(() => [
  176. parallel(() => headerRows.map(row => row.getCellTextByColumnName())),
  177. parallel(() => footerRows.map(row => row.getCellTextByColumnName())),
  178. parallel(() => dataRows.map(row => row.getCellTextByColumnName())),
  179. ]);
  180. rowsData.forEach(data => {
  181. Object.keys(data).forEach(columnName => {
  182. const cellText = data[columnName];
  183. if (!text[columnName]) {
  184. text[columnName] = {
  185. headerText: getCellTextsByColumn(headerData, columnName),
  186. footerText: getCellTextsByColumn(footerData, columnName),
  187. text: [],
  188. };
  189. }
  190. text[columnName].text.push(cellText);
  191. });
  192. });
  193. return text;
  194. }
  195. }
  196. /** Extracts the text of cells only under a particular column. */
  197. function getCellTextsByColumn(rowsData, column) {
  198. const columnTexts = [];
  199. rowsData.forEach(data => {
  200. Object.keys(data).forEach(columnName => {
  201. if (columnName === column) {
  202. columnTexts.push(data[columnName]);
  203. }
  204. });
  205. });
  206. return columnTexts;
  207. }
  208. export { MatCellHarness, MatFooterCellHarness, MatFooterRowHarness, MatHeaderCellHarness, MatHeaderRowHarness, MatRowHarness, MatTableHarness, _MatCellHarnessBase, _MatRowHarnessBase };
  209. //# sourceMappingURL=testing.mjs.map