Skip to content

Commit 87a2541

Browse files
committed
feat(#1340374): Refacto e2e grid tests, and add e2e tests for recommender
pages
1 parent 0690e18 commit 87a2541

28 files changed

+983
-624
lines changed

front/e2e/src/helper/alertMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Locator, Page, expect } from '@playwright/test'
2-
import {generateTestId, TestId} from "./testIds";
2+
import {generateTestId, TestId} from "../utils/testIds";
33

44
/**
55
* Helper class to interact with alert messages on the page.

front/e2e/src/helper/dropdown.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expect, Locator, Page} from '@playwright/test'
2-
import {generateTestId, TestId} from "./testIds";
2+
import {generateTestId, TestId} from "../utils/testIds";
33

44
/**
55
* A generic Dropdown class to interact with single or multi-select dropdowns in Playwright.

front/e2e/src/helper/filter.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {expect, Locator, Page} from '@playwright/test'
22
import {Dropdown} from './dropdown'
3-
import {generateTestId, TestId} from "./testIds";
3+
import {generateTestId, TestId} from "../utils/testIds";
44

55
/**
66
* Enumeration of supported filter types.
@@ -12,6 +12,21 @@ export enum FilterType {
1212
RANGE,
1313
}
1414

15+
export type FilterValue<T extends FilterType> =
16+
T extends FilterType.BOOLEAN
17+
? boolean
18+
: T extends FilterType.TEXT
19+
? string
20+
: T extends FilterType.DROPDOWN
21+
? string[]
22+
: T extends FilterType.RANGE
23+
? [number, number]
24+
: never;
25+
26+
export type FilterValues<TFilters extends Record<string, FilterType>> = {
27+
[K in keyof TFilters]?: FilterValue<TFilters[K]>;
28+
};
29+
1530
/**
1631
* Generic class to manage and assert filters in a UI, supporting
1732
* various filter types like dropdowns, booleans, text inputs, and ranges.
@@ -177,6 +192,21 @@ export class Filter<TFilters extends Record<string, FilterType>> {
177192
}
178193
}
179194

195+
public async removeFilters(filters: Partial<{ [K in keyof TFilters]: any }>): Promise<void> {
196+
for (const name in filters) {
197+
const value = filters[name];
198+
199+
// If it's an array (only for DROPDOWN), iterate.
200+
if (Array.isArray(value) && this.filters[name] !== FilterType.RANGE) {
201+
for (const item of value) {
202+
await this.removeFilter(name as keyof TFilters & string, item);
203+
}
204+
} else {
205+
await this.removeFilter(name as keyof TFilters & string, value);
206+
}
207+
}
208+
}
209+
180210
/**
181211
* Adds a new filter based on its type and value.
182212
*
@@ -185,15 +215,7 @@ export class Filter<TFilters extends Record<string, FilterType>> {
185215
*/
186216
public async addFilter<Name extends keyof TFilters & string>(
187217
name: Name,
188-
value: TFilters[Name] extends FilterType.BOOLEAN
189-
? boolean
190-
: TFilters[Name] extends FilterType.TEXT
191-
? string
192-
: TFilters[Name] extends FilterType.DROPDOWN
193-
? string[]
194-
: TFilters[Name] extends FilterType.RANGE
195-
? [number, number]
196-
: never
218+
value: FilterValue<TFilters[Name]>
197219
): Promise<void> {
198220
const type = this.filters[name]
199221
const filter = this.getFilter()
@@ -243,6 +265,17 @@ export class Filter<TFilters extends Record<string, FilterType>> {
243265
await expect(activeFiltersCount).toHaveText(count.toString())
244266
}
245267

268+
public async addFilters(filters: FilterValues<TFilters>): Promise<void> {
269+
for (const name in filters) {
270+
const value = filters[name];
271+
272+
await this.addFilter(
273+
name as keyof TFilters & string,
274+
value as FilterValue<TFilters[keyof TFilters]>
275+
);
276+
}
277+
}
278+
246279
/**
247280
* Asserts that the current number of active filters equals the expected count.
248281
*

front/e2e/src/helper/grid.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Locator, Page, expect} from '@playwright/test'
22
import {Pagination} from './pagination'
3-
import {generateTestId, TestId} from "./testIds";
3+
import {generateTestId, TestId} from "../utils/testIds";
4+
import {Filter, FilterType, FilterValues} from "./filter";
45

56
interface GridCondition {
67
columnName: string
@@ -55,6 +56,82 @@ export class Grid {
5556
})
5657
}
5758

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 expectRowsToBe<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 filters have been applied.
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+
58135
/**
59136
* Returns the total number of rows in the grid via the pagination component.
60137
*
@@ -136,6 +213,14 @@ export class Grid {
136213
await expect(grid).toBeVisible()
137214
}
138215

216+
public static getCommonGridTestIds(
217+
resourceName: string,
218+
) {
219+
return {
220+
createButton: generateTestId(TestId.GRID_CREATE_BUTTON, resourceName)
221+
} as const
222+
}
223+
139224
/**
140225
* Lazily retrieves the grid Locator.
141226
*

front/e2e/src/helper/pagination.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Locator, Page, expect } from '@playwright/test'
2-
import {generateTestId, TestId} from "./testIds";
2+
import {generateTestId, TestId} from "../utils/testIds";
33

44
/**
55
* Represents a pagination component on a web page.

front/e2e/src/helper/switch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Locator, Page, expect } from '@playwright/test'
2-
import {generateTestId, TestId} from "./testIds";
2+
import {generateTestId, TestId} from "../utils/testIds";
33

44
/**
55
* Represents a toggle switch component on a web page.

front/e2e/src/helper/tabs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expect, Locator, Page} from "@playwright/test";
2-
import {generateTestId, TestId} from "./testIds";
2+
import {generateTestId, TestId} from "../utils/testIds";
33

44
/**
55
* Utility class to interact with a Tabs component in Playwright tests.

front/e2e/src/tests/globals/appBar.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {test, expect} from '@playwright/test'
2-
import {login} from '../../helper/auth'
3-
import {generateTestId, TestId} from "../../helper/testIds";
2+
import {login} from '../../utils/auth'
3+
import {generateTestId, TestId} from "../../utils/testIds";
44

55
const testIds = {
66
appBar: generateTestId(TestId.APP_BAR),

front/e2e/src/tests/globals/sideBar.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {expect, test} from '@playwright/test'
2-
import {login} from '../../helper/auth'
3-
import {generateTestId, TestId} from "../../helper/testIds";
2+
import {login} from '../../utils/auth'
3+
import {generateTestId, TestId} from "../../utils/testIds";
44

55
const testIds = {
66
sideBar: generateTestId(TestId.SIDE_BAR),

front/e2e/src/tests/pages/admin/analyze/results.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {login} from '../../../../helper/auth'
1+
import {login} from '../../../../utils/auth'
22
import {Dropdown} from '../../../../helper/dropdown'
33
import {Grid} from '../../../../helper/grid'
4-
import {navigateTo} from '../../../../helper/menu'
4+
import {navigateTo} from '../../../../utils/menu'
55
import {expect, test} from '@playwright/test'
6-
import {generateTestId, TestId} from "../../../../helper/testIds";
6+
import {generateTestId, TestId} from "../../../../utils/testIds";
77

88
const testIds = {
99
explainButton: generateTestId(TestId.BUTTON, 'explain'),

0 commit comments

Comments
 (0)