1
1
import { ClientFunction , Selector } from "testcafe" ;
2
2
import { queries } from "@testing-library/dom" ;
3
- import type {
4
- Options ,
5
- TestcafeBoundFunction ,
6
- TestcafeBoundFunctions ,
7
- } from "./types" ;
3
+ import type { Options , QueryName , WithinSelectors } from "./types" ;
8
4
9
5
declare global {
10
6
interface Window {
@@ -14,7 +10,8 @@ declare global {
14
10
15
11
const SELECTOR_TYPE = Selector ( "" ) ( ) . constructor . name ;
16
12
17
- const withinSelectors = Object . keys ( queries ) . reduce ( ( acc , withinQueryName ) => {
13
+ const queryNames = Object . keys ( queries ) as QueryName [ ] ;
14
+ const withinSelectors = queryNames . reduce ( ( acc , withinQueryName ) => {
18
15
return {
19
16
...acc ,
20
17
[ withinQueryName ] : new Function ( `
@@ -27,12 +24,11 @@ const withinSelectors = Object.keys(queries).reduce((acc, withinQueryName) => {
27
24
return window.TestingLibraryDom.within(el).${ withinQueryName } .apply(null, args);
28
25
` ) ,
29
26
} ;
30
- } , { } as Record < keyof typeof queries , ( node : Element , ...methodParams : any [ ] ) => any > ) ;
27
+ } , { } as Record < QueryName , ( node : Element , ...methodParams : any [ ] ) => any > ) ;
31
28
32
29
export async function configureOnce ( options : Partial < Options > ) {
33
30
const { content } = configure ( options ) ;
34
- // @ts -ignore
35
- await ClientFunction ( new Function ( content ) ) ( ) ;
31
+ await ClientFunction ( new Function ( content ) as ( ) => Function ) ( ) ;
36
32
}
37
33
38
34
export function configure ( options : Partial < Options > ) {
@@ -42,17 +38,23 @@ export function configure(options: Partial<Options>) {
42
38
return { content : configFunction } ;
43
39
}
44
40
45
- export function within < T > (
46
- sel : string | Selector | SelectorPromise | TestcafeBoundFunction < T >
47
- ) : TestcafeBoundFunctions < typeof queries > {
48
- if ( sel instanceof Function ) {
49
- return within ( sel ( ) ) ;
41
+ const withWithinMethods = ( selector : Selector ) => {
42
+ return ( selector . addCustomMethods ( withinSelectors , {
43
+ returnDOMNodes : true ,
44
+ } ) as unknown ) as WithinSelectors ;
45
+ } ;
46
+
47
+ export function within (
48
+ selector : string | Selector | SelectorPromise | ( ( ) => SelectorPromise )
49
+ ) : WithinSelectors {
50
+ if ( selector instanceof Function ) {
51
+ return within ( selector ( ) ) ;
50
52
}
51
- if ( isSelector ( sel ) ) {
52
- // @ts -ignore
53
- return sel . addCustomMethods ( withinSelectors , { returnDOMNodes : true } ) ;
54
- } else if ( typeof sel === "string" ) {
55
- return within ( Selector ( sel ) ) ;
53
+
54
+ if ( isSelector ( selector ) ) {
55
+ return withWithinMethods ( selector ) ;
56
+ } else if ( typeof selector === "string" ) {
57
+ return within ( Selector ( selector ) ) ;
56
58
} else {
57
59
throw new Error (
58
60
`"within" only accepts a query (getBy, queryBy, etc), string or testcafe Selector`
@@ -64,7 +66,7 @@ function isSelector(sel: any): sel is Selector {
64
66
return sel . constructor . name === SELECTOR_TYPE ;
65
67
}
66
68
67
- const bindFunction = < T extends keyof typeof queries > ( queryName : T ) => {
69
+ const bindFunction = < T extends QueryName > ( queryName : T ) => {
68
70
const query = queryName . replace ( "find" , "query" ) as T ;
69
71
return Selector (
70
72
( matcher , ...options ) => {
0 commit comments