Skip to content

Commit d9c14ca

Browse files
committed
draft
1 parent 9e3a8cd commit d9c14ca

File tree

2 files changed

+105
-9
lines changed

2 files changed

+105
-9
lines changed

front/e2e/src/helper/filter.ts

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@ 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+
// value: TFilters[Name] extends FilterType.BOOLEAN
27+
// ? boolean
28+
// : TFilters[Name] extends FilterType.TEXT
29+
// ? string
30+
// : TFilters[Name] extends FilterType.DROPDOWN
31+
// ? string
32+
// : TFilters[Name] extends FilterType.RANGE
33+
// ? [number, number]
34+
// : never
35+
36+
37+
export type FilterValues<TFilters extends Record<string, FilterType>> = {
38+
[K in keyof TFilters]?: FilterValue<TFilters[K]>;
39+
};
40+
1541
/**
1642
* Generic class to manage and assert filters in a UI, supporting
1743
* various filter types like dropdowns, booleans, text inputs, and ranges.
@@ -177,6 +203,21 @@ export class Filter<TFilters extends Record<string, FilterType>> {
177203
}
178204
}
179205

206+
public async removeFilters(filters: Partial<{ [K in keyof TFilters]: any }>): Promise<void> {
207+
for (const name in filters) {
208+
const value = filters[name];
209+
210+
// If it's an array (only for DROPDOWN), iterate
211+
if (Array.isArray(value)) {
212+
for (const item of value) {
213+
await this.removeFilter(name as keyof TFilters & string, item);
214+
}
215+
} else {
216+
await this.removeFilter(name as keyof TFilters & string, value);
217+
}
218+
}
219+
}
220+
180221
/**
181222
* Adds a new filter based on its type and value.
182223
*
@@ -185,15 +226,7 @@ export class Filter<TFilters extends Record<string, FilterType>> {
185226
*/
186227
public async addFilter<Name extends keyof TFilters & string>(
187228
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
229+
value: FilterValue<TFilters[Name]>
197230
): Promise<void> {
198231
const type = this.filters[name]
199232
const filter = this.getFilter()
@@ -243,6 +276,17 @@ export class Filter<TFilters extends Record<string, FilterType>> {
243276
await expect(activeFiltersCount).toHaveText(count.toString())
244277
}
245278

279+
public async addFilters(filters: FilterValues<TFilters>): Promise<void> {
280+
for (const name in filters) {
281+
const value = filters[name];
282+
283+
await this.addFilter(
284+
name as keyof TFilters & string,
285+
value as FilterValue<TFilters[keyof TFilters]>
286+
);
287+
}
288+
}
289+
246290
/**
247291
* Asserts that the current number of active filters equals the expected count.
248292
*

front/e2e/src/helper/grid.ts

Lines changed: 52 additions & 0 deletions
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'
33
import {generateTestId, TestId} from "../utils/testIds";
4+
import {Filter, FilterType, FilterValues} from "./filter";
45

56
interface GridCondition {
67
columnName: string
@@ -55,6 +56,57 @@ 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 expectColumnsToBe<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+
await this.expectToFindLineWhere(expectedRow)
94+
}
95+
96+
/**
97+
* Assert that All filters have been removed.
98+
*
99+
* @param filter - Grid's filter
100+
* @param defaultRowCount - Row count without filters
101+
*/
102+
public async expectAllFiltersRemoved<TFilters extends Record<string, FilterType>>(
103+
filter: Filter<TFilters>,
104+
defaultRowCount: number,
105+
) {
106+
await filter.clearFilters()
107+
await this.pagination.expectToHaveCount(defaultRowCount)
108+
}
109+
58110
/**
59111
* Returns the total number of rows in the grid via the pagination component.
60112
*

0 commit comments

Comments
 (0)