@atomic-testing/component-driver-mui-x-v8
Version:
Atomic Testing Component driver to help drive Material UI X V8 components
381 lines (373 loc) • 13.7 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __atomic_testing_core = __toESM(require("@atomic-testing/core"));
const __atomic_testing_component_driver_html = __toESM(require("@atomic-testing/component-driver-html"));
//#region src/components/datagrid/DataGridRowDriverBase.ts
const columnStartingIndex = 1;
/**
* Base class for data grid row
*/
var DataGridRowDriverBase = class extends __atomic_testing_core.ComponentDriver {
async getCellCount() {
let count = 0;
for await (const _ of __atomic_testing_core.listHelper.getListItemIterator(this, this.getCellLocator(), __atomic_testing_component_driver_html.HTMLElementDriver, columnStartingIndex)) count++;
return count;
}
/**
* Get the text of each visible cell in the row.
* Caveat: Because of virtualization, the text of the cell may not be available until the cell is visible.
* @returns A promise array of text of each visible cell in the row
*/
async getRowText() {
const textList = [];
for await (const cell of __atomic_testing_core.listHelper.getListItemIterator(this, this.getCellLocator(), __atomic_testing_component_driver_html.HTMLElementDriver, columnStartingIndex)) {
const text = await cell.getText();
textList.push(text.trim());
}
return textList;
}
/**
* Get the cell driver at the specified index or data field.
* Caveat: Because of virtualization, the cell may not be available until the cell is visible.
* @param cellIndexOrField number: column index, string: column field
* @param driverClass The driver class of the cell. Default is HTMLElementDriver
* @returns A promise of the cell driver, or null if the cell is not found
*/
async getCell(cellIndexOrField, driverClass = __atomic_testing_component_driver_html.HTMLElementDriver) {
let cellLocator;
if (typeof cellIndexOrField === "number") cellLocator = (0, __atomic_testing_core.byAttribute)("data-colindex", cellIndexOrField.toString());
else cellLocator = (0, __atomic_testing_core.byAttribute)("data-field", cellIndexOrField);
const locator = __atomic_testing_core.locatorUtil.append(this.locator, cellLocator);
const cellExists = await this.interactor.exists(locator);
if (cellExists) return new driverClass(locator, this.interactor, this.commutableOption);
return null;
}
};
//#endregion
//#region src/components/datagrid/DataGridDataRowDriver.ts
var DataGridDataRowDriver = class extends DataGridRowDriverBase {
_dataCellLocator;
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts: {}
});
this._dataCellLocator = __atomic_testing_core.locatorUtil.append(locator, (0, __atomic_testing_core.byRole)("cell"));
}
getCellLocator() {
return this._dataCellLocator;
}
get driverName() {
return "MuiV8DataGridDataRowDriver";
}
};
//#endregion
//#region src/components/datagrid/DataGridHeaderRowDriver.ts
var DataGridHeaderRowDriver = class extends DataGridRowDriverBase {
_headerCellLocator;
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts: {}
});
this._headerCellLocator = __atomic_testing_core.locatorUtil.append(locator, (0, __atomic_testing_core.byRole)("columnheader"));
}
async getColumnCount() {
return this.getCellCount();
}
getCellLocator() {
return this._headerCellLocator;
}
get driverName() {
return "MuiV8DataGridHeaderRowDriver";
}
};
//#endregion
//#region src/components/datagrid/DataGridPaginationActionDriver.ts
const parts$2 = {
previousButton: {
locator: (0, __atomic_testing_core.byAttribute)("aria-label", "Go to previous page"),
driver: __atomic_testing_component_driver_html.HTMLButtonDriver
},
nextButton: {
locator: (0, __atomic_testing_core.byAttribute)("aria-label", "Go to next page"),
driver: __atomic_testing_component_driver_html.HTMLButtonDriver
}
};
/**
* Driver for Material UI v6 DataGridPro component.
* @see https://mui.com/x/react-data-grid/
*/
var DataGridPaginationActionDriver = class extends __atomic_testing_core.ComponentDriver {
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts: parts$2
});
}
async isPreviousPageEnabled() {
await this.enforcePartExistence("previousButton");
const isDisabled = await this.parts.previousButton.isDisabled();
return !isDisabled;
}
async gotoPreviousPage() {
await this.enforcePartExistence("previousButton");
await this.parts.previousButton.click();
}
async isNextPageEnabled() {
await this.enforcePartExistence("nextButton");
const isDisabled = await this.parts.nextButton.isDisabled();
return !isDisabled;
}
async gotoNextPage() {
await this.enforcePartExistence("nextButton");
await this.parts.nextButton.click();
}
get driverName() {
return "MuiV8DataGridPaginationActionDriver";
}
};
//#endregion
//#region src/components/datagrid/DataGridFooterDriver.ts
const parts$1 = {
paginationAction: {
locator: (0, __atomic_testing_core.byCssClass)("MuiTablePagination-actions"),
driver: DataGridPaginationActionDriver
},
paginationDescription: {
locator: (0, __atomic_testing_core.byCssClass)("MuiTablePagination-displayedRows"),
driver: __atomic_testing_component_driver_html.HTMLElementDriver
}
};
/**
* Driver for Material UI v6 DataGridPro component.
* @see https://mui.com/x/react-data-grid/
*/
var DataGridFooterDriver = class extends __atomic_testing_core.ComponentDriver {
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts: parts$1
});
}
async isPreviousPageEnabled() {
await this.enforcePartExistence("paginationAction");
return this.parts.paginationAction.isPreviousPageEnabled();
}
async gotoPreviousPage() {
await this.enforcePartExistence("paginationAction");
await this.parts.paginationAction.gotoPreviousPage();
}
async isNextPageEnabled() {
await this.enforcePartExistence("paginationAction");
return this.parts.paginationAction.isNextPageEnabled();
}
async gotoNextPage() {
await this.enforcePartExistence("paginationAction");
await this.parts.paginationAction.gotoNextPage();
}
async getPaginationDescription() {
await this.enforcePartExistence("paginationDescription");
return this.parts.paginationDescription.getText();
}
get driverName() {
return "MuiV8DataGridFooterDriver";
}
};
//#endregion
//#region src/components/datagrid/DataGridProDriver.ts
const parts = {
headerRow: {
locator: (0, __atomic_testing_core.byCssClass)("MuiDataGrid-columnHeaders").chain((0, __atomic_testing_core.byCssSelector)("[role=row]:first-of-type")),
driver: DataGridHeaderRowDriver
},
loading: {
locator: (0, __atomic_testing_core.byRole)("progressbar"),
driver: __atomic_testing_component_driver_html.HTMLElementDriver
},
skeletonOverlay: {
locator: (0, __atomic_testing_core.byCssClass)("MuiDataGrid-main--hasSkeletonLoadingOverlay"),
driver: __atomic_testing_component_driver_html.HTMLElementDriver
},
footer: {
locator: (0, __atomic_testing_core.byCssClass)("MuiDataGrid-footerContainer"),
driver: DataGridFooterDriver
}
};
const dataRowLocator = (0, __atomic_testing_core.byCssSelector)("[role=row][data-rowindex]");
/**
* Driver for Material UI v8 DataGridPro component.
* V8 DataGridPro component does not support data-testid, to use data-testid
* to locate the component, you need to put the data-testid on the parent element of the grid
* @see https://mui.com/x/react-data-grid/
*/
var DataGridProDriver = class extends __atomic_testing_core.ComponentDriver {
constructor(locator, interactor, option) {
super(locator, interactor, {
...option,
parts
});
}
/**
* Checks if the data grid is currently loading.
* @returns A promise that resolves to a boolean indicating if the data grid is loading.
*/
async isLoading() {
const result = await Promise.all([this.parts.skeletonOverlay.isVisible(), this.parts.loading.isVisible()]);
return result.some((v) => v);
}
/**
* Waits for the data grid to exit the loading state.
* @param timeoutMs The maximum time to wait for the load to complete, in milliseconds.
*/
async waitForLoad(timeoutMs = 1e4) {
await this.parts.headerRow.waitUntilComponentState();
await this.waitUntil({
probeFn: () => this.isLoading(),
terminateCondition: false,
timeoutMs
});
}
/**
* The number of columns currently displayed in the data grid, note that data grid pro
* uses virtualize rendering, therefore the column count heavily depends on the viewport size
* @returns The number of columns currently displayed in the data grid
*/
async getColumnCount() {
return this.parts.headerRow.getColumnCount();
}
/**
* The array text of the header row, note that columns not shown in the viewport may not be included because of virtualize rendering
* @returns The array of text of the header row
*/
async getHeaderText() {
return this.parts.headerRow.getRowText();
}
/**
* The number of rows currently displayed in the data grid, note that data grid pro
* uses virtualize rendering, therefore the row count heavily depends on the viewport size
* @returns The number of columns currently displayed in the data grid
*/
async getRowCount() {
const gridRowLocator = __atomic_testing_core.locatorUtil.append(this.locator, dataRowLocator);
let count = 0;
for await (const _ of __atomic_testing_core.listHelper.getListItemIterator(this, gridRowLocator, __atomic_testing_component_driver_html.HTMLElementDriver)) count++;
return count;
}
/**
* Return the row driver for the row at the specified index, if the row does not exist, return null
* @param rowIndex
* @returns
*/
async getRow(rowIndex) {
const rowLocator = __atomic_testing_core.locatorUtil.append(this.locator, (0, __atomic_testing_core.byCssSelector)(`[role=row][data-rowindex="${rowIndex}"]`));
const rowExists = await this.interactor.exists(rowLocator);
if (rowExists) return new DataGridHeaderRowDriver(rowLocator, this.interactor, this.commutableOption);
return null;
}
/**
* The array text of the specified row, note that columns not shown in the viewport may not be included because of virtualize rendering
* @param rowIndex The index of the row
* @returns The array of text of the specified row
*/
async getRowText(rowIndex) {
const row = await this.getRow(rowIndex);
if (row != null) return row.getRowText();
throw new Error(`Row ${rowIndex} does not exist`);
}
/**
* Get the cell driver for the cell, if the cell does not exist, return null
* The cell driver is default to HTMLElementDriver, you can specify a different driver class
* @param query The query to locate the cell
* @param driverClass Optional, the driver class to use for the cell, default to HTMLElementDriver
* @returns
*/
async getCell(query, driverClass = __atomic_testing_component_driver_html.HTMLElementDriver) {
const rowDriver = await this.getRow(query.rowIndex);
if (rowDriver === null) return null;
if ("columnIndex" in query) return rowDriver.getCell(query.columnIndex, driverClass);
return rowDriver.getCell(query.columnField, driverClass);
}
/**
* Get the text content of the cell, if the cell does not exist, throw an error
* @param query The query to locate the cell
* @returns
*/
async getCellText(query) {
const cell = await this.getCell(query);
if (cell != null) {
const text = await cell.getText();
return text;
}
throw new Error(`Cell at row:${query.rowIndex} column:${query.columnIndex ?? query.columnField} does not exist`);
}
/**
* Determine if the pagination footer is currently visible.
*/
isFooterVisible() {
return this.parts.footer.isVisible();
}
/**
* Check whether the "previous page" control is enabled.
*/
async isPreviousPageEnabled() {
await this.enforcePartExistence("footer");
return this.parts.footer.isPreviousPageEnabled();
}
/**
* Navigate to the previous page using the grid footer control.
*/
async gotoPreviousPage() {
await this.enforcePartExistence("footer");
await this.parts.footer.gotoPreviousPage();
}
/**
* Check whether the "next page" control is enabled.
*/
async isNextPageEnabled() {
await this.enforcePartExistence("footer");
return this.parts.footer.isNextPageEnabled();
}
/**
* Navigate to the next page using the grid footer control.
*/
async gotoNextPage() {
await this.enforcePartExistence("footer");
await this.parts.footer.gotoNextPage();
}
/**
* Read the textual description of the current pagination state.
*/
async getPaginationDescription() {
await this.enforcePartExistence("footer");
return this.parts.footer.getText();
}
get driverName() {
return "MuiV8DataGridProDriver";
}
};
//#endregion
exports.DataGridDataRowDriver = DataGridDataRowDriver;
exports.DataGridHeaderRowDriver = DataGridHeaderRowDriver;
exports.DataGridProDriver = DataGridProDriver;
//# sourceMappingURL=index.js.map