Skip to content

BREAKING CHANGE: Move testHook to its own pacakge #307

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

Merged
merged 5 commits into from
Feb 24, 2019
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ practices.</p>
- [Example](#example)
- [Installation](#installation)
- [Examples](#examples)
- [Hooks](#hooks)
- [Other Solutions](#other-solutions)
- [Guiding Principles](#guiding-principles)
- [Contributors](#contributors)
Expand Down Expand Up @@ -162,12 +163,15 @@ Some included are:
- [`react-redux`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-redux.js)
- [`react-router`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-router.js)
- [`react-context`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-context.js)
- [`react-hooks`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-hooks.js) -
Use react-testing-library to test a custom React Hook.

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

## Hooks

If you are interested in testing a custom hook, check out
[react-hooks-testing-library][react-hooks-testing-library]

## Other Solutions

In preparing this project,
Expand Down Expand Up @@ -296,5 +300,6 @@ Links:
[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"+
[reactiflux]: https://www.reactiflux.com/
[stackoverflow]: https://stackoverflow.com/questions/tagged/react-testing-library
[react-hooks-testing-library]: https://github.com/mpeyper/react-hooks-testing-library

<!-- prettier-ignore-end -->
119 changes: 0 additions & 119 deletions examples/__tests__/react-hooks.js

This file was deleted.

77 changes: 0 additions & 77 deletions src/__tests__/test-hook.js

This file was deleted.

30 changes: 1 addition & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function render(
// they're passing us a custom container or not.
mountedContainers.add(container)


const wrapUiIfNeeded = innerElement =>
WrapperComponent
? React.createElement(WrapperComponent, null, innerElement)
Expand Down Expand Up @@ -72,33 +71,6 @@ function render(
}
}

function TestHook({callback, children}) {
children(callback())
return null
}

function testHook(callback, options = {}) {
const result = {
current: null,
}
const toRender = () => (
<TestHook callback={callback}>
{res => {
result.current = res
}}
</TestHook>
)

const {unmount, rerender: rerenderComponent} = render(toRender(), options)
return {
result,
unmount,
rerender: () => {
rerenderComponent(toRender(), options)
},
}
}

function cleanup() {
mountedContainers.forEach(cleanupAtContainer)
}
Expand Down Expand Up @@ -159,6 +131,6 @@ fireEvent.select = (node, init) => {

// just re-export everything from dom-testing-library
export * from 'dom-testing-library'
export {render, testHook, cleanup, fireEvent, act}
export {render, cleanup, fireEvent, act}

/* eslint func-name-matching:0 */
16 changes: 0 additions & 16 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ export type RenderResult<Q extends Queries = typeof queries> = {
asFragment: () => DocumentFragment
} & {[P in keyof Q]: BoundFunction<Q[P]>}

export type HookResult<TResult> = {
result: React.MutableRefObject<TResult>
rerender: () => void
unmount: () => boolean
}

export interface RenderOptions<Q extends Queries = typeof queries> {
container?: HTMLElement
baseElement?: HTMLElement
Expand All @@ -33,8 +27,6 @@ export interface RenderOptions<Q extends Queries = typeof queries> {
wrapper?: React.ComponentType
}

export type HookOptions = RenderOptions

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

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

/**
* Renders a test component that calls back to the test.
*/
export function testHook<T>(
callback: () => T,
options?: Partial<HookOptions>,
): HookResult<T>

/**
* Unmounts React trees that were mounted with render.
*/
Expand Down