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] -[![All Contributors](https://img.shields.io/badge/all_contributors-68-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-69-orange.svg?style=flat-square)](#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
dadamssg](https://github.com/dadamssg)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=dadamssg "Documentation") | [Yazan Aabed
Yazan Aabed](https://www.yaabed.com/)
[πŸ“](#blog-YazanAabeed "Blogposts") | [Tim
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
Divyanshu Maithani](http://divyanshu.xyz)
[βœ…](#tutorial-divyanshu013 "Tutorials") [πŸ“Ή](#video-divyanshu013 "Videos") | [Deepak Grover
Deepak Grover](https://www.linkedin.com/in/metagrover)
[βœ…](#tutorial-metagrover "Tutorials") [πŸ“Ή](#video-metagrover "Videos") | [Eyal Cohen
Eyal Cohen](https://github.com/eyalcohen4)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=eyalcohen4 "Documentation") | [Peter Makowski
Peter Makowski](https://github.com/petermakowski)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=petermakowski "Documentation") | | [Michiel Nuyts
Michiel Nuyts](https://github.com/Michielnuyts)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=Michielnuyts "Documentation") | [Joe Ng'ethe
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
Kate](https://github.com/Enikol)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=Enikol "Documentation") | [Sean
Sean](http://www.seanrparker.com)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=SeanRParker "Documentation") | [James Long
James Long](http://jlongster.com)
[πŸ€”](#ideas-jlongster "Ideas, Planning, & Feedback") [πŸ“¦](#platform-jlongster "Packaging/porting to new platform") | [Herb Hagely
Herb Hagely](https://github.com/hhagely)
[πŸ’‘](#example-hhagely "Examples") | [Alex Wendte
Alex Wendte](http://www.wendtedesigns.com/)
[πŸ’‘](#example-themostcolm "Examples") | | [Monica Powell
Monica Powell](http://www.aboutmonica.com)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=M0nica "Documentation") | [Vitaly Sivkov
Vitaly Sivkov](http://sivkoff.com)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=sivkoff "Code") | [Weyert de Boer
Weyert de Boer](https://github.com/weyert)
[πŸ€”](#ideas-weyert "Ideas, Planning, & Feedback") [πŸ‘€](#review-weyert "Reviewed Pull Requests") | [EstebanMarin
EstebanMarin](https://github.com/EstebanMarin)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=EstebanMarin "Documentation") | [Victor Martins
Victor Martins](https://github.com/vctormb)
[πŸ“–](https://github.com/kentcdodds/react-testing-library/commits?author=vctormb "Documentation") | [Royston Shufflebotham
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
chrbala](https://github.com/chrbala)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=chrbala "Code") | -| [Donavon West
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
Richard Maisano](https://github.com/maisano)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=maisano "Code") | [Marco Biedermann
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
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
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
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
Richard Maisano](https://github.com/maisano)
[πŸ’»](https://github.com/kentcdodds/react-testing-library/commits?author=maisano "Code") | [Marco Biedermann
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
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
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.
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.