diff --git a/types/__tests__/type-tests.ts b/types/__tests__/type-tests.ts index 9b5c81e8..f3b69105 100644 --- a/types/__tests__/type-tests.ts +++ b/types/__tests__/type-tests.ts @@ -9,6 +9,7 @@ import { waitFor, waitForElementToBeRemoved, MatcherOptions, + BoundFunctions, } from '@testing-library/dom' const { @@ -38,12 +39,14 @@ export async function testQueries() { // screen queries screen.getByText('foo') + screen.getByText('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('button', {name: 'submit'}) await screen.findAllByText('bar', undefined, {timeout: 10}) } @@ -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') diff --git a/types/get-queries-for-element.d.ts b/types/get-queries-for-element.d.ts index 04a48539..5b5903dc 100644 --- a/types/get-queries-for-element.d.ts +++ b/types/get-queries-for-element.d.ts @@ -1,23 +1,162 @@ import * as queries from './queries' export type BoundFunction = 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 = {[P in keyof T]: BoundFunction} + +export type BoundFunctions = Q extends typeof queries + ? { + getByLabelText( + ...args: Parameters>> + ): ReturnType> + getAllByLabelText( + ...args: Parameters>> + ): ReturnType> + queryByLabelText( + ...args: Parameters>> + ): ReturnType> + queryAllByLabelText( + ...args: Parameters>> + ): ReturnType> + findByLabelText( + ...args: Parameters>> + ): ReturnType> + findAllByLabelText( + ...args: Parameters>> + ): ReturnType> + getByPlaceholderText( + ...args: Parameters>> + ): ReturnType> + getAllByPlaceholderText( + ...args: Parameters>> + ): ReturnType> + queryByPlaceholderText( + ...args: Parameters>> + ): ReturnType> + queryAllByPlaceholderText( + ...args: Parameters>> + ): ReturnType> + findByPlaceholderText( + ...args: Parameters>> + ): ReturnType> + findAllByPlaceholderText( + ...args: Parameters>> + ): ReturnType> + getByText( + ...args: Parameters>> + ): ReturnType> + getAllByText( + ...args: Parameters>> + ): ReturnType> + queryByText( + ...args: Parameters>> + ): ReturnType> + queryAllByText( + ...args: Parameters>> + ): ReturnType> + findByText( + ...args: Parameters>> + ): ReturnType> + findAllByText( + ...args: Parameters>> + ): ReturnType> + getByAltText( + ...args: Parameters>> + ): ReturnType> + getAllByAltText( + ...args: Parameters>> + ): ReturnType> + queryByAltText( + ...args: Parameters>> + ): ReturnType> + queryAllByAltText( + ...args: Parameters>> + ): ReturnType> + findByAltText( + ...args: Parameters>> + ): ReturnType> + findAllByAltText( + ...args: Parameters>> + ): ReturnType> + getByTitle( + ...args: Parameters>> + ): ReturnType> + getAllByTitle( + ...args: Parameters>> + ): ReturnType> + queryByTitle( + ...args: Parameters>> + ): ReturnType> + queryAllByTitle( + ...args: Parameters>> + ): ReturnType> + findByTitle( + ...args: Parameters>> + ): ReturnType> + findAllByTitle( + ...args: Parameters>> + ): ReturnType> + getByDisplayValue( + ...args: Parameters>> + ): ReturnType> + getAllByDisplayValue( + ...args: Parameters>> + ): ReturnType> + queryByDisplayValue( + ...args: Parameters>> + ): ReturnType> + queryAllByDisplayValue( + ...args: Parameters>> + ): ReturnType> + findByDisplayValue( + ...args: Parameters>> + ): ReturnType> + findAllByDisplayValue( + ...args: Parameters>> + ): ReturnType> + getByRole( + ...args: Parameters>> + ): ReturnType> + getAllByRole( + ...args: Parameters>> + ): ReturnType> + queryByRole( + ...args: Parameters>> + ): ReturnType> + queryAllByRole( + ...args: Parameters>> + ): ReturnType> + findByRole( + ...args: Parameters>> + ): ReturnType> + findAllByRole( + ...args: Parameters>> + ): ReturnType> + getByTestId( + ...args: Parameters>> + ): ReturnType> + getAllByTestId( + ...args: Parameters>> + ): ReturnType> + queryByTestId( + ...args: Parameters>> + ): ReturnType> + queryAllByTestId( + ...args: Parameters>> + ): ReturnType> + findByTestId( + ...args: Parameters>> + ): ReturnType> + findAllByTestId( + ...args: Parameters>> + ): ReturnType> + } + : { + [P in keyof Q]: BoundFunction + } export type Query = ( container: HTMLElement,