@@ -2,7 +2,7 @@ import {readFileSync} from 'fs'
2
2
import * as path from 'path'
3
3
import { ElementHandle , EvaluateFn , JSHandle , Page } from 'puppeteer'
4
4
import waitForExpect from 'wait-for-expect'
5
- import { ITestUtils } from './typedefs'
5
+ import { IQueryUtils } from './typedefs'
6
6
7
7
const domLibraryAsString = readFileSync (
8
8
path . join ( __dirname , '../dom-testing-library.js' ) ,
@@ -53,33 +53,53 @@ async function covertToElementHandle(handle: JSHandle, asArray: boolean): Promis
53
53
return asArray ? createElementHandleArray ( handle ) : createElementHandle ( handle )
54
54
}
55
55
56
- function createDelegateFor (
57
- fnName : keyof ITestUtils ,
56
+ function processNodeText ( handles : IHandleSet ) : Promise < string > {
57
+ return handles . containerHandle
58
+ . executionContext ( )
59
+ . evaluate ( handles . evaluateFn , handles . containerHandle , 'getNodeText' )
60
+ }
61
+
62
+ async function processQuery ( handles : IHandleSet ) : Promise < DOMReturnType > {
63
+ const { containerHandle, evaluateFn, fnName, argsToForward} = handles
64
+
65
+ const handle = await containerHandle
66
+ . executionContext ( )
67
+ . evaluateHandle ( evaluateFn , containerHandle , fnName , ...argsToForward )
68
+ return covertToElementHandle ( handle , fnName . includes ( 'All' ) )
69
+ }
70
+
71
+ interface IHandleSet {
72
+ containerHandle : ElementHandle
73
+ evaluateFn : EvaluateFn
74
+ fnName : string
75
+ argsToForward : any [ ]
76
+ }
77
+
78
+ function createDelegateFor < T = DOMReturnType > (
79
+ fnName : keyof IQueryUtils ,
58
80
contextFn ?: ContextFn ,
59
- ) : ( ...args : any [ ] ) => Promise < DOMReturnType > {
60
- return async function ( ...args : any [ ] ) : Promise < DOMReturnType > {
81
+ processHandleFn ?: ( handles : IHandleSet ) => Promise < T > ,
82
+ ) : ( ...args : any [ ] ) => Promise < T > {
83
+ // @ts -ignore
84
+ processHandleFn = processHandleFn || processQuery
85
+
86
+ return async function ( ...args : any [ ] ) : Promise < T > {
61
87
// @ts -ignore
62
88
const containerHandle : ElementHandle = contextFn ? contextFn . apply ( this , args ) : this
63
89
// @ts -ignore
64
90
const evaluateFn : EvaluateFn = { toString : ( ) => delegateFnToExecuteInPage }
65
91
66
92
// Convert RegExp to a special format since they don't serialize well
67
93
let argsToForward = args . map ( arg => ( arg instanceof RegExp ? { regex : arg . source } : arg ) )
94
+ // Remove the container from the argsToForward since it's always the first argument
68
95
if ( containerHandle === args [ 0 ] ) {
69
96
argsToForward = args . slice ( 1 )
70
97
}
71
98
72
- const handle = await containerHandle
73
- . executionContext ( )
74
- . evaluateHandle ( evaluateFn , containerHandle , fnName , ...argsToForward )
75
- return covertToElementHandle ( handle , fnName . includes ( 'All' ) )
99
+ return processHandleFn ! ( { fnName, containerHandle, evaluateFn, argsToForward} )
76
100
}
77
101
}
78
102
79
- export function wait ( callback = ( ) => { } , { timeout = 4500 , interval = 50 } = { } ) : Promise < { } > {
80
- return waitForExpect ( callback , timeout , interval )
81
- }
82
-
83
103
export async function getDocument ( _page ?: Page ) : Promise < ElementHandle > {
84
104
// @ts -ignore
85
105
const page : Page = _page || this
@@ -89,7 +109,11 @@ export async function getDocument(_page?: Page): Promise<ElementHandle> {
89
109
return document
90
110
}
91
111
92
- export function getQueriesForElement < T > ( object : T , contextFn ?: ContextFn ) : T & ITestUtils {
112
+ export function wait ( callback = ( ) => { } , { timeout = 4500 , interval = 50 } = { } ) : Promise < { } > {
113
+ return waitForExpect ( callback , timeout , interval )
114
+ }
115
+
116
+ export function getQueriesForElement < T > ( object : T , contextFn ?: ContextFn ) : T & IQueryUtils {
93
117
const o = object as any
94
118
o . queryByPlaceholderText = createDelegateFor ( 'queryByPlaceholderText' , contextFn )
95
119
o . queryAllByPlaceholderText = createDelegateFor ( 'queryAllByPlaceholderText' , contextFn )
@@ -115,11 +139,12 @@ export function getQueriesForElement<T>(object: T, contextFn?: ContextFn): T & I
115
139
o . queryAllByTitle = createDelegateFor ( 'queryAllByTitle' , contextFn )
116
140
o . getByTitle = createDelegateFor ( 'getByTitle' , contextFn )
117
141
o . getAllByTitle = createDelegateFor ( 'getAllByTitle' , contextFn )
142
+ o . getNodeText = createDelegateFor < string > ( 'getNodeText' , contextFn , processNodeText )
118
143
return o
119
144
}
120
145
121
146
export const within = getQueriesForElement
122
147
123
148
// @ts -ignore
124
- export const queries : ITestUtils = { }
149
+ export const queries : IQueryUtils = { }
125
150
getQueriesForElement ( queries , el => el )
0 commit comments