@@ -2,7 +2,7 @@ import {readFileSync} from 'fs'
22import * as path from 'path'
33import { ElementHandle , EvaluateFn , JSHandle , Page } from 'puppeteer'
44import waitForExpect from 'wait-for-expect'
5- import { ITestUtils } from './typedefs'
5+ import { IQueryUtils } from './typedefs'
66
77const domLibraryAsString = readFileSync (
88 path . join ( __dirname , '../dom-testing-library.js' ) ,
@@ -53,33 +53,53 @@ async function covertToElementHandle(handle: JSHandle, asArray: boolean): Promis
5353 return asArray ? createElementHandleArray ( handle ) : createElementHandle ( handle )
5454}
5555
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 ,
5880 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 > {
6187 // @ts -ignore
6288 const containerHandle : ElementHandle = contextFn ? contextFn . apply ( this , args ) : this
6389 // @ts -ignore
6490 const evaluateFn : EvaluateFn = { toString : ( ) => delegateFnToExecuteInPage }
6591
6692 // Convert RegExp to a special format since they don't serialize well
6793 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
6895 if ( containerHandle === args [ 0 ] ) {
6996 argsToForward = args . slice ( 1 )
7097 }
7198
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} )
76100 }
77101}
78102
79- export function wait ( callback = ( ) => { } , { timeout = 4500 , interval = 50 } = { } ) : Promise < { } > {
80- return waitForExpect ( callback , timeout , interval )
81- }
82-
83103export async function getDocument ( _page ?: Page ) : Promise < ElementHandle > {
84104 // @ts -ignore
85105 const page : Page = _page || this
@@ -89,7 +109,11 @@ export async function getDocument(_page?: Page): Promise<ElementHandle> {
89109 return document
90110}
91111
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 {
93117 const o = object as any
94118 o . queryByPlaceholderText = createDelegateFor ( 'queryByPlaceholderText' , contextFn )
95119 o . queryAllByPlaceholderText = createDelegateFor ( 'queryAllByPlaceholderText' , contextFn )
@@ -115,11 +139,12 @@ export function getQueriesForElement<T>(object: T, contextFn?: ContextFn): T & I
115139 o . queryAllByTitle = createDelegateFor ( 'queryAllByTitle' , contextFn )
116140 o . getByTitle = createDelegateFor ( 'getByTitle' , contextFn )
117141 o . getAllByTitle = createDelegateFor ( 'getAllByTitle' , contextFn )
142+ o . getNodeText = createDelegateFor < string > ( 'getNodeText' , contextFn , processNodeText )
118143 return o
119144}
120145
121146export const within = getQueriesForElement
122147
123148// @ts -ignore
124- export const queries : ITestUtils = { }
149+ export const queries : IQueryUtils = { }
125150getQueriesForElement ( queries , el => el )
0 commit comments