Skip to content

Commit

Permalink
feat: add generics to screen queries (#1034)
Browse files Browse the repository at this point in the history
Closes #1033
  • Loading branch information
timdeschryver committed Sep 29, 2021
1 parent 669602c commit 99bc2c0
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 15 deletions.
19 changes: 19 additions & 0 deletions types/__tests__/type-tests.ts
Expand Up @@ -9,6 +9,7 @@ import {
waitFor,
waitForElementToBeRemoved,
MatcherOptions,
BoundFunctions,
} from '@testing-library/dom'

const {
Expand Down Expand Up @@ -38,12 +39,14 @@ export async function testQueries() {

// screen queries
screen.getByText('foo')
screen.getByText<HTMLDivElement>('foo')
screen.queryByText('foo')
await screen.findByText('foo')
await screen.findByText('foo', undefined, {timeout: 10})
screen.debug(screen.getAllByText('bar'))
screen.queryAllByText('bar')
await screen.findAllByText('bar')
await screen.findAllByRole<HTMLButtonElement>('button', {name: 'submit'})
await screen.findAllByText('bar', undefined, {timeout: 10})
}

Expand Down Expand Up @@ -88,6 +91,22 @@ export async function testQueryHelpers() {
await findByAutomationId(element, 'id')
}

export function testBoundFunctions() {
const boundfunctions = {} as BoundFunctions<{
customQueryOne: (container: HTMLElement, text: string) => HTMLElement
customQueryTwo: (
container: HTMLElement,
text: string,
text2: string,
) => HTMLElement
customQueryThree: (container: HTMLElement, number: number) => HTMLElement
}>

boundfunctions.customQueryOne('one')
boundfunctions.customQueryTwo('one', 'two')
boundfunctions.customQueryThree(3)
}

export async function testByRole() {
const element = document.createElement('button')
element.setAttribute('aria-hidden', 'true')
Expand Down
169 changes: 154 additions & 15 deletions types/get-queries-for-element.d.ts
@@ -1,23 +1,162 @@
import * as queries from './queries'

export type BoundFunction<T> = T extends (
attribute: string,
element: HTMLElement,
text: infer P,
options: infer Q,
container: HTMLElement,
...args: infer P
) => 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
? (...args: P) => R
: never
export type BoundFunctions<T> = {[P in keyof T]: BoundFunction<T[P]>}

export type BoundFunctions<Q> = Q extends typeof queries
? {
getByLabelText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByText<T>>>
): ReturnType<queries.GetByText<T>>
getAllByLabelText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByText<T>>>
): ReturnType<queries.AllByText<T>>
queryByLabelText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByText<T>>>
): ReturnType<queries.QueryByText<T>>
queryAllByLabelText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByText<T>>>
): ReturnType<queries.AllByText<T>>
findByLabelText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByText<T>>>
): ReturnType<queries.FindByText<T>>
findAllByLabelText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByText<T>>>
): ReturnType<queries.FindAllByText<T>>
getByPlaceholderText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
): ReturnType<queries.GetByBoundAttribute<T>>
getAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
queryByPlaceholderText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
): ReturnType<queries.QueryByBoundAttribute<T>>
queryAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
findByPlaceholderText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
): ReturnType<queries.FindByBoundAttribute<T>>
findAllByPlaceholderText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
): ReturnType<queries.FindAllByBoundAttribute<T>>
getByText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByText<T>>>
): ReturnType<queries.GetByText<T>>
getAllByText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByText<T>>>
): ReturnType<queries.AllByText<T>>
queryByText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByText<T>>>
): ReturnType<queries.QueryByText<T>>
queryAllByText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByText<T>>>
): ReturnType<queries.AllByText<T>>
findByText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByText<T>>>
): ReturnType<queries.FindByText<T>>
findAllByText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByText<T>>>
): ReturnType<queries.FindAllByText<T>>
getByAltText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
): ReturnType<queries.GetByBoundAttribute<T>>
getAllByAltText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
queryByAltText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
): ReturnType<queries.QueryByBoundAttribute<T>>
queryAllByAltText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
findByAltText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
): ReturnType<queries.FindByBoundAttribute<T>>
findAllByAltText<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
): ReturnType<queries.FindAllByBoundAttribute<T>>
getByTitle<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
): ReturnType<queries.GetByBoundAttribute<T>>
getAllByTitle<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
queryByTitle<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
): ReturnType<queries.QueryByBoundAttribute<T>>
queryAllByTitle<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
findByTitle<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
): ReturnType<queries.FindByBoundAttribute<T>>
findAllByTitle<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
): ReturnType<queries.FindAllByBoundAttribute<T>>
getByDisplayValue<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
): ReturnType<queries.GetByBoundAttribute<T>>
getAllByDisplayValue<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
queryByDisplayValue<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
): ReturnType<queries.QueryByBoundAttribute<T>>
queryAllByDisplayValue<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
findByDisplayValue<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
): ReturnType<queries.FindByBoundAttribute<T>>
findAllByDisplayValue<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
): ReturnType<queries.FindAllByBoundAttribute<T>>
getByRole<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByRole<T>>>
): ReturnType<queries.GetByRole<T>>
getAllByRole<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByRole<T>>>
): ReturnType<queries.AllByRole<T>>
queryByRole<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByRole<T>>>
): ReturnType<queries.QueryByRole<T>>
queryAllByRole<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByRole<T>>>
): ReturnType<queries.AllByRole<T>>
findByRole<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByRole<T>>>
): ReturnType<queries.FindByRole<T>>
findAllByRole<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByRole<T>>>
): ReturnType<queries.FindAllByRole<T>>
getByTestId<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.GetByBoundAttribute<T>>>
): ReturnType<queries.GetByBoundAttribute<T>>
getAllByTestId<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
queryByTestId<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.QueryByBoundAttribute<T>>>
): ReturnType<queries.QueryByBoundAttribute<T>>
queryAllByTestId<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.AllByBoundAttribute<T>>>
): ReturnType<queries.AllByBoundAttribute<T>>
findByTestId<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindByBoundAttribute<T>>>
): ReturnType<queries.FindByBoundAttribute<T>>
findAllByTestId<T extends HTMLElement = HTMLElement>(
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
): ReturnType<queries.FindAllByBoundAttribute<T>>
}
: {
[P in keyof Q]: BoundFunction<Q[P]>
}

export type Query = (
container: HTMLElement,
Expand Down

0 comments on commit 99bc2c0

Please sign in to comment.