|
| 1 | +import * as path from 'path' |
| 2 | +import {readFileSync} from 'fs' |
| 3 | +import {MatcherOptions, Matcher, SelectorMatcherOptions} from 'dom-testing-library/typings/' |
| 4 | +import {ElementHandle, EvaluateFn} from 'puppeteer' |
| 5 | + |
| 6 | +const Page = require('puppeteer/lib/Page.js') |
| 7 | +const ElementHandle = require('puppeteer/lib/ElementHandle.js') |
| 8 | + |
| 9 | +const domLibraryAsString = readFileSync( |
| 10 | + path.join(__dirname, '../dom-testing-library.js'), |
| 11 | + 'utf8', |
| 12 | +).replace(/process.env/g, '{}') |
| 13 | + |
| 14 | +const mockFnToExecuteInPage = ` |
| 15 | +function evaluateInPage(container, fnName, ...args) { |
| 16 | + ${domLibraryAsString} |
| 17 | +
|
| 18 | + const mappedArgs = args.map(item => item.regex ? new RegExp(item.regex) : item) |
| 19 | + return __dom_testing_library__[fnName](container, ...mappedArgs) |
| 20 | +} |
| 21 | +` |
| 22 | + |
| 23 | +type DOMReturnType = ElementHandle | ElementHandle[] | null |
| 24 | + |
| 25 | +function createDelegateFor(fnName: string): (...args: any[]) => Promise<DOMReturnType> { |
| 26 | + return async function(...args: any[]): Promise<DOMReturnType> { |
| 27 | + if (fnName.includes('All')) throw new Error('*All methods not yet supported') |
| 28 | + |
| 29 | + // @ts-ignore |
| 30 | + const containerHandle: ElementHandle = this |
| 31 | + // @ts-ignore |
| 32 | + const evaluateFn: EvaluateFn = {toString: () => mockFnToExecuteInPage} |
| 33 | + |
| 34 | + const mappedArgs = args.map(arg => (arg instanceof RegExp ? {regex: arg.source} : arg)) |
| 35 | + const handle = await containerHandle |
| 36 | + .executionContext() |
| 37 | + .evaluateHandle(evaluateFn, containerHandle, fnName, ...mappedArgs) |
| 38 | + const element = handle.asElement() |
| 39 | + if (element) return element |
| 40 | + await handle.dispose() |
| 41 | + return null |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +Page.prototype.getTestingUtilsForDocument = async function(): Promise<ElementHandle> { |
| 46 | + const documentHandle = await this.mainFrame().evaluateHandle('document') |
| 47 | + return await documentHandle.asElement() |
| 48 | +} |
| 49 | + |
| 50 | +ElementHandle.prototype.queryByPlaceholderText = createDelegateFor('queryByPlaceholderText') |
| 51 | +ElementHandle.prototype.queryAllByPlaceholderText = createDelegateFor('queryAllByPlaceholderText') |
| 52 | +ElementHandle.prototype.getByPlaceholderText = createDelegateFor('getByPlaceholderText') |
| 53 | +ElementHandle.prototype.getAllByPlaceholderText = createDelegateFor('getAllByPlaceholderText') |
| 54 | +ElementHandle.prototype.queryByText = createDelegateFor('queryByText') |
| 55 | +ElementHandle.prototype.queryAllByText = createDelegateFor('queryAllByText') |
| 56 | +ElementHandle.prototype.getByText = createDelegateFor('getByText') |
| 57 | +ElementHandle.prototype.getAllByText = createDelegateFor('getAllByText') |
| 58 | +ElementHandle.prototype.queryByLabelText = createDelegateFor('queryByLabelText') |
| 59 | +ElementHandle.prototype.queryAllByLabelText = createDelegateFor('queryAllByLabelText') |
| 60 | +ElementHandle.prototype.getByLabelText = createDelegateFor('getByLabelText') |
| 61 | +ElementHandle.prototype.getAllByLabelText = createDelegateFor('getAllByLabelText') |
| 62 | +ElementHandle.prototype.queryByAltText = createDelegateFor('queryByAltText') |
| 63 | +ElementHandle.prototype.queryAllByAltText = createDelegateFor('queryAllByAltText') |
| 64 | +ElementHandle.prototype.getByAltText = createDelegateFor('getByAltText') |
| 65 | +ElementHandle.prototype.getAllByAltText = createDelegateFor('getAllByAltText') |
| 66 | +ElementHandle.prototype.queryByTestId = createDelegateFor('queryByTestId') |
| 67 | +ElementHandle.prototype.queryAllByTestId = createDelegateFor('queryAllByTestId') |
| 68 | +ElementHandle.prototype.getByTestId = createDelegateFor('getByTestId') |
| 69 | +ElementHandle.prototype.getAllByTestId = createDelegateFor('getAllByTestId') |
| 70 | +ElementHandle.prototype.queryByTitle = createDelegateFor('queryByTitle') |
| 71 | +ElementHandle.prototype.queryAllByTitle = createDelegateFor('queryAllByTitle') |
| 72 | +ElementHandle.prototype.getByTitle = createDelegateFor('getByTitle') |
| 73 | +ElementHandle.prototype.getAllByTitle = createDelegateFor('getAllByTitle') |
| 74 | + |
| 75 | +declare module 'puppeteer' { |
| 76 | + interface Page { |
| 77 | + getTestingUtilsForDocument(): ElementHandle |
| 78 | + } |
| 79 | + |
| 80 | + interface ElementHandle { |
| 81 | + queryByPlaceholderText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle | null> |
| 82 | + queryAllByPlaceholderText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 83 | + getByPlaceholderText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle> |
| 84 | + getAllByPlaceholderText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 85 | + queryByText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle | null> |
| 86 | + queryAllByText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle[]> |
| 87 | + getByText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle> |
| 88 | + getAllByText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle[]> |
| 89 | + queryByLabelText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle | null> |
| 90 | + queryAllByLabelText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle[]> |
| 91 | + getByLabelText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle> |
| 92 | + getAllByLabelText(m: Matcher, opts?: SelectorMatcherOptions): Promise<ElementHandle[]> |
| 93 | + queryByAltText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle | null> |
| 94 | + queryAllByAltText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 95 | + getByAltText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle> |
| 96 | + getAllByAltText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 97 | + queryByTestId(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle | null> |
| 98 | + queryAllByTestId(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 99 | + getByTestId(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle> |
| 100 | + getAllByTestId(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 101 | + queryByTitle(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle | null> |
| 102 | + queryAllByTitle(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 103 | + getByTitle(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle> |
| 104 | + getAllByTitle(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]> |
| 105 | + } |
| 106 | +} |
0 commit comments