File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 1
1
const replacer = ( _ : string , value : unknown ) => {
2
2
if ( value instanceof RegExp ) return `__REGEXP ${ value . toString ( ) } `
3
+ if ( typeof value === 'function' ) return `__FUNCTION ${ value . toString ( ) } `
3
4
4
5
return value
5
6
}
@@ -11,6 +12,11 @@ const reviver = (_: string, value: string) => {
11
12
return new RegExp ( match ! [ 1 ] , match ! [ 2 ] || '' )
12
13
}
13
14
15
+ if ( value . toString ( ) . includes ( '__FUNCTION ' ) ) {
16
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
17
+ return new Function ( `return (${ value . split ( '__FUNCTION ' ) [ 1 ] } ).apply(this, arguments)` )
18
+ }
19
+
14
20
return value
15
21
}
16
22
Original file line number Diff line number Diff line change @@ -40,6 +40,28 @@ test.describe('lib/fixture.ts (locators)', () => {
40
40
expect ( await locator . textContent ( ) ) . toEqual ( 'Hello h1' )
41
41
} )
42
42
43
+ test ( 'supports function style `TextMatch`' , async ( { screen} ) => {
44
+ const locator = screen . getByText (
45
+ // eslint-disable-next-line prefer-arrow-callback, func-names
46
+ function ( content , element ) {
47
+ return content . startsWith ( 'Hello' ) && element ?. tagName . toLowerCase ( ) === 'h3'
48
+ } ,
49
+ )
50
+
51
+ expect ( locator ) . toBeTruthy ( )
52
+ expect ( await locator . textContent ( ) ) . toEqual ( 'Hello h3' )
53
+ } )
54
+
55
+ test ( 'supports arrow function style `TextMatch`' , async ( { screen} ) => {
56
+ const locator = screen . getByText (
57
+ ( content , element ) =>
58
+ content . startsWith ( 'Hello' ) && element ?. tagName . toLowerCase ( ) === 'h3' ,
59
+ )
60
+
61
+ expect ( locator ) . toBeTruthy ( )
62
+ expect ( await locator . textContent ( ) ) . toEqual ( 'Hello h3' )
63
+ } )
64
+
43
65
test ( 'should handle the get* methods' , async ( { queries : { getByTestId} } ) => {
44
66
const locator = getByTestId ( 'testid-text-input' )
45
67
You can’t perform that action at this time.
0 commit comments