Skip to content
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

fix: Match runtime type of baseElement in TypeScript types #1023

Merged
merged 2 commits into from
Mar 9, 2022
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
11 changes: 7 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export * from '@testing-library/dom'
export type RenderResult<
Q extends Queries = typeof queries,
Container extends Element | DocumentFragment = HTMLElement,
BaseElement extends Element | DocumentFragment = Container,
> = {
container: Container
baseElement: Element
baseElement: BaseElement
debug: (
baseElement?:
| Element
Expand All @@ -33,6 +34,7 @@ export type RenderResult<
export interface RenderOptions<
Q extends Queries = typeof queries,
Container extends Element | DocumentFragment = HTMLElement,
BaseElement extends Element | DocumentFragment = Container,
> {
/**
* By default, React Testing Library will create a div and append that div to the document.body. Your React component will be rendered in the created div. If you provide your own HTMLElement container via this option,
Expand All @@ -50,7 +52,7 @@ export interface RenderOptions<
*
* @see https://testing-library.com/docs/react-testing-library/api/#baseelement
*/
baseElement?: Element
baseElement?: BaseElement
/**
* If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
* rendering and use ReactDOM.hydrate to mount your components.
Expand Down Expand Up @@ -81,10 +83,11 @@ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
export function render<
Q extends Queries = typeof queries,
Container extends Element | DocumentFragment = HTMLElement,
BaseElement extends Element | DocumentFragment = Container,
>(
ui: React.ReactElement,
options: RenderOptions<Q, Container>,
): RenderResult<Q, Container>
options: RenderOptions<Q, Container, BaseElement>,
): RenderResult<Q, Container, BaseElement>
export function render(
ui: React.ReactElement,
options?: Omit<RenderOptions, 'queries'>,
Expand Down
19 changes: 19 additions & 0 deletions types/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ export function wrappedRenderC(
return pure.render(ui, {wrapper: AppWrapperProps, ...options})
}

export function testBaseElement() {
const {baseElement: baseDefaultElement} = render(<div />)
expectType<HTMLElement, typeof baseDefaultElement>(baseDefaultElement)

const container = document.createElement('input')
const {baseElement: baseElementFromContainer} = render(<div />, {container})
expectType<typeof container, typeof baseElementFromContainer>(
baseElementFromContainer,
)

const baseElementOption = document.createElement('input')
const {baseElement: baseElementFromOption} = render(<div />, {
baseElement: baseElementOption,
})
expectType<typeof baseElementOption, typeof baseElementFromOption>(
baseElementFromOption,
)
}

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