Skip to content

Commit 519a8c4

Browse files
authored
Merge branch 'main' into alpha
2 parents 01ec707 + 0c4aabe commit 519a8c4

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

.all-contributorsrc

+19
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,25 @@
12881288
"contributions": [
12891289
"doc"
12901290
]
1291+
},
1292+
{
1293+
"login": "akashshyamdev",
1294+
"name": "Akash Shyam",
1295+
"avatar_url": "https://avatars.githubusercontent.com/u/56759828?v=4",
1296+
"profile": "https://www.akashshyam.online/",
1297+
"contributions": [
1298+
"bug"
1299+
]
1300+
},
1301+
{
1302+
"login": "fmeum",
1303+
"name": "Fabian Meumertzheim",
1304+
"avatar_url": "https://avatars.githubusercontent.com/u/4312191?v=4",
1305+
"profile": "https://hen.ne.ke",
1306+
"contributions": [
1307+
"code",
1308+
"bug"
1309+
]
12911310
}
12921311
],
12931312
"contributorsPerLine": 7,

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ Thanks goes to these people ([emoji key][emojis]):
613613
</tr>
614614
<tr>
615615
<td align="center"><a href="https://github.com/ImADrafter"><img src="https://avatars.githubusercontent.com/u/44379989?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Marcos Gómez</b></sub></a><br /><a href="https://github.com/testing-library/react-testing-library/commits?author=ImADrafter" title="Documentation">📖</a></td>
616+
<td align="center"><a href="https://www.akashshyam.online/"><img src="https://avatars.githubusercontent.com/u/56759828?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Akash Shyam</b></sub></a><br /><a href="https://github.com/testing-library/react-testing-library/issues?q=author%3Aakashshyamdev" title="Bug reports">🐛</a></td>
617+
<td align="center"><a href="https://hen.ne.ke"><img src="https://avatars.githubusercontent.com/u/4312191?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fabian Meumertzheim</b></sub></a><br /><a href="https://github.com/testing-library/react-testing-library/commits?author=fmeum" title="Code">💻</a> <a href="https://github.com/testing-library/react-testing-library/issues?q=author%3Afmeum" title="Bug reports">🐛</a></td>
616618
</tr>
617619
</table>
618620

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
"license": "MIT",
4747
"dependencies": {
4848
"@babel/runtime": "^7.12.5",
49-
"@testing-library/dom": "^8.5.0"
49+
"@testing-library/dom": "^8.5.0",
50+
"@types/react-dom": "*"
5051
},
5152
"devDependencies": {
5253
"@testing-library/jest-dom": "^5.11.6",
53-
"@types/react-dom": "^17.0.0",
5454
"dotenv-cli": "^4.0.0",
5555
"kcd-scripts": "^11.1.0",
5656
"npm-run-all": "^4.1.5",

types/index.d.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export * from '@testing-library/dom'
1414
export type RenderResult<
1515
Q extends Queries = typeof queries,
1616
Container extends Element | DocumentFragment = HTMLElement,
17+
BaseElement extends Element | DocumentFragment = Container,
1718
> = {
1819
container: Container
19-
baseElement: Element
20+
baseElement: BaseElement
2021
debug: (
2122
baseElement?:
2223
| Element
@@ -33,6 +34,7 @@ export type RenderResult<
3334
export interface RenderOptions<
3435
Q extends Queries = typeof queries,
3536
Container extends Element | DocumentFragment = HTMLElement,
37+
BaseElement extends Element | DocumentFragment = Container,
3638
> {
3739
/**
3840
* 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,
@@ -50,7 +52,7 @@ export interface RenderOptions<
5052
*
5153
* @see https://testing-library.com/docs/react-testing-library/api/#baseelement
5254
*/
53-
baseElement?: Element
55+
baseElement?: BaseElement
5456
/**
5557
* If `hydrate` is set to `true`, then it will render with `ReactDOM.hydrate`. This may be useful if you are using server-side
5658
* rendering and use ReactDOM.hydrate to mount your components.
@@ -75,7 +77,7 @@ export interface RenderOptions<
7577
*
7678
* @see https://testing-library.com/docs/react-testing-library/api/#wrapper
7779
*/
78-
wrapper?: React.ComponentType
80+
wrapper?: React.JSXElementConstructor<{children: React.ReactElement}>
7981
}
8082

8183
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
@@ -86,10 +88,11 @@ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
8688
export function render<
8789
Q extends Queries = typeof queries,
8890
Container extends Element | DocumentFragment = HTMLElement,
91+
BaseElement extends Element | DocumentFragment = Container,
8992
>(
9093
ui: React.ReactElement,
91-
options: RenderOptions<Q, Container>,
92-
): RenderResult<Q, Container>
94+
options: RenderOptions<Q, Container, BaseElement>,
95+
): RenderResult<Q, Container, BaseElement>
9396
export function render(
9497
ui: React.ReactElement,
9598
options?: Omit<RenderOptions, 'queries'>,

types/test.tsx

+60
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,66 @@ export function testQueries() {
100100
)
101101
}
102102

103+
export function wrappedRender(
104+
ui: React.ReactElement,
105+
options?: pure.RenderOptions,
106+
) {
107+
const Wrapper = ({children}: {children: React.ReactElement}): JSX.Element => {
108+
return <div>{children}</div>
109+
}
110+
111+
return pure.render(ui, {wrapper: Wrapper, ...options})
112+
}
113+
114+
export function wrappedRenderB(
115+
ui: React.ReactElement,
116+
options?: pure.RenderOptions,
117+
) {
118+
const Wrapper: React.FunctionComponent<{children?: React.ReactNode}> = ({
119+
children,
120+
}) => {
121+
return <div>{children}</div>
122+
}
123+
124+
return pure.render(ui, {wrapper: Wrapper, ...options})
125+
}
126+
127+
export function wrappedRenderC(
128+
ui: React.ReactElement,
129+
options?: pure.RenderOptions,
130+
) {
131+
interface AppWrapperProps {
132+
userProviderProps?: {user: string}
133+
}
134+
const AppWrapperProps: React.FunctionComponent<AppWrapperProps> = ({
135+
children,
136+
userProviderProps = {user: 'TypeScript'},
137+
}) => {
138+
return <div data-testid={userProviderProps.user}>{children}</div>
139+
}
140+
141+
return pure.render(ui, {wrapper: AppWrapperProps, ...options})
142+
}
143+
144+
export function testBaseElement() {
145+
const {baseElement: baseDefaultElement} = render(<div />)
146+
expectType<HTMLElement, typeof baseDefaultElement>(baseDefaultElement)
147+
148+
const container = document.createElement('input')
149+
const {baseElement: baseElementFromContainer} = render(<div />, {container})
150+
expectType<typeof container, typeof baseElementFromContainer>(
151+
baseElementFromContainer,
152+
)
153+
154+
const baseElementOption = document.createElement('input')
155+
const {baseElement: baseElementFromOption} = render(<div />, {
156+
baseElement: baseElementOption,
157+
})
158+
expectType<typeof baseElementOption, typeof baseElementFromOption>(
159+
baseElementFromOption,
160+
)
161+
}
162+
103163
/*
104164
eslint
105165
testing-library/prefer-explicit-assert: "off",

0 commit comments

Comments
 (0)