ghost-portal/packages/test-support/src/pagesElements/Table.ts
Jaco eacf028ba6
Use .js imports in src (#9132)
* Use .js imports in src

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Add WP extension aliases

* Bump dev

* Additional

* Additional

* Adjust extensionAlias order
2023-03-09 12:11:03 +02:00

64 lines
2.0 KiB
TypeScript

// Copyright 2017-2023 @polkadot/test-supports authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { within } from '@testing-library/react';
import { showBalance } from '../utils/balance.js';
import { Row } from './Row.js';
export class Table {
constructor (private readonly table: HTMLElement, private readonly rowClassName: string) {
this.table = table;
this.rowClassName = rowClassName;
}
async assertRowsOrder (balancesExpectedOrder: number[]): Promise<void> {
const orderedRows = await this.getRows();
for (let index = 0; index < orderedRows.length; index++) {
const row = orderedRows[index];
const expectedBalanceTextContent = showBalance(balancesExpectedOrder[index]);
expect(await row.getBalanceSummary()).toHaveTextContent(expectedBalanceTextContent);
}
}
async getRows (): Promise<Row[]> {
const htmlRows = await this.getFilteredHtmlRows();
const collapsibleRows: Row[] = [];
for (let rowIdx = 0; rowIdx < htmlRows.length; rowIdx = rowIdx + 2) {
const primaryRow = htmlRows[rowIdx];
const detailsRow = htmlRows[rowIdx + 1];
collapsibleRows.push(new Row(primaryRow, detailsRow));
}
return collapsibleRows;
}
assertColumnNotExist (columnName: string): void {
expect(within(this.table).queryByRole('columnheader', { name: columnName })).toBeFalsy();
}
assertColumnExists (columnName: string): void {
expect(within(this.table).getByRole('columnheader', { name: columnName })).toBeTruthy();
}
async assertText (text: string): Promise<HTMLElement> {
return within(this.table).findByText(text);
}
private async getFilteredHtmlRows (): Promise<HTMLElement[]> {
const htmlRows = await this.getAllHtmlRows();
return htmlRows.filter((row) => row.className.startsWith(this.rowClassName));
}
private async getAllHtmlRows (): Promise<HTMLElement[]> {
const tableBody = this.table.getElementsByTagName('tbody')[0];
return within(tableBody).findAllByRole('row');
}
}