Skip to content

Add find* queries #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,47 @@ Unique methods, not part of `@testing-library/dom`
- `queryAllByPlaceholderText`
- `getByPlaceholderText`
- `getAllByPlaceholderText`
- `findByPlaceholderText`
- `findAllByPlaceholderText`
- `queryByText`
- `queryAllByText`
- `getByText`
- `getAllByText`
- `findByText`
- `findAllByText`
- `queryByLabelText`
- `queryAllByLabelText`
- `getByLabelText`
- `getAllByLabelText`
- `findByLabelText`
- `findAllByLabelText`
- `queryByAltText`
- `queryAllByAltText`
- `getByAltText`
- `getAllByAltText`
- `findByAltText`
- `findAllByAltText`
- `queryByTestId`
- `queryAllByTestId`
- `getByTestId`
- `getAllByTestId`
- `findByTestId`
- `findAllByTestId`
- `queryByTitle`
- `queryAllByTitle`
- `getByTitle`
- `getAllByTitle`
- `findByTitle`
- `findAllByTitle`
- `queryByDisplayValue`,
- `queryAllByDisplayValue`,
- `getByDisplayValue`,
- `getAllByDisplayValue`,
- `findByDisplayValue`,
- `findAllByDisplayValue`,

## Known Limitations

- `waitForElement` method is not exposed. Puppeteer has its own set of wait utilities that somewhat conflict with the style used in `@testing-library/dom`. See [#3](https://github.com/testing-library/pptr-testing-library/issues/3).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to remove this note, but maybe convert it to cover waitForDomChange and waitForElementToBeRemoved as well and nudge to "use the find* methods instead".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check latest commit, writing documentation is hard 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use find* queries for waitForElementToBeRemoved, but I think we should be able to get that working nicely. Didn't get round to it in this PR but I'll raise an issue if anyone else wants to do it :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good thanks! :)

- Async utilities `waitForElement`, `waitForElementToBeRemoved` and `waitForDomChange` are not exposed. Consider using a `find*` query.
- `fireEvent` method is not exposed, use puppeteer's built-ins instead.
- `expect` assertion extensions are not available.

Expand Down
16 changes: 16 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,41 +161,57 @@ export function getQueriesForElement<T>(
'queryAllByPlaceholderText',
'getByPlaceholderText',
'getAllByPlaceholderText',
'findByPlaceholderText',
'findAllByPlaceholderText',

'queryByText',
'queryAllByText',
'getByText',
'getAllByText',
'findByText',
'findAllByText',

'queryByLabelText',
'queryAllByLabelText',
'getByLabelText',
'getAllByLabelText',
'findByLabelText',
'findAllByLabelText',

'queryByAltText',
'queryAllByAltText',
'getByAltText',
'getAllByAltText',
'findByAltText',
'findAllByAltText',

'queryByTestId',
'queryAllByTestId',
'getByTestId',
'getAllByTestId',
'findByTestId',
'findAllByTestId',

'queryByTitle',
'queryAllByTitle',
'getByTitle',
'getAllByTitle',
'findByTitle',
'findAllByTitle',

'queryByRole',
'queryAllByRole',
'getByRole',
'getAllByRole',
'findByRole',
'findAllByRole',

'queryByDisplayValue',
'queryAllByDisplayValue',
'getByDisplayValue',
'getAllByDisplayValue',
'findByDisplayValue',
'findAllByDisplayValue',
]
functionNames.forEach(functionName => {
o[functionName] = createDelegateFor(functionName, contextFn)
Expand Down
104 changes: 97 additions & 7 deletions lib/typedefs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Matcher, MatcherOptions, SelectorMatcherOptions} from '@testing-library/dom'
import {Matcher, MatcherOptions, SelectorMatcherOptions, WaitForElementOptions} from '@testing-library/dom'
import {ElementHandle} from 'puppeteer'

type Element = ElementHandle
Expand All @@ -8,54 +8,144 @@ interface IQueryMethods {
queryAllByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
getByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
getAllByPlaceholderText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
findByPlaceholderText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByPlaceholderText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element | null>
queryAllByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
getByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element>
getAllByText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
findByText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element | null>
queryAllByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
getByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element>
getAllByLabelText(el: Element, m: Matcher, opts?: SelectorMatcherOptions): Promise<Element[]>
findByLabelText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByLabelText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
queryAllByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
getByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
getAllByAltText(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
findByAltText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByAltText(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
queryAllByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
getByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
getAllByTestId(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
findByTestId(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByTestId(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
queryAllByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
getByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
getAllByTitle(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
findByTitle(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByTitle(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByRole(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
queryAllByRole(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
getByRole(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
getAllByRole(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
findByRole(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByRole(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>

queryByDisplayValue(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element | null>
queryAllByDisplayValue(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
getByDisplayValue(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element>
getAllByDisplayValue(el: Element, m: Matcher, opts?: MatcherOptions): Promise<Element[]>
findByDisplayValue(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element>
findAllByDisplayValue(
el: Element,
m: Matcher,
opts?: SelectorMatcherOptions,
waitForOpts?: WaitForElementOptions): Promise<Element[]>
}

type IScopedQueryMethods = {
[K in keyof IQueryMethods]: (m: Matcher, opts?: MatcherOptions) => ReturnType<IQueryMethods[K]>
}
export type BoundFunction<T> = T extends (
attribute: string,
element: Element,
text: infer P,
options: infer Q,
) => infer R
? (text: P, options?: Q) => R
: T extends (a1: any, text: infer P, options: infer Q, waitForElementOptions: infer W) => infer R
? (text: P, options?: Q, waitForElementOptions?: W) => R
: T extends (a1: any, text: infer P, options: infer Q) => infer R
? (text: P, options?: Q) => R
: never
export type BoundFunctions<T> = { [P in keyof T]: BoundFunction<T[P]> }

export interface IScopedQueryUtils extends IScopedQueryMethods {
getQueriesForElement(): IQueryUtils & IScopedQueryUtils
export interface IScopedQueryUtils extends BoundFunctions<IQueryMethods> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure BoundFunctions will work for us if it uses HTMLElement type inferences when we're using puppeteer ElementHandles

I know it's a pain, but maybe we just have to write our own type inference or just add waitForOpts?: WaitForElementOptions as optional to the signature of all the methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could duplicate BoundFunctions but with ElementHandle instead of HTMLElement? Everything else should be the same I think

getQueriesForElement(): IScopedQueryUtils
getNodeText(): Promise<string>
}

export interface IQueryUtils extends IQueryMethods {
getQueriesForElement(): IQueryUtils & IScopedQueryUtils
getQueriesForElement(): IScopedQueryUtils
getNodeText(el: Element): Promise<string>
}

Expand Down
23 changes: 23 additions & 0 deletions test/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ describe('lib/extend.ts', () => {
expect(await queryByText('Hello h3')).toBeTruthy()
})

describe('deferred page', () => {
beforeEach(async () => {
await page.goto(`file://${path.join(__dirname, 'fixtures/late-page.html')}`)
document = await page.getDocument()
})

it('should handle the findBy* methods', async () => {
expect(await document.findByText('Loaded!', {}, { timeout: 7000 })).toBeTruthy()
}, 9000)

it('should handle the findByAll* methods', async () => {
const elements = await document.findAllByText(/Hello/, {}, { timeout: 7000 })
expect(elements).toHaveLength(2)

const text = await Promise.all([
page.evaluate(el => el.textContent, elements[0]),
page.evaluate(el => el.textContent, elements[1]),
])

expect(text).toEqual(['Hello h1', 'Hello h2'])
}, 9000)
})

afterAll(async () => {
await browser.close()
})
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/late-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
const loaded = document.createElement('span')
loaded.textContent = 'Loaded!'
document.body.appendChild(loaded)

const heading1 = document.createElement('h1')
heading1.textContent = 'Hello h1'
document.body.appendChild(heading1)

const heading2 = document.createElement('h2')
heading2.textContent = 'Hello h2'
document.body.appendChild(heading2)
}, 5000)
</script>
</body>
Expand Down