So i have something like this from kent c dodd's library that is super useful
import * as React from 'react'
import {render as rtlRender} from '@testing-library/react'
import {ThemeProvider} from 'components/theme'
function render(ui, {theme = 'light', ...options} = {}) {
const Wrapper = ({children}) => (
<ThemeProvider initialTheme={theme}>{children}</ThemeProvider>
)
return rtlRender(ui, {wrapper: Wrapper, ...options})
}
export * from '@testing-library/react'
// override React Testing Library's render with our own
export {render}
i'm having trouble converting it to typescript though. any thoughts on what i need to fine tune below?
import * as React from 'react'
import { ReactNode } from 'react'
import {render as rtlRender} from '@testing-library/react'
import { QueryClientProvider, QueryClient } from 'react-query'
interface WrapperProps {
children: ReactNode
}
const queryClient = new QueryClient();
function render(ui, {client = queryClient, ...options} = {}) {
const Wrapper = ({children}: WrapperProps) => (
<QueryClientProvider client={client}>
{children}
</QueryClientProvider>
)
return rtlRender(ui, {wrapper: Wrapper, ...options})
}
export * from '@testing-library/react'
// override React Testing Library's render with our own
export {render}
i'm getting the below about typing on the wrapper:
No overload matches this call.
Overload 1 of 2, '(ui: ReactElement<any, string | JSXElementConstructor<any>>, options: RenderOptions<typeof import("path/node_modules/@testing-library/dom/types/queries"), HTMLElement>): RenderResult<...>', gave the following error.
Type '({ children }: WrapperProps) => JSX.Element' is not assignable to type 'ComponentType<{}>'.
Type '({ children }: WrapperProps) => JSX.Element' is not assignable to type 'FunctionComponent<{}>'.
Types of parameters '__0' and 'props' are incompatible.
Type '{ children?: ReactNode; }' is not assignable to type 'WrapperProps'.
Property 'children' is optional in type '{ children?: ReactNode; }' but required in type 'WrapperProps'.
Overload 2 of 2, '(ui: ReactElement<any, string | JSXElementConstructor<any>>, options?: Omit<RenderOptions<typeof import("path/node_modules/@testing-library/dom/types/queries"), HTMLElement>, "queries">): RenderResult<...>', gave the following error.
Type '({ children }: WrapperProps) => JSX.Element' is not assignable to type 'ComponentType<{}>'.
Type '({ children }: WrapperProps) => JSX.Element' is not assignable to type 'FunctionComponent<{}>'.ts(2769)
index.d.ts(41, 3): The expected type comes from property 'wrapper' which is declared here on type 'RenderOptions<typeof import("path/node_modules/@testing-library/dom/types/queries"), HTMLElement>'
index.d.ts(41, 3): The expected type comes from property 'wrapper' which is declared here on type 'Omit<RenderOptions<typeof import("path/node_modules/@testing-library/dom/types/queries"), HTMLElement>, "queries">'
render
function fromWrapper
component, but that's not required at all. It was just simpler for me to understand it. – Cheat