@@ -21,102 +21,44 @@ export function configure(options) {
21
21
` ;
22
22
return { content : configFunction } ;
23
23
}
24
+ const withinSelectors = { } ;
25
+ Object . keys ( queries ) . forEach ( withinQueryName => {
26
+
27
+ withinSelectors [ withinQueryName ] =
28
+ new Function ( `
29
+ const els = arguments[0];
30
+ if(els.length > 1) {
31
+ throw new Error("within() only works with a single element, found " + els.length);
32
+ }
33
+ const el = els[0];
34
+ const args = Array.from(arguments).slice(1);
35
+ return window.TestingLibraryDom.within(el).${ withinQueryName } .apply(null, args);
36
+ ` )
24
37
38
+ } ) ;
25
39
26
40
Object . keys ( queries ) . forEach ( queryName => {
41
+
27
42
module . exports [ queryName ] = Selector (
28
- new Function (
29
- `
30
- if(!window.tctlReplacer) {
31
- window.tctlReplacer = function tctlReplacer(key, value) {
32
- if (value instanceof RegExp)
33
- return ("__REGEXP " + value.toString());
34
- else if (typeof value === 'function')
35
- return ("__FUNCTION " + value.toString());
36
- else
37
- return value;
38
- }
39
- }
40
- const els = TestingLibraryDom.${ queryName } (document.body, ...arguments);
41
- if(!Array.isArray(els)) {
42
- els.setAttribute('data-tctl-args', JSON.stringify(Array.from(arguments), window.tctlReplacer, 0));
43
- els.setAttribute('data-tctl-queryname', '${ queryName } ');
44
- } else {
45
- els.forEach((el,i) => {
46
- el.setAttribute('data-tctl-args', JSON.stringify(Array.from(arguments), window.tctlReplacer, 0));
47
- el.setAttribute('data-tctl-queryname', '${ queryName } ');
48
- el.setAttribute('data-tctl-index', i);
49
- });
50
- }
51
- return els;
52
- ` ,
53
- ) ,
54
- ) ;
43
+ ( ...args ) => window . TestingLibraryDom [ queryName ] ( document . body , ...args )
44
+ , { dependencies : { queryName } } ) ;
45
+
55
46
} )
56
- function reviver ( key , value ) {
57
- if ( value . toString ( ) . indexOf ( '__REGEXP ' ) == 0 ) {
58
- const m = value . split ( '__REGEXP ' ) [ 1 ] . match ( / \/ ( .* ) \/ ( .* ) ? / ) ;
59
- return new RegExp ( m [ 1 ] , m [ 2 ] || '' ) ;
60
- } else
61
- return value ;
62
- }
63
47
64
- export const within = async selector => {
65
- if ( selector instanceof Function ) {
66
- return within ( selector ( ) ) ;
48
+ export const within = sel => {
49
+ if ( sel instanceof Function ) {
50
+ return within ( sel ( ) ) ;
67
51
}
68
-
69
- if ( selector . constructor . name === SELECTOR_TYPE ) {
70
- const count = await selector . count ;
71
- if ( count > 1 ) {
72
- throw new Error ( `within() requires a single element, found ${ count } ` ) ;
73
- }
74
- const el = await selector ;
75
- const withinQueryName = el . getAttribute ( 'data-tctl-queryname' ) ;
76
-
77
- const withinArgs = JSON . parse ( el . getAttribute ( 'data-tctl-args' ) , reviver )
78
- . map ( arg => {
79
- if ( arg instanceof RegExp ) {
80
- return arg . toString ( ) ;
81
- } else if ( arg . toString ( ) . indexOf ( '__FUNCTION ' ) == 0 ) {
82
- return ( arg . replace ( '__FUNCTION ' , '' ) )
83
- } else {
84
- return JSON . stringify ( arg ) ;
85
- }
86
- } ) . join ( ', ' ) ;
87
-
88
- const withinIndexer = el . hasAttribute ( 'data-tctl-index' ) ? `[${ el . getAttribute ( 'data-tctl-index' ) } ]` : '' ;
89
-
90
- const withinSelectors = { } ;
91
- Object . keys ( queries ) . forEach ( queryName => {
92
- withinSelectors [ queryName ] = Selector (
93
- new Function ( `
94
-
95
- const {within, ${ withinQueryName } } = TestingLibraryDom;
96
- const el = ${ withinQueryName } (document.body, ${ withinArgs } )${ withinIndexer } ;
97
- return within(el).${ queryName } (...arguments);
98
- `
99
- ) ) ;
100
- } ) ;
101
- return withinSelectors ;
102
- } else if ( typeof ( selector ) === 'string' ) {
103
- const sanitizedSelector = selector . replace ( / " / g, "'" ) ;
104
-
105
- const withinSelectors = { } ;
106
-
107
- Object . keys ( queries ) . forEach ( queryName => {
108
- withinSelectors [ queryName ] = Selector (
109
- new Function (
110
- `
111
- const {within} = TestingLibraryDom;
112
- return within(document.querySelector("${ sanitizedSelector } ")).${ queryName } (...arguments);
113
- ` ) ,
114
- )
115
- } )
116
-
117
- return withinSelectors ;
52
+ if ( isSelector ( sel ) ) {
53
+ return ( sel ) . addCustomMethods ( withinSelectors , { returnDOMNodes : true } )
54
+ } else if ( typeof ( sel ) === 'string' ) {
55
+ return within ( Selector ( sel ) ) ;
118
56
} else {
119
- throw new Error ( `"within" only accepts a string or another testing-library query as a parameter. ${ selector } is not one of those ` )
57
+ throw new Error ( `"within" only accepts a query (getBy, queryBy, etc), string or testcafe Selector ` )
120
58
}
121
59
}
122
60
61
+ function isSelector ( sel ) {
62
+ return sel . constructor . name === SELECTOR_TYPE ;
63
+ }
64
+
0 commit comments