1
1
import './typedefs'
2
2
import * as path from 'path'
3
3
import { readFileSync } from 'fs'
4
- import { ElementHandle , EvaluateFn } from 'puppeteer'
4
+ import { ElementHandle , EvaluateFn , Page } from 'puppeteer'
5
+ import { ITestUtils } from './typedefs'
5
6
6
7
const domLibraryAsString = readFileSync (
7
8
path . join ( __dirname , '../dom-testing-library.js' ) ,
8
9
'utf8' ,
9
10
) . replace ( / p r o c e s s .e n v / g, '{}' )
10
11
12
+ function mapArgument ( argument : any ) : any {
13
+ return typeof argument === 'object' && argument . regex ? new RegExp ( argument . regex ) : argument
14
+ }
15
+
11
16
const mockFnToExecuteInPage = `
12
17
function evaluateInPage(container, fnName, ...args) {
13
18
${ domLibraryAsString }
14
19
15
- const mappedArgs = args.map(item => item.regex ? new RegExp(item.regex) : item )
20
+ const mappedArgs = args.map(${ mapArgument . toString ( ) } )
16
21
return __dom_testing_library__[fnName](container, ...mappedArgs)
17
22
}
18
23
`
19
24
20
25
type DOMReturnType = ElementHandle | ElementHandle [ ] | null
21
26
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 ,
25
32
) : ( ...args : any [ ] ) => Promise < DOMReturnType > {
26
33
return async function ( ...args : any [ ] ) : Promise < DOMReturnType > {
27
34
if ( fnName . includes ( 'All' ) ) throw new Error ( '*All methods not yet supported' )
28
35
29
36
// @ts -ignore
30
- const containerHandle : ElementHandle = context ? context ( ) : this
37
+ const containerHandle : ElementHandle = contextFn ? contextFn ( ... args ) : this
31
38
// @ts -ignore
32
39
const evaluateFn : EvaluateFn = { toString : ( ) => mockFnToExecuteInPage }
33
40
@@ -42,36 +49,42 @@ export function createDelegateFor(
42
49
}
43
50
}
44
51
45
- export async function getTestingUtilsForDocument ( ) : Promise < ElementHandle > {
52
+ export async function getTestingUtilsForDocument ( context ?: Page ) : Promise < ElementHandle > {
46
53
// @ts -ignore
47
- const page : Page = this
54
+ const page : Page = context || this
48
55
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
50
59
}
51
60
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 )
77
86
}
87
+
88
+ // @ts -ignore
89
+ export const utils : ITestUtils = { }
90
+ extendObjectWithTestingUtils ( utils , el => el )
0 commit comments