Skip to content

Commit c6373c6

Browse files
committed
fix: getQueriesForElement always binds
1 parent 51e7ea6 commit c6373c6

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

lib/extend.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Matcher, MatcherOptions, SelectorMatcherOptions} from 'dom-testing-library/typings' // tslint:disable-line no-submodule-imports
22
import {getDocument, getQueriesForElement} from '.'
3+
import {ElementHandle} from '../node_modules/@types/puppeteer'
34

45
// tslint:disable-next-line
56
let Page, ElementHandle
@@ -9,7 +10,13 @@ try {
910
ElementHandle = require('puppeteer/lib/ElementHandle.js') // tslint:disable-line
1011

1112
Page.prototype.getDocument = getDocument
12-
getQueriesForElement(ElementHandle.prototype)
13+
getQueriesForElement(ElementHandle.prototype, function(this: ElementHandle): ElementHandle {
14+
return this
15+
})
16+
17+
ElementHandle.prototype.getQueriesForElement = function(this: ElementHandle): ElementHandle {
18+
return getQueriesForElement(this)
19+
}
1320
} catch (err) {
1421
// tslint:disable-next-line
1522
console.error('Could not augment puppeteer functions, do you have a conflicting version?')

lib/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ export function wait(
118118

119119
export function getQueriesForElement<T>(object: T, contextFn?: ContextFn): T & IQueryUtils {
120120
const o = object as any
121-
o.getQueriesForElement = function() {
122-
const realContextFn = contextFn || ((): ElementHandle => this)
123-
return getQueriesForElement(this, realContextFn)
124-
}
125-
121+
if (!contextFn) contextFn = () => o
122+
o.getQueriesForElement = () => getQueriesForElement(o, () => o)
126123
o.queryByPlaceholderText = createDelegateFor('queryByPlaceholderText', contextFn)
127124
o.queryAllByPlaceholderText = createDelegateFor('queryAllByPlaceholderText', contextFn)
128125
o.getByPlaceholderText = createDelegateFor('getByPlaceholderText', contextFn)

test/index.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path'
22
import * as puppeteer from 'puppeteer'
3-
import {getDocument, queries} from '../lib'
3+
import {getDocument, queries, getQueriesForElement} from '../lib'
44

55
describe('lib/index.ts', () => {
66
let browser: puppeteer.Browser
@@ -15,7 +15,13 @@ describe('lib/index.ts', () => {
1515
it('should export the utilities', async () => {
1616
const document = await getDocument(page)
1717
const element = await queries.getByText(document, 'Hello h1')
18-
expect(await page.evaluate(el => el.textContent, element)).toEqual('Hello h1')
18+
expect(await queries.getNodeText(element)).toEqual('Hello h1')
19+
})
20+
21+
it('should bind getQueriesForElement', async () => {
22+
const {getByText} = getQueriesForElement(await getDocument(page))
23+
const element = await getByText('Hello h1')
24+
expect(await queries.getNodeText(element)).toEqual('Hello h1')
1925
})
2026

2127
afterAll(async () => {

0 commit comments

Comments
 (0)