@@ -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 *
0 commit comments