Skip to content

Commit 9bfb092

Browse files
committed
fix: better document API
1 parent 0f70a8f commit 9bfb092

File tree

7 files changed

+52
-17
lines changed

7 files changed

+52
-17
lines changed

lib/extend.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {getTestingUtilsForDocument, extendObjectWithTestingUtils} from '.'
1+
import {extendObjectWithTestingUtils, getDocument} from '.'
22

3-
const Page = require('puppeteer/lib/Page.js')
4-
const ElementHandle = require('puppeteer/lib/ElementHandle.js')
3+
const Page = require('puppeteer/lib/Page.js') // tslint:disable-line
4+
const ElementHandle = require('puppeteer/lib/ElementHandle.js') // tslint:disable-line
55

6-
Page.prototype.getTestingUtilsForDocument = getTestingUtilsForDocument
6+
Page.prototype.document = getDocument
77

88
extendObjectWithTestingUtils(ElementHandle.prototype)

lib/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import './typedefs'
2-
import * as path from 'path'
31
import {readFileSync} from 'fs'
2+
import * as path from 'path'
43
import {ElementHandle, EvaluateFn, Page} from 'puppeteer'
54
import {ITestUtils} from './typedefs'
65

@@ -38,22 +37,26 @@ function createDelegateFor(
3837
// @ts-ignore
3938
const evaluateFn: EvaluateFn = {toString: () => mockFnToExecuteInPage}
4039

41-
const mappedArgs = args.map(arg => (arg instanceof RegExp ? {regex: arg.source} : arg))
40+
let argsToForward = args.map(arg => (arg instanceof RegExp ? {regex: arg.source} : arg))
41+
if (containerHandle === args[0]) {
42+
argsToForward = args.slice(1)
43+
}
44+
4245
const handle = await containerHandle
4346
.executionContext()
44-
.evaluateHandle(evaluateFn, containerHandle, fnName, ...mappedArgs)
47+
.evaluateHandle(evaluateFn, containerHandle, fnName, ...argsToForward)
4548
const element = handle.asElement()
4649
if (element) return element
4750
await handle.dispose()
48-
return null
51+
return null // tslint:disable-line
4952
}
5053
}
5154

52-
export async function getTestingUtilsForDocument(context?: Page): Promise<ElementHandle> {
55+
export async function getDocument(context?: Page): Promise<ElementHandle> {
5356
// @ts-ignore
5457
const page: Page = context || this
5558
const documentHandle = await page.mainFrame().evaluateHandle('document')
56-
const document = await documentHandle.asElement()
59+
const document = documentHandle.asElement()
5760
if (!document) throw new Error('Could not find document')
5861
return document
5962
}

lib/typedefs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import {Matcher, MatcherOptions, SelectorMatcherOptions} from 'dom-testing-library/typings' // tslint:disable-line no-submodule-imports
12
import {ElementHandle} from 'puppeteer'
2-
import {MatcherOptions, Matcher, SelectorMatcherOptions} from 'dom-testing-library/typings/'
33

44
type Element = ElementHandle
55

@@ -30,9 +30,10 @@ export interface ITestUtils {
3030
getAllByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
3131
}
3232

33+
/* tslint:disable */
3334
declare module 'puppeteer' {
3435
interface Page {
35-
getTestingUtilsForDocument(): ElementHandle
36+
document(): Promise<ElementHandle>
3637
}
3738

3839
interface ElementHandle {

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"test": "npm run test:lint && npm run test:unit",
88
"test:unit": "jest",
9-
"test:lint": "lint",
9+
"test:lint": "lint -t typescript './lib/**/*.ts'",
1010
"semantic-release": "semantic-release",
1111
"clean": "rm -fR dist/",
1212
"rebuild": "npm run clean && npm run build",
@@ -32,15 +32,22 @@
3232
"utility"
3333
],
3434
"config": {
35+
"tslint": {
36+
"rules": {
37+
"no-unsafe-any": false
38+
}
39+
},
3540
"exportAliases": {
3641
"extend": "./dist/extend"
3742
}
3843
},
44+
"dependencies": {
45+
"dom-testing-library": "^2.6.1"
46+
},
3947
"devDependencies": {
4048
"@patrickhulce/lint": "^2.1.3",
4149
"@types/jest": "^23.1.1",
4250
"@types/puppeteer": "^1.3.4",
43-
"dom-testing-library": "2.6.1",
4451
"generate-export-aliases": "^1.1.0",
4552
"jest": "^23.1.0",
4653
"puppeteer": "^1.5.0",

test/extend.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('lib/extend.ts', () => {
1414
})
1515

1616
it('should extend puppeteer ElementHandle', async () => {
17-
document = await page.getTestingUtilsForDocument()
17+
document = await page.document()
1818
expect(typeof document.queryAllByAltText).toBe('function')
1919
})
2020

test/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as path from 'path'
2+
import * as puppeteer from 'puppeteer'
3+
import {getDocument, utils} from '../lib'
4+
5+
describe('lib/index.ts', () => {
6+
let browser: puppeteer.Browser
7+
let page: puppeteer.Page
8+
9+
it('should launch puppeteer', async () => {
10+
browser = await puppeteer.launch()
11+
page = await browser.newPage()
12+
await page.goto(`file://${path.join(__dirname, 'fixtures/page.html')}`)
13+
})
14+
15+
it('should export the utilities', async () => {
16+
const document = await getDocument(page)
17+
const element = await utils.getByText(document, 'Hello h1')
18+
expect(await page.evaluate(el => el.textContent, element)).toEqual('Hello h1')
19+
})
20+
21+
afterAll(async () => {
22+
await browser.close()
23+
})
24+
})

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ doctrine@^2.0.0:
18401840
dependencies:
18411841
esutils "^2.0.2"
18421842

1843-
1843+
dom-testing-library@^2.6.1:
18441844
version "2.6.1"
18451845
resolved "https://registry.yarnpkg.com/dom-testing-library/-/dom-testing-library-2.6.1.tgz#ec97192d3ddbc026809d1377eafc22eabdc372d3"
18461846
dependencies:

0 commit comments

Comments
 (0)