|
1 | 1 | import {Locator, Page, expect} from '@playwright/test' |
2 | 2 | import {Pagination} from './pagination' |
3 | | -import {generateTestId, TestId} from "./testIds"; |
| 3 | +import {generateTestId, TestId} from "../utils/testIds"; |
| 4 | +import {Filter, FilterType, FilterValues} from "./filter"; |
4 | 5 |
|
5 | 6 | interface GridCondition { |
6 | 7 | columnName: string |
@@ -55,6 +56,82 @@ export class Grid { |
55 | 56 | }) |
56 | 57 | } |
57 | 58 |
|
| 59 | + /** |
| 60 | + * Asserts that the grid headers match the given list. |
| 61 | + * Asserts that the pagination dropdown contains the given pagination options. |
| 62 | + * Asserts that the "rows per page" input has the expected value. |
| 63 | + * |
| 64 | + * @param headerNames - An array of expected header titles |
| 65 | + * @param paginationOptions - Array of expected pagination options (as strings) |
| 66 | + * @param rowsPerPage - Expected number of rows per page |
| 67 | + * |
| 68 | + */ |
| 69 | + public async expectHeadersAndPaginationToBe(headerNames: string[], paginationOptions: string[] = ['10', '25', '50'] , rowsPerPage: number = 50): Promise<void> { |
| 70 | + await this.expectHeadersToBe(headerNames) |
| 71 | + await this.pagination.expectToHaveOptions(paginationOptions) |
| 72 | + await this.pagination.expectToHaveRowsPerPage(rowsPerPage) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Assert that the expected rows are present after the filters have been applied. |
| 77 | + * |
| 78 | + * @param filter - Grid's filter |
| 79 | + * @param filtersToApply - Filters to apply |
| 80 | + * @param expectedRow - An array of objects containing: |
| 81 | + * - columnName: the header of the column to search in |
| 82 | + * - value: the expected value in that column |
| 83 | + * @param expectedRowCount - Expected row count |
| 84 | + */ |
| 85 | + public async expectRowsAfterFiltersToBe<TFilters extends Record<string, FilterType>>( |
| 86 | + filter: Filter<TFilters>, |
| 87 | + filtersToApply: FilterValues<TFilters>, |
| 88 | + expectedRow: GridCondition[], |
| 89 | + expectedRowCount: number = 1, |
| 90 | + ) { |
| 91 | + await filter.addFilters(filtersToApply) |
| 92 | + await this.pagination.expectToHaveCount(expectedRowCount) |
| 93 | + if (expectedRowCount !== 0 || expectedRow.length !== 0) { |
| 94 | + await this.expectToFindLineWhere(expectedRow) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Assert that the expected rows are present after the search has been done. |
| 100 | + * |
| 101 | + * @param filter - Grid's filter |
| 102 | + * @param term - The search term to input |
| 103 | + * @param expectedRow - An array of objects containing: |
| 104 | + * - columnName: the header of the column to search in |
| 105 | + * - value: the expected value in that column |
| 106 | + * @param expectedRowCount - Expected row count |
| 107 | + */ |
| 108 | + public async expectRowsAfterSearchToBe<TFilters extends Record<string, FilterType>>( |
| 109 | + filter: Filter<TFilters>, |
| 110 | + term: string, |
| 111 | + expectedRow: GridCondition[], |
| 112 | + expectedRowCount: number = 1, |
| 113 | + ) { |
| 114 | + await filter.searchTerm(term) |
| 115 | + await this.pagination.expectToHaveCount(expectedRowCount) |
| 116 | + if (expectedRowCount !== 0 || expectedRow.length !== 0) { |
| 117 | + await this.expectToFindLineWhere(expectedRow) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Assert that All filters have been removed. |
| 123 | + * |
| 124 | + * @param filter - Grid's filter |
| 125 | + * @param defaultRowCount - Row count without filters |
| 126 | + */ |
| 127 | + public async expectAllFiltersRemoved<TFilters extends Record<string, FilterType>>( |
| 128 | + filter: Filter<TFilters>, |
| 129 | + defaultRowCount: number, |
| 130 | + ) { |
| 131 | + await filter.clearFilters() |
| 132 | + await this.pagination.expectToHaveCount(defaultRowCount) |
| 133 | + } |
| 134 | + |
58 | 135 | /** |
59 | 136 | * Returns the total number of rows in the grid via the pagination component. |
60 | 137 | * |
@@ -136,6 +213,14 @@ export class Grid { |
136 | 213 | await expect(grid).toBeVisible() |
137 | 214 | } |
138 | 215 |
|
| 216 | + public static getCommonGridTestIds( |
| 217 | + resourceName: string, |
| 218 | + ) { |
| 219 | + return { |
| 220 | + createButton: generateTestId(TestId.GRID_CREATE_BUTTON, resourceName) |
| 221 | + } as const |
| 222 | + } |
| 223 | + |
139 | 224 | /** |
140 | 225 | * Lazily retrieves the grid Locator. |
141 | 226 | * |
|
0 commit comments