From 9f286058da779d74ed84365ea5aa769044aefc99 Mon Sep 17 00:00:00 2001
From: andre-matulionis-ifood
Date: Fri, 8 Feb 2019 14:02:24 -0200
Subject: [PATCH 1/4] Returns unmount function on testHook
---
src/index.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/index.js b/src/index.js
index 0f4c7d47..e6423873 100644
--- a/src/index.js
+++ b/src/index.js
@@ -67,7 +67,8 @@ function TestHook({callback}) {
}
function testHook(callback) {
- render()
+ const { unmount } = render()
+ return unmount
}
function cleanup() {
From 53d2b3844f8fcd4c1332a58f44d5f5d97c58bf9c Mon Sep 17 00:00:00 2001
From: Andrewmat
Date: Fri, 8 Feb 2019 20:55:12 -0200
Subject: [PATCH 2/4] Adds test for unmount function; Adds example of useEffect
---
examples/__tests__/react-hooks.js | 91 +++++++++++++++++++------------
examples/react-hooks.js | 15 ++++-
src/__tests__/test-hook.js | 16 +++++-
src/index.js | 2 +-
4 files changed, 85 insertions(+), 39 deletions(-)
diff --git a/examples/__tests__/react-hooks.js b/examples/__tests__/react-hooks.js
index 5e001a37..6e68735d 100644
--- a/examples/__tests__/react-hooks.js
+++ b/examples/__tests__/react-hooks.js
@@ -6,53 +6,76 @@
*/
import {testHook, act, cleanup} from 'react-testing-library'
-import useCounter from '../react-hooks'
+import { useCounter, useDocumentTitle } from '../react-hooks'
afterEach(cleanup)
-test('accepts default initial values', () => {
- let count
- testHook(() => ({count} = useCounter()))
+describe('useCounter', () => {
+ test('accepts default initial values', () => {
+ let count
+ testHook(() => ({count} = useCounter()))
- expect(count).toBe(0)
-})
+ expect(count).toBe(0)
+ })
-test('accepts a default initial value for `count`', () => {
- let count
- testHook(() => ({count} = useCounter({})))
+ test('accepts a default initial value for `count`', () => {
+ let count
+ testHook(() => ({count} = useCounter({})))
- expect(count).toBe(0)
-})
+ expect(count).toBe(0)
+ })
-test('provides an `increment` function', () => {
- let count, increment
- testHook(() => ({count, increment} = useCounter({step: 2})))
+ test('provides an `increment` function', () => {
+ let count, increment
+ testHook(() => ({count, increment} = useCounter({step: 2})))
- expect(count).toBe(0)
- act(() => {
- increment()
+ expect(count).toBe(0)
+ act(() => {
+ increment()
+ })
+ expect(count).toBe(2)
})
- expect(count).toBe(2)
-})
-test('provides an `decrement` function', () => {
- let count, decrement
- testHook(() => ({count, decrement} = useCounter({step: 2})))
+ test('provides an `decrement` function', () => {
+ let count, decrement
+ testHook(() => ({count, decrement} = useCounter({step: 2})))
- expect(count).toBe(0)
- act(() => {
- decrement()
+ expect(count).toBe(0)
+ act(() => {
+ decrement()
+ })
+ expect(count).toBe(-2)
})
- expect(count).toBe(-2)
-})
-test('accepts a default initial value for `step`', () => {
- let count, increment
- testHook(() => ({count, increment} = useCounter({})))
+ test('accepts a default initial value for `step`', () => {
+ let count, increment
+ testHook(() => ({count, increment} = useCounter({})))
- expect(count).toBe(0)
- act(() => {
- increment()
+ expect(count).toBe(0)
+ act(() => {
+ increment()
+ })
+ expect(count).toBe(1)
})
- expect(count).toBe(1)
})
+
+describe('useDocumentTitle', () => {
+ test('sets a title', () => {
+ document.title = 'original title'
+ testHook(() => {
+ useDocumentTitle('modified title')
+ })
+
+ expect(document.title).toBe('modified title')
+ })
+
+ test('returns to original title when component is unmounted', () => {
+ document.title = 'original title'
+ const { unmount } = testHook(() => {
+ useDocumentTitle('modified title')
+ })
+
+ unmount()
+ expect(document.title).toBe('original title')
+ })
+})
\ No newline at end of file
diff --git a/examples/react-hooks.js b/examples/react-hooks.js
index fad0de4c..141e8452 100644
--- a/examples/react-hooks.js
+++ b/examples/react-hooks.js
@@ -1,10 +1,19 @@
-import {useState} from 'react'
+import {useState, useEffect} from 'react'
-function useCounter({initialCount = 0, step = 1} = {}) {
+export function useCounter({initialCount = 0, step = 1} = {}) {
const [count, setCount] = useState(initialCount)
const increment = () => setCount(c => c + step)
const decrement = () => setCount(c => c - step)
return {count, increment, decrement}
}
-export default useCounter
+export function useDocumentTitle(title) {
+ const [originalTitle, setOriginalTitle] = useState(document.title)
+ useEffect(() => {
+ setOriginalTitle(document.title)
+ document.title = title
+ return () => {
+ document.title = originalTitle
+ }
+ }, [title])
+}
diff --git a/src/__tests__/test-hook.js b/src/__tests__/test-hook.js
index f6bce9bd..301bee67 100644
--- a/src/__tests__/test-hook.js
+++ b/src/__tests__/test-hook.js
@@ -1,4 +1,4 @@
-import {useState} from 'react'
+import {useState, useEffect} from 'react'
import 'jest-dom/extend-expect'
import {testHook, cleanup} from '../'
@@ -12,3 +12,17 @@ test('testHook calls the callback', () => {
test('confirm we can safely call a React Hook from within the callback', () => {
testHook(() => useState())
})
+test('returns a function to unmount the component', () => {
+ let isMounted
+ const { unmount } = testHook(() => {
+ useEffect(() => {
+ isMounted = true
+ return () => {
+ isMounted = false
+ }
+ })
+ })
+ expect(isMounted).toBe(true)
+ unmount()
+ expect(isMounted).toBe(false)
+})
diff --git a/src/index.js b/src/index.js
index e6423873..85d0827d 100644
--- a/src/index.js
+++ b/src/index.js
@@ -68,7 +68,7 @@ function TestHook({callback}) {
function testHook(callback) {
const { unmount } = render()
- return unmount
+ return { unmount }
}
function cleanup() {
From 3d92dcc6a9689e12539c0f4498a0f081876b5410 Mon Sep 17 00:00:00 2001
From: Andrewmat
Date: Sat, 9 Feb 2019 21:19:57 -0200
Subject: [PATCH 3/4] Add Andrewmat as contributor
---
.all-contributorsrc | 11 +++++++++++
README.md | 4 ++--
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/.all-contributorsrc b/.all-contributorsrc
index 9edfc26a..dea90e11 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -657,6 +657,17 @@
"bug",
"code"
]
+ },
+ {
+ "login": "Andrewmat",
+ "name": "AndrΓ© Matulionis dos Santos",
+ "avatar_url": "https://avatars0.githubusercontent.com/u/5133846?v=4",
+ "profile": "https://twitter.com/Andrewmat",
+ "contributions": [
+ "code",
+ "example",
+ "test"
+ ]
}
]
}
diff --git a/README.md b/README.md
index 08cb5c43..49de2ab5 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,7 +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") |
+| [
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") |
From f99b41ad6f58b8911fd15ad5e4a145ad6fcac099 Mon Sep 17 00:00:00 2001
From: Andrewmat
Date: Sun, 10 Feb 2019 12:57:42 -0200
Subject: [PATCH 4/4] Adds rerender on testHook return
---
.github/ISSUE_TEMPLATE.md | 3 +-
.../__tests__/mock.react-transition-group.js | 5 +--
examples/__tests__/react-hooks.js | 44 +++++++++++++++++--
examples/react-hooks.js | 6 +++
src/__tests__/test-hook.js | 16 ++++++-
src/index.js | 11 ++++-
6 files changed, 74 insertions(+), 11 deletions(-)
diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md
index 5114fdbd..724a7df6 100644
--- a/.github/ISSUE_TEMPLATE.md
+++ b/.github/ISSUE_TEMPLATE.md
@@ -59,7 +59,8 @@ What happened:
-Reproduction repository: https://github.com/alexkrolick/dom-testing-library-template
+Reproduction repository:
+https://github.com/alexkrolick/dom-testing-library-template