[6a3a178] | 1 | import { __awaiter } from 'tslib';
|
---|
| 2 | import { ContentContainerComponentHarness, HarnessPredicate, ComponentHarness, parallel } from '@angular/cdk/testing';
|
---|
| 3 |
|
---|
| 4 | /**
|
---|
| 5 | * @license
|
---|
| 6 | * Copyright Google LLC All Rights Reserved.
|
---|
| 7 | *
|
---|
| 8 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 9 | * found in the LICENSE file at https://angular.io/license
|
---|
| 10 | */
|
---|
| 11 | /** Harness for interacting with a standard Angular Material table cell. */
|
---|
| 12 | class MatCellHarness extends ContentContainerComponentHarness {
|
---|
| 13 | /**
|
---|
| 14 | * Gets a `HarnessPredicate` that can be used to search for a table cell with specific attributes.
|
---|
| 15 | * @param options Options for narrowing the search
|
---|
| 16 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 17 | */
|
---|
| 18 | static with(options = {}) {
|
---|
| 19 | return MatCellHarness._getCellPredicate(MatCellHarness, options);
|
---|
| 20 | }
|
---|
| 21 | /** Gets the cell's text. */
|
---|
| 22 | getText() {
|
---|
| 23 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 24 | return (yield this.host()).text();
|
---|
| 25 | });
|
---|
| 26 | }
|
---|
| 27 | /** Gets the name of the column that the cell belongs to. */
|
---|
| 28 | getColumnName() {
|
---|
| 29 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 30 | const host = yield this.host();
|
---|
| 31 | const classAttribute = yield host.getAttribute('class');
|
---|
| 32 | if (classAttribute) {
|
---|
| 33 | const prefix = 'mat-column-';
|
---|
| 34 | const name = classAttribute.split(' ').map(c => c.trim()).find(c => c.startsWith(prefix));
|
---|
| 35 | if (name) {
|
---|
| 36 | return name.split(prefix)[1];
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 | throw Error('Could not determine column name of cell.');
|
---|
| 40 | });
|
---|
| 41 | }
|
---|
| 42 | static _getCellPredicate(type, options) {
|
---|
| 43 | return new HarnessPredicate(type, options)
|
---|
| 44 | .addOption('text', options.text, (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
|
---|
| 45 | .addOption('columnName', options.columnName, (harness, name) => HarnessPredicate.stringMatches(harness.getColumnName(), name));
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | /** The selector for the host element of a `MatCellHarness` instance. */
|
---|
| 49 | MatCellHarness.hostSelector = '.mat-cell';
|
---|
| 50 | /** Harness for interacting with a standard Angular Material table header cell. */
|
---|
| 51 | class MatHeaderCellHarness extends MatCellHarness {
|
---|
| 52 | /**
|
---|
| 53 | * Gets a `HarnessPredicate` that can be used to search for
|
---|
| 54 | * a table header cell with specific attributes.
|
---|
| 55 | * @param options Options for narrowing the search
|
---|
| 56 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 57 | */
|
---|
| 58 | static with(options = {}) {
|
---|
| 59 | return MatHeaderCellHarness._getCellPredicate(MatHeaderCellHarness, options);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | /** The selector for the host element of a `MatHeaderCellHarness` instance. */
|
---|
| 63 | MatHeaderCellHarness.hostSelector = '.mat-header-cell';
|
---|
| 64 | /** Harness for interacting with a standard Angular Material table footer cell. */
|
---|
| 65 | class MatFooterCellHarness extends MatCellHarness {
|
---|
| 66 | /**
|
---|
| 67 | * Gets a `HarnessPredicate` that can be used to search for
|
---|
| 68 | * a table footer cell with specific attributes.
|
---|
| 69 | * @param options Options for narrowing the search
|
---|
| 70 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 71 | */
|
---|
| 72 | static with(options = {}) {
|
---|
| 73 | return MatFooterCellHarness._getCellPredicate(MatFooterCellHarness, options);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | /** The selector for the host element of a `MatFooterCellHarness` instance. */
|
---|
| 77 | MatFooterCellHarness.hostSelector = '.mat-footer-cell';
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @license
|
---|
| 81 | * Copyright Google LLC All Rights Reserved.
|
---|
| 82 | *
|
---|
| 83 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 84 | * found in the LICENSE file at https://angular.io/license
|
---|
| 85 | */
|
---|
| 86 | class _MatRowHarnessBase extends ComponentHarness {
|
---|
| 87 | /** Gets a list of `MatCellHarness` for all cells in the row. */
|
---|
| 88 | getCells(filter = {}) {
|
---|
| 89 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 90 | return this.locatorForAll(this._cellHarness.with(filter))();
|
---|
| 91 | });
|
---|
| 92 | }
|
---|
| 93 | /** Gets the text of the cells in the row. */
|
---|
| 94 | getCellTextByIndex(filter = {}) {
|
---|
| 95 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 96 | const cells = yield this.getCells(filter);
|
---|
| 97 | return parallel(() => cells.map(cell => cell.getText()));
|
---|
| 98 | });
|
---|
| 99 | }
|
---|
| 100 | /** Gets the text inside the row organized by columns. */
|
---|
| 101 | getCellTextByColumnName() {
|
---|
| 102 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 103 | const output = {};
|
---|
| 104 | const cells = yield this.getCells();
|
---|
| 105 | const cellsData = yield parallel(() => cells.map(cell => {
|
---|
| 106 | return parallel(() => [cell.getColumnName(), cell.getText()]);
|
---|
| 107 | }));
|
---|
| 108 | cellsData.forEach(([columnName, text]) => output[columnName] = text);
|
---|
| 109 | return output;
|
---|
| 110 | });
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | /** Harness for interacting with a standard Angular Material table row. */
|
---|
| 114 | class MatRowHarness extends _MatRowHarnessBase {
|
---|
| 115 | constructor() {
|
---|
| 116 | super(...arguments);
|
---|
| 117 | this._cellHarness = MatCellHarness;
|
---|
| 118 | }
|
---|
| 119 | /**
|
---|
| 120 | * Gets a `HarnessPredicate` that can be used to search for a table row with specific attributes.
|
---|
| 121 | * @param options Options for narrowing the search
|
---|
| 122 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 123 | */
|
---|
| 124 | static with(options = {}) {
|
---|
| 125 | return new HarnessPredicate(MatRowHarness, options);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | /** The selector for the host element of a `MatRowHarness` instance. */
|
---|
| 129 | MatRowHarness.hostSelector = '.mat-row';
|
---|
| 130 | /** Harness for interacting with a standard Angular Material table header row. */
|
---|
| 131 | class MatHeaderRowHarness extends _MatRowHarnessBase {
|
---|
| 132 | constructor() {
|
---|
| 133 | super(...arguments);
|
---|
| 134 | this._cellHarness = MatHeaderCellHarness;
|
---|
| 135 | }
|
---|
| 136 | /**
|
---|
| 137 | * Gets a `HarnessPredicate` that can be used to search for
|
---|
| 138 | * a table header row with specific attributes.
|
---|
| 139 | * @param options Options for narrowing the search
|
---|
| 140 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 141 | */
|
---|
| 142 | static with(options = {}) {
|
---|
| 143 | return new HarnessPredicate(MatHeaderRowHarness, options);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | /** The selector for the host element of a `MatHeaderRowHarness` instance. */
|
---|
| 147 | MatHeaderRowHarness.hostSelector = '.mat-header-row';
|
---|
| 148 | /** Harness for interacting with a standard Angular Material table footer row. */
|
---|
| 149 | class MatFooterRowHarness extends _MatRowHarnessBase {
|
---|
| 150 | constructor() {
|
---|
| 151 | super(...arguments);
|
---|
| 152 | this._cellHarness = MatFooterCellHarness;
|
---|
| 153 | }
|
---|
| 154 | /**
|
---|
| 155 | * Gets a `HarnessPredicate` that can be used to search for
|
---|
| 156 | * a table footer row cell with specific attributes.
|
---|
| 157 | * @param options Options for narrowing the search
|
---|
| 158 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 159 | */
|
---|
| 160 | static with(options = {}) {
|
---|
| 161 | return new HarnessPredicate(MatFooterRowHarness, options);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | /** The selector for the host element of a `MatFooterRowHarness` instance. */
|
---|
| 165 | MatFooterRowHarness.hostSelector = '.mat-footer-row';
|
---|
| 166 |
|
---|
| 167 | /**
|
---|
| 168 | * @license
|
---|
| 169 | * Copyright Google LLC All Rights Reserved.
|
---|
| 170 | *
|
---|
| 171 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 172 | * found in the LICENSE file at https://angular.io/license
|
---|
| 173 | */
|
---|
| 174 | class _MatTableHarnessBase extends ContentContainerComponentHarness {
|
---|
| 175 | /** Gets all of the header rows in a table. */
|
---|
| 176 | getHeaderRows(filter = {}) {
|
---|
| 177 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 178 | return this.locatorForAll(this._headerRowHarness.with(filter))();
|
---|
| 179 | });
|
---|
| 180 | }
|
---|
| 181 | /** Gets all of the regular data rows in a table. */
|
---|
| 182 | getRows(filter = {}) {
|
---|
| 183 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 184 | return this.locatorForAll(this._rowHarness.with(filter))();
|
---|
| 185 | });
|
---|
| 186 | }
|
---|
| 187 | /** Gets all of the footer rows in a table. */
|
---|
| 188 | getFooterRows(filter = {}) {
|
---|
| 189 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 190 | return this.locatorForAll(this._footerRowHarness.with(filter))();
|
---|
| 191 | });
|
---|
| 192 | }
|
---|
| 193 | /** Gets the text inside the entire table organized by rows. */
|
---|
| 194 | getCellTextByIndex() {
|
---|
| 195 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 196 | const rows = yield this.getRows();
|
---|
| 197 | return parallel(() => rows.map(row => row.getCellTextByIndex()));
|
---|
| 198 | });
|
---|
| 199 | }
|
---|
| 200 | /** Gets the text inside the entire table organized by columns. */
|
---|
| 201 | getCellTextByColumnName() {
|
---|
| 202 | return __awaiter(this, void 0, void 0, function* () {
|
---|
| 203 | const [headerRows, footerRows, dataRows] = yield parallel(() => [
|
---|
| 204 | this.getHeaderRows(),
|
---|
| 205 | this.getFooterRows(),
|
---|
| 206 | this.getRows()
|
---|
| 207 | ]);
|
---|
| 208 | const text = {};
|
---|
| 209 | const [headerData, footerData, rowsData] = yield parallel(() => [
|
---|
| 210 | parallel(() => headerRows.map(row => row.getCellTextByColumnName())),
|
---|
| 211 | parallel(() => footerRows.map(row => row.getCellTextByColumnName())),
|
---|
| 212 | parallel(() => dataRows.map(row => row.getCellTextByColumnName())),
|
---|
| 213 | ]);
|
---|
| 214 | rowsData.forEach(data => {
|
---|
| 215 | Object.keys(data).forEach(columnName => {
|
---|
| 216 | const cellText = data[columnName];
|
---|
| 217 | if (!text[columnName]) {
|
---|
| 218 | text[columnName] = {
|
---|
| 219 | headerText: getCellTextsByColumn(headerData, columnName),
|
---|
| 220 | footerText: getCellTextsByColumn(footerData, columnName),
|
---|
| 221 | text: []
|
---|
| 222 | };
|
---|
| 223 | }
|
---|
| 224 | text[columnName].text.push(cellText);
|
---|
| 225 | });
|
---|
| 226 | });
|
---|
| 227 | return text;
|
---|
| 228 | });
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | /** Harness for interacting with a standard mat-table in tests. */
|
---|
| 232 | class MatTableHarness extends _MatTableHarnessBase {
|
---|
| 233 | constructor() {
|
---|
| 234 | super(...arguments);
|
---|
| 235 | this._headerRowHarness = MatHeaderRowHarness;
|
---|
| 236 | this._rowHarness = MatRowHarness;
|
---|
| 237 | this._footerRowHarness = MatFooterRowHarness;
|
---|
| 238 | }
|
---|
| 239 | /**
|
---|
| 240 | * Gets a `HarnessPredicate` that can be used to search for a table with specific attributes.
|
---|
| 241 | * @param options Options for narrowing the search
|
---|
| 242 | * @return a `HarnessPredicate` configured with the given options.
|
---|
| 243 | */
|
---|
| 244 | static with(options = {}) {
|
---|
| 245 | return new HarnessPredicate(MatTableHarness, options);
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | /** The selector for the host element of a `MatTableHarness` instance. */
|
---|
| 249 | MatTableHarness.hostSelector = '.mat-table';
|
---|
| 250 | /** Extracts the text of cells only under a particular column. */
|
---|
| 251 | function getCellTextsByColumn(rowsData, column) {
|
---|
| 252 | const columnTexts = [];
|
---|
| 253 | rowsData.forEach(data => {
|
---|
| 254 | Object.keys(data).forEach(columnName => {
|
---|
| 255 | if (columnName === column) {
|
---|
| 256 | columnTexts.push(data[columnName]);
|
---|
| 257 | }
|
---|
| 258 | });
|
---|
| 259 | });
|
---|
| 260 | return columnTexts;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
| 264 | * @license
|
---|
| 265 | * Copyright Google LLC All Rights Reserved.
|
---|
| 266 | *
|
---|
| 267 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 268 | * found in the LICENSE file at https://angular.io/license
|
---|
| 269 | */
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * @license
|
---|
| 273 | * Copyright Google LLC All Rights Reserved.
|
---|
| 274 | *
|
---|
| 275 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 276 | * found in the LICENSE file at https://angular.io/license
|
---|
| 277 | */
|
---|
| 278 |
|
---|
| 279 | export { MatCellHarness, MatFooterCellHarness, MatFooterRowHarness, MatHeaderCellHarness, MatHeaderRowHarness, MatRowHarness, MatTableHarness, _MatRowHarnessBase, _MatTableHarnessBase };
|
---|
| 280 | //# sourceMappingURL=testing.js.map
|
---|