Skip to content

Commit 12d934a

Browse files
schettinoKent C. Dodds
authored and
Kent C. Dodds
committed
fix: move testHook to its own pacakge (#307)
This removes testHook in favor of http://npm.im/react-hook-testing-library Closes #302 BREAKING CHANGE: move testHook to another library. Use http://npm.im/react-hook-testing-library instead
1 parent 9128410 commit 12d934a

File tree

5 files changed

+8
-243
lines changed

5 files changed

+8
-243
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ practices.</p>
5555
- [Example](#example)
5656
- [Installation](#installation)
5757
- [Examples](#examples)
58+
- [Hooks](#hooks)
5859
- [Other Solutions](#other-solutions)
5960
- [Guiding Principles](#guiding-principles)
6061
- [Contributors](#contributors)
@@ -162,12 +163,15 @@ Some included are:
162163
- [`react-redux`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-redux.js)
163164
- [`react-router`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-router.js)
164165
- [`react-context`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-context.js)
165-
- [`react-hooks`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-hooks.js) -
166-
Use react-testing-library to test a custom React Hook.
167166

168167
You can also find react-testing-library examples at
169168
[react-testing-examples.com](https://react-testing-examples.com/jest-rtl/).
170169

170+
## Hooks
171+
172+
If you are interested in testing a custom hook, check out
173+
[react-hooks-testing-library][react-hooks-testing-library]
174+
171175
## Other Solutions
172176

173177
In preparing this project,
@@ -296,5 +300,6 @@ Links:
296300
[good-first-issue]: https://github.com/kentcdodds/react-testing-library/issues?utf8=✓&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A"good+first+issue"+
297301
[reactiflux]: https://www.reactiflux.com/
298302
[stackoverflow]: https://stackoverflow.com/questions/tagged/react-testing-library
303+
[react-hooks-testing-library]: https://github.com/mpeyper/react-hooks-testing-library
299304

300305
<!-- prettier-ignore-end -->

examples/__tests__/react-hooks.js

-119
This file was deleted.

src/__tests__/test-hook.js

-77
This file was deleted.

src/index.js

+1-29
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ function render(
3131
// they're passing us a custom container or not.
3232
mountedContainers.add(container)
3333

34-
3534
const wrapUiIfNeeded = innerElement =>
3635
WrapperComponent
3736
? React.createElement(WrapperComponent, null, innerElement)
@@ -72,33 +71,6 @@ function render(
7271
}
7372
}
7473

75-
function TestHook({callback, children}) {
76-
children(callback())
77-
return null
78-
}
79-
80-
function testHook(callback, options = {}) {
81-
const result = {
82-
current: null,
83-
}
84-
const toRender = () => (
85-
<TestHook callback={callback}>
86-
{res => {
87-
result.current = res
88-
}}
89-
</TestHook>
90-
)
91-
92-
const {unmount, rerender: rerenderComponent} = render(toRender(), options)
93-
return {
94-
result,
95-
unmount,
96-
rerender: () => {
97-
rerenderComponent(toRender(), options)
98-
},
99-
}
100-
}
101-
10274
function cleanup() {
10375
mountedContainers.forEach(cleanupAtContainer)
10476
}
@@ -159,6 +131,6 @@ fireEvent.select = (node, init) => {
159131

160132
// just re-export everything from dom-testing-library
161133
export * from 'dom-testing-library'
162-
export {render, testHook, cleanup, fireEvent, act}
134+
export {render, cleanup, fireEvent, act}
163135

164136
/* eslint func-name-matching:0 */

typings/index.d.ts

-16
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ export type RenderResult<Q extends Queries = typeof queries> = {
1919
asFragment: () => DocumentFragment
2020
} & {[P in keyof Q]: BoundFunction<Q[P]>}
2121

22-
export type HookResult<TResult> = {
23-
result: React.MutableRefObject<TResult>
24-
rerender: () => void
25-
unmount: () => boolean
26-
}
27-
2822
export interface RenderOptions<Q extends Queries = typeof queries> {
2923
container?: HTMLElement
3024
baseElement?: HTMLElement
@@ -33,8 +27,6 @@ export interface RenderOptions<Q extends Queries = typeof queries> {
3327
wrapper?: React.ComponentType
3428
}
3529

36-
export type HookOptions = RenderOptions
37-
3830
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
3931

4032
/**
@@ -49,14 +41,6 @@ export function render<Q extends Queries>(
4941
options: RenderOptions<Q>,
5042
): RenderResult<Q>
5143

52-
/**
53-
* Renders a test component that calls back to the test.
54-
*/
55-
export function testHook<T>(
56-
callback: () => T,
57-
options?: Partial<HookOptions>,
58-
): HookResult<T>
59-
6044
/**
6145
* Unmounts React trees that were mounted with render.
6246
*/

0 commit comments

Comments
 (0)