Skip to content
Open
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
8 changes: 6 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as ReactDOMClient from 'react-dom/client'
import {
queries,
Queries,
BoundFunction,
BoundFunctions,
prettyFormat,
Config as ConfigDTL,
} from '@testing-library/dom'
Expand Down Expand Up @@ -44,7 +44,11 @@ export type RenderResult<
rerender: (ui: React.ReactNode) => void
unmount: () => void
asFragment: () => DocumentFragment
} & {[P in keyof Q]: BoundFunction<Q[P]>}
} & {
[P in keyof Q]: P extends keyof BoundFunctions<Q>
? BoundFunctions<Q>[P]
: never
}

/** @deprecated */
export type BaseRenderOptions<
Expand Down
38 changes: 38 additions & 0 deletions types/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,44 @@ export function testErrorHandlers() {
})
}

export async function testGenericQueryElementTypes() {
const view = pure.render(<button />)

const buttonElementGetByRole = view.getByRole<HTMLButtonElement>('button')

const buttonElementQueryByRole = view.queryByRole<HTMLButtonElement>('button')
const buttonElementFindByRole = await view.findByRole<HTMLButtonElement>(
'button',
)

expectType<HTMLButtonElement, typeof buttonElementGetByRole>(
buttonElementGetByRole,
)
expectType<HTMLButtonElement | null, typeof buttonElementQueryByRole>(
buttonElementQueryByRole,
)
expectType<HTMLButtonElement, typeof buttonElementFindByRole>(
buttonElementFindByRole,
)

const buttonElementsGetAllByRole =
view.getAllByRole<HTMLButtonElement>('button')
const buttonElementsQueryAllByText =
view.queryAllByText<HTMLButtonElement>('button')
const buttonElementsFindAllByText =
await view.findAllByText<HTMLButtonElement>('button')

expectType<HTMLButtonElement[], typeof buttonElementsGetAllByRole>(
buttonElementsGetAllByRole,
)
expectType<HTMLButtonElement[], typeof buttonElementsQueryAllByText>(
buttonElementsQueryAllByText,
)
expectType<HTMLButtonElement[], typeof buttonElementsFindAllByText>(
buttonElementsFindAllByText,
)
}

/*
eslint
testing-library/prefer-explicit-assert: "off",
Expand Down
Loading