Skip to content

Commit 730ee2a

Browse files
chochihimKent C. Dodds
authored and
Kent C. Dodds
committed
fix(TS): add queries typings (#259)
<!-- Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: Add typings for queries in RenderOptions <!-- Why are these changes necessary? --> **Why**: It's missing. Also in a project I need to add some custom queries. <!-- How were these changes implemented? --> **How**: <!-- Have you done all of these things? --> **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> - [ ] Documentation - [ ] Tests - [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> - [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments --> The implementation is trickier than I thought. Any comment is appreciated. Also `Query` and `Queries` should probably be declared in `dom-testing-library`. I can create another PR once the implementation of this PR is accepted.
1 parent 23a2007 commit 730ee2a

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

typings/index.d.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
import {getQueriesForElement} from 'dom-testing-library'
1+
import {queries, BoundFunction} from 'dom-testing-library'
22

33
export * from 'dom-testing-library'
44

5-
type GetsAndQueries = ReturnType<typeof getQueriesForElement>
5+
interface Query extends Function {
6+
(container: HTMLElement, ...args): HTMLElement[] | HTMLElement | null
7+
}
8+
9+
interface Queries {
10+
[T: string]: Query
11+
}
612

7-
export interface RenderResult extends GetsAndQueries {
13+
export type RenderResult<Q extends Queries = typeof queries> = {
814
container: HTMLElement
915
baseElement: HTMLElement
1016
debug: (baseElement?: HTMLElement | DocumentFragment) => void
1117
rerender: (ui: React.ReactElement<any>) => void
1218
unmount: () => boolean
1319
asFragment: () => DocumentFragment
14-
}
20+
} & {[P in keyof Q]: BoundFunction<Q[P]>}
1521

16-
export interface RenderOptions {
22+
export interface RenderOptions<Q extends Queries = typeof queries> {
1723
container?: HTMLElement
1824
baseElement?: HTMLElement
1925
hydrate?: boolean
26+
queries?: Q
2027
}
2128

29+
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
30+
2231
/**
2332
* Render into a container which is appended to document.body. It should be used with cleanup.
2433
*/
2534
export function render(
2635
ui: React.ReactElement<any>,
27-
options?: RenderOptions,
36+
options?: Omit<RenderOptions, 'queries'>,
2837
): RenderResult
38+
export function render<Q extends Queries>(
39+
ui: React.ReactElement<any>,
40+
options: RenderOptions<Q>,
41+
): RenderResult<Q>
2942

3043
/**
3144
* Unmounts React trees that were mounted with render.

0 commit comments

Comments
 (0)