Skip to content

Commit 51e7ea6

Browse files
committed
fix: support desctructing for /extend context
fixes #4
1 parent f8813d2 commit 51e7ea6

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const page = await browser.newPage()
4949
const $document = await page.getDocument()
5050
// query methods are added directly to prototype of ElementHandle
5151
const $form = await $document.getByTestId('my-form')
52+
// destructing works if you explicitly call getQueriesForElement
53+
const {getByText} = $form.getQueriesForElement()
5254
// ...
5355
```
5456

@@ -95,7 +97,6 @@ Unique methods, not part of `dom-testing-library`
9597

9698
- `waitForElement` method is not exposed. Puppeteer has its own set of wait utilities that somewhat conflict with the style used in `dom-testing-library`. See [#3](https://github.com/patrickhulce/pptr-testing-library/issues/3).
9799
- `fireEvent` method is not exposed, use puppeteer's built-ins instead.
98-
- Query methods rely on the context and don't support destructuring. See [#4](https://github.com/patrickhulce/pptr-testing-library/issues/4).
99100
- `expect` assertion extensions are not available.
100101

101102
## Special Thanks

lib/extend.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ declare module 'puppeteer' {
2323
}
2424

2525
interface ElementHandle {
26+
getQueriesForElement(): ElementHandle
2627
getNodeText(): Promise<string>
2728
queryByPlaceholderText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle | null>
2829
queryAllByPlaceholderText(m: Matcher, opts?: MatcherOptions): Promise<ElementHandle[]>

lib/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ 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+
121126
o.queryByPlaceholderText = createDelegateFor('queryByPlaceholderText', contextFn)
122127
o.queryAllByPlaceholderText = createDelegateFor('queryAllByPlaceholderText', contextFn)
123128
o.getByPlaceholderText = createDelegateFor('getByPlaceholderText', contextFn)

lib/typedefs.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {ElementHandle} from 'puppeteer'
44
type Element = ElementHandle
55

66
export interface IQueryUtils {
7+
getQueriesForElement(): IQueryUtils
78
getNodeText(el: Element): Promise<string>
89
queryByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
910
queryAllByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>

test/extend.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ describe('lib/extend.ts', () => {
7676
expect(await $h3.getNodeText()).toEqual('Hello h3')
7777
})
7878

79+
it('should work with destructuring', async () => {
80+
const {queryByText} = (await document.$('#scoped')).getQueriesForElement()
81+
expect(await queryByText('Hello h1')).toBeFalsy()
82+
expect(await queryByText('Hello h3')).toBeTruthy()
83+
})
84+
7985
afterAll(async () => {
8086
await browser.close()
8187
})

0 commit comments

Comments
 (0)