11import './typedefs'
22import * as path from 'path'
33import { readFileSync } from 'fs'
4- import { ElementHandle , EvaluateFn } from 'puppeteer'
4+ import { ElementHandle , EvaluateFn , Page } from 'puppeteer'
5+ import { ITestUtils } from './typedefs'
56
67const domLibraryAsString = readFileSync (
78 path . join ( __dirname , '../dom-testing-library.js' ) ,
89 'utf8' ,
910) . replace ( / p r o c e s s .e n v / g, '{}' )
1011
12+ function mapArgument ( argument : any ) : any {
13+ return typeof argument === 'object' && argument . regex ? new RegExp ( argument . regex ) : argument
14+ }
15+
1116const mockFnToExecuteInPage = `
1217function evaluateInPage(container, fnName, ...args) {
1318 ${ domLibraryAsString }
1419
15- const mappedArgs = args.map(item => item.regex ? new RegExp(item.regex) : item )
20+ const mappedArgs = args.map(${ mapArgument . toString ( ) } )
1621 return __dom_testing_library__[fnName](container, ...mappedArgs)
1722}
1823`
1924
2025type DOMReturnType = ElementHandle | ElementHandle [ ] | null
2126
22- export function createDelegateFor (
23- fnName : string ,
24- context ?: ( ) => ElementHandle ,
27+ type ContextFn = ( ...args : any [ ] ) => ElementHandle
28+
29+ function createDelegateFor (
30+ fnName : keyof ITestUtils ,
31+ contextFn ?: ContextFn ,
2532) : ( ...args : any [ ] ) => Promise < DOMReturnType > {
2633 return async function ( ...args : any [ ] ) : Promise < DOMReturnType > {
2734 if ( fnName . includes ( 'All' ) ) throw new Error ( '*All methods not yet supported' )
2835
2936 // @ts -ignore
30- const containerHandle : ElementHandle = context ? context ( ) : this
37+ const containerHandle : ElementHandle = contextFn ? contextFn ( ... args ) : this
3138 // @ts -ignore
3239 const evaluateFn : EvaluateFn = { toString : ( ) => mockFnToExecuteInPage }
3340
@@ -42,36 +49,42 @@ export function createDelegateFor(
4249 }
4350}
4451
45- export async function getTestingUtilsForDocument ( ) : Promise < ElementHandle > {
52+ export async function getTestingUtilsForDocument ( context ?: Page ) : Promise < ElementHandle > {
4653 // @ts -ignore
47- const page : Page = this
54+ const page : Page = context || this
4855 const documentHandle = await page . mainFrame ( ) . evaluateHandle ( 'document' )
49- return await documentHandle . asElement ( )
56+ const document = await documentHandle . asElement ( )
57+ if ( ! document ) throw new Error ( 'Could not find document' )
58+ return document
5059}
5160
52- export function extendObjectWithTestingUtils ( object : any ) : void {
53- object . queryByPlaceholderText = createDelegateFor ( 'queryByPlaceholderText' )
54- object . queryAllByPlaceholderText = createDelegateFor ( 'queryAllByPlaceholderText' )
55- object . getByPlaceholderText = createDelegateFor ( 'getByPlaceholderText' )
56- object . getAllByPlaceholderText = createDelegateFor ( 'getAllByPlaceholderText' )
57- object . queryByText = createDelegateFor ( 'queryByText' )
58- object . queryAllByText = createDelegateFor ( 'queryAllByText' )
59- object . getByText = createDelegateFor ( 'getByText' )
60- object . getAllByText = createDelegateFor ( 'getAllByText' )
61- object . queryByLabelText = createDelegateFor ( 'queryByLabelText' )
62- object . queryAllByLabelText = createDelegateFor ( 'queryAllByLabelText' )
63- object . getByLabelText = createDelegateFor ( 'getByLabelText' )
64- object . getAllByLabelText = createDelegateFor ( 'getAllByLabelText' )
65- object . queryByAltText = createDelegateFor ( 'queryByAltText' )
66- object . queryAllByAltText = createDelegateFor ( 'queryAllByAltText' )
67- object . getByAltText = createDelegateFor ( 'getByAltText' )
68- object . getAllByAltText = createDelegateFor ( 'getAllByAltText' )
69- object . queryByTestId = createDelegateFor ( 'queryByTestId' )
70- object . queryAllByTestId = createDelegateFor ( 'queryAllByTestId' )
71- object . getByTestId = createDelegateFor ( 'getByTestId' )
72- object . getAllByTestId = createDelegateFor ( 'getAllByTestId' )
73- object . queryByTitle = createDelegateFor ( 'queryByTitle' )
74- object . queryAllByTitle = createDelegateFor ( 'queryAllByTitle' )
75- object . getByTitle = createDelegateFor ( 'getByTitle' )
76- object . getAllByTitle = createDelegateFor ( 'getAllByTitle' )
61+ export function extendObjectWithTestingUtils ( object : any , contextFn ?: ContextFn ) : void {
62+ object . queryByPlaceholderText = createDelegateFor ( 'queryByPlaceholderText' , contextFn )
63+ object . queryAllByPlaceholderText = createDelegateFor ( 'queryAllByPlaceholderText' , contextFn )
64+ object . getByPlaceholderText = createDelegateFor ( 'getByPlaceholderText' , contextFn )
65+ object . getAllByPlaceholderText = createDelegateFor ( 'getAllByPlaceholderText' , contextFn )
66+ object . queryByText = createDelegateFor ( 'queryByText' , contextFn )
67+ object . queryAllByText = createDelegateFor ( 'queryAllByText' , contextFn )
68+ object . getByText = createDelegateFor ( 'getByText' , contextFn )
69+ object . getAllByText = createDelegateFor ( 'getAllByText' , contextFn )
70+ object . queryByLabelText = createDelegateFor ( 'queryByLabelText' , contextFn )
71+ object . queryAllByLabelText = createDelegateFor ( 'queryAllByLabelText' , contextFn )
72+ object . getByLabelText = createDelegateFor ( 'getByLabelText' , contextFn )
73+ object . getAllByLabelText = createDelegateFor ( 'getAllByLabelText' , contextFn )
74+ object . queryByAltText = createDelegateFor ( 'queryByAltText' , contextFn )
75+ object . queryAllByAltText = createDelegateFor ( 'queryAllByAltText' , contextFn )
76+ object . getByAltText = createDelegateFor ( 'getByAltText' , contextFn )
77+ object . getAllByAltText = createDelegateFor ( 'getAllByAltText' , contextFn )
78+ object . queryByTestId = createDelegateFor ( 'queryByTestId' , contextFn )
79+ object . queryAllByTestId = createDelegateFor ( 'queryAllByTestId' , contextFn )
80+ object . getByTestId = createDelegateFor ( 'getByTestId' , contextFn )
81+ object . getAllByTestId = createDelegateFor ( 'getAllByTestId' , contextFn )
82+ object . queryByTitle = createDelegateFor ( 'queryByTitle' , contextFn )
83+ object . queryAllByTitle = createDelegateFor ( 'queryAllByTitle' , contextFn )
84+ object . getByTitle = createDelegateFor ( 'getByTitle' , contextFn )
85+ object . getAllByTitle = createDelegateFor ( 'getAllByTitle' , contextFn )
7786}
87+
88+ // @ts -ignore
89+ export const utils : ITestUtils = { }
90+ extendObjectWithTestingUtils ( utils , el => el )
0 commit comments