diff --git a/.all-contributorsrc b/.all-contributorsrc
index dea90e11..2a0be067 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -668,6 +668,18 @@
"example",
"test"
]
+ },
+ {
+ "login": "FredyC",
+ "name": "Daniel K.",
+ "avatar_url": "https://avatars0.githubusercontent.com/u/1096340?v=4",
+ "profile": "https://github.com/FredyC",
+ "contributions": [
+ "bug",
+ "code",
+ "ideas",
+ "test"
+ ]
}
]
}
diff --git a/README.md b/README.md
index 49de2ab5..7ae254f6 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,7 @@ practices.
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]
-[](#contributors)
+[](#contributors)
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Join the community on Spectrum][spectrum-badge]][spectrum]
@@ -216,8 +216,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [
dadamssg](https://github.com/dadamssg)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=dadamssg "Documentation") | [
Yazan Aabed](https://www.yaabed.com/)
[π](#blog-YazanAabeed "Blogposts") | [
Tim](https://github.com/timbonicus)
[π](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Atimbonicus "Bug reports") [π»](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Code") [π](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Documentation") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Tests") | [
Divyanshu Maithani](http://divyanshu.xyz)
[β
](#tutorial-divyanshu013 "Tutorials") [πΉ](#video-divyanshu013 "Videos") | [
Deepak Grover](https://www.linkedin.com/in/metagrover)
[β
](#tutorial-metagrover "Tutorials") [πΉ](#video-metagrover "Videos") | [
Eyal Cohen](https://github.com/eyalcohen4)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=eyalcohen4 "Documentation") | [
Peter Makowski](https://github.com/petermakowski)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=petermakowski "Documentation") |
| [
Michiel Nuyts](https://github.com/Michielnuyts)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=Michielnuyts "Documentation") | [
Joe Ng'ethe](https://github.com/joeynimu)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Code") [π](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Documentation") | [
Kate](https://github.com/Enikol)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=Enikol "Documentation") | [
Sean](http://www.seanrparker.com)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=SeanRParker "Documentation") | [
James Long](http://jlongster.com)
[π€](#ideas-jlongster "Ideas, Planning, & Feedback") [π¦](#platform-jlongster "Packaging/porting to new platform") | [
Herb Hagely](https://github.com/hhagely)
[π‘](#example-hhagely "Examples") | [
Alex Wendte](http://www.wendtedesigns.com/)
[π‘](#example-themostcolm "Examples") |
| [
Monica Powell](http://www.aboutmonica.com)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=M0nica "Documentation") | [
Vitaly Sivkov](http://sivkoff.com)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=sivkoff "Code") | [
Weyert de Boer](https://github.com/weyert)
[π€](#ideas-weyert "Ideas, Planning, & Feedback") [π](#review-weyert "Reviewed Pull Requests") | [
EstebanMarin](https://github.com/EstebanMarin)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=EstebanMarin "Documentation") | [
Victor Martins](https://github.com/vctormb)
[π](https://github.com/kentcdodds/react-testing-library/commits?author=vctormb "Documentation") | [
Royston Shufflebotham](https://github.com/RoystonS)
[π](https://github.com/kentcdodds/react-testing-library/issues?q=author%3ARoystonS "Bug reports") [π](https://github.com/kentcdodds/react-testing-library/commits?author=RoystonS "Documentation") [π‘](#example-RoystonS "Examples") | [
chrbala](https://github.com/chrbala)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=chrbala "Code") |
-| [
Donavon West](http://donavon.com)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=donavon "Code") [π](https://github.com/kentcdodds/react-testing-library/commits?author=donavon "Documentation") [π€](#ideas-donavon "Ideas, Planning, & Feedback") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=donavon "Tests") | [
Richard Maisano](https://github.com/maisano)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=maisano "Code") | [
Marco Biedermann](https://www.marcobiedermann.com)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=marcobiedermann "Code") [π§](#maintenance-marcobiedermann "Maintenance") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=marcobiedermann "Tests") | [
Alex Zherdev](https://github.com/alexzherdev)
[π](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Aalexzherdev "Bug reports") [π»](https://github.com/kentcdodds/react-testing-library/commits?author=alexzherdev "Code") | [
AndrΓ© Matulionis dos Santos](https://twitter.com/Andrewmat)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=Andrewmat "Code") [π‘](#example-Andrewmat "Examples") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=Andrewmat "Tests") |
-
+| [
Donavon West](http://donavon.com)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=donavon "Code") [π](https://github.com/kentcdodds/react-testing-library/commits?author=donavon "Documentation") [π€](#ideas-donavon "Ideas, Planning, & Feedback") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=donavon "Tests") | [
Richard Maisano](https://github.com/maisano)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=maisano "Code") | [
Marco Biedermann](https://www.marcobiedermann.com)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=marcobiedermann "Code") [π§](#maintenance-marcobiedermann "Maintenance") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=marcobiedermann "Tests") | [
Alex Zherdev](https://github.com/alexzherdev)
[π](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Aalexzherdev "Bug reports") [π»](https://github.com/kentcdodds/react-testing-library/commits?author=alexzherdev "Code") | [
AndrΓ© Matulionis dos Santos](https://twitter.com/Andrewmat)
[π»](https://github.com/kentcdodds/react-testing-library/commits?author=Andrewmat "Code") [π‘](#example-Andrewmat "Examples") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=Andrewmat "Tests") | [
Daniel K.](https://github.com/FredyC)
[π](https://github.com/kentcdodds/react-testing-library/issues?q=author%3AFredyC "Bug reports") [π»](https://github.com/kentcdodds/react-testing-library/commits?author=FredyC "Code") [π€](#ideas-FredyC "Ideas, Planning, & Feedback") [β οΈ](https://github.com/kentcdodds/react-testing-library/commits?author=FredyC "Tests") |
This project follows the [all-contributors][all-contributors] specification.
diff --git a/src/__tests__/test-hook.js b/src/__tests__/test-hook.js
index 673e9497..00b5cc80 100644
--- a/src/__tests__/test-hook.js
+++ b/src/__tests__/test-hook.js
@@ -1,4 +1,4 @@
-import {useState, useEffect} from 'react'
+import React, {useState, useEffect} from 'react'
import 'jest-dom/extend-expect'
import {testHook, cleanup} from '../'
@@ -38,3 +38,25 @@ test('returns a function to rerender component', () => {
rerender()
expect(renderCount).toBe(2)
})
+test('accepts wrapper option to wrap rendered hook with', () => {
+ const ctxA = React.createContext()
+ const ctxB = React.createContext()
+ const useHook = () => {
+ return React.useContext(ctxA) * React.useContext(ctxB)
+ }
+ let actual
+ testHook(
+ () => {
+ actual = useHook()
+ },
+ {
+ // eslint-disable-next-line react/display-name
+ wrapper: props => (
+
+
+
+ ),
+ },
+ )
+ expect(actual).toBe(12)
+})
diff --git a/src/index.js b/src/index.js
index 78b5ffae..5410d0dd 100644
--- a/src/index.js
+++ b/src/index.js
@@ -66,14 +66,19 @@ function TestHook({callback}) {
return null
}
-function testHook(callback) {
- const {unmount, rerender: rerenderComponent} = render(
- ,
- )
+function testHook(callback, options = {}) {
+ const toRender = () => {
+ const hookRender =
+ if (options.wrapper) {
+ return React.createElement(options.wrapper, null, hookRender)
+ }
+ return hookRender
+ }
+ const {unmount, rerender: rerenderComponent} = render(toRender())
return {
unmount,
rerender: () => {
- rerenderComponent()
+ rerenderComponent(toRender())
},
}
}
diff --git a/typings/index.d.ts b/typings/index.d.ts
index fa844fd2..941e4bfc 100644
--- a/typings/index.d.ts
+++ b/typings/index.d.ts
@@ -24,6 +24,10 @@ export type HookResult = {
unmount: () => boolean
}
+export type HookOptions = {
+ wrapper: React.FunctionComponent
+}
+
export interface RenderOptions {
container?: HTMLElement
baseElement?: HTMLElement
@@ -48,7 +52,7 @@ export function render(
/**
* Renders a test component that calls back to the test.
*/
-export function testHook(callback: () => void): HookResult
+export function testHook(callback: () => void, options?: Partial): HookResult
/**
* Unmounts React trees that were mounted with render.