Skip to content

Commit 0f70a8f

Browse files
committed
feat: export individual methods
1 parent 6ebba92 commit 0f70a8f

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

lib/index.ts

+47-34
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
import './typedefs'
22
import * as path from 'path'
33
import {readFileSync} from 'fs'
4-
import {ElementHandle, EvaluateFn} from 'puppeteer'
4+
import {ElementHandle, EvaluateFn, Page} from 'puppeteer'
5+
import {ITestUtils} from './typedefs'
56

67
const domLibraryAsString = readFileSync(
78
path.join(__dirname, '../dom-testing-library.js'),
89
'utf8',
910
).replace(/process.env/g, '{}')
1011

12+
function mapArgument(argument: any): any {
13+
return typeof argument === 'object' && argument.regex ? new RegExp(argument.regex) : argument
14+
}
15+
1116
const mockFnToExecuteInPage = `
1217
function 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

2025
type 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)

lib/typedefs.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
import {ElementHandle} from 'puppeteer'
12
import {MatcherOptions, Matcher, SelectorMatcherOptions} from 'dom-testing-library/typings/'
23

4+
type Element = ElementHandle
5+
6+
export interface ITestUtils {
7+
queryByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
8+
queryAllByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
9+
getByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
10+
getAllByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
11+
queryByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element | null>
12+
queryAllByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
13+
getByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element>
14+
getAllByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
15+
queryByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element | null>
16+
queryAllByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
17+
getByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element>
18+
getAllByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
19+
queryByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
20+
queryAllByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
21+
getByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
22+
getAllByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
23+
queryByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
24+
queryAllByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
25+
getByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
26+
getAllByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
27+
queryByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
28+
queryAllByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
29+
getByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
30+
getAllByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
31+
}
32+
333
declare module 'puppeteer' {
434
interface Page {
535
getTestingUtilsForDocument(): ElementHandle

0 commit comments

Comments
 (0)