From 72ccbb9fbb9c685e12621856893ec3efe9dc4af9 Mon Sep 17 00:00:00 2001 From: apostolidhs Date: Sun, 23 Jun 2019 14:10:32 +0300 Subject: [PATCH 1/2] Added cleanup method --- README.md | 11 ++++++++++ cleanup-after-each.js | 1 + src/index.js | 23 ++++++++++++++------ test/cleanup.test.js | 50 +++++++++++++++++++++++++++++++++++++++++++ typings/index.d.ts | 2 ++ 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 cleanup-after-each.js create mode 100644 test/cleanup.test.js diff --git a/README.md b/README.md index be2d6204..bf6b20c7 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,17 @@ npm install --save-dev react-test-renderer@^x.y.z Both of these dependecies must be installed as at least version `16.8.0` to be compatible with `react-hooks-testing-library`. +### `cleanup()` + +Unmounts any of the Hooks that were mounted with `renderHook`. + +Optionally, it is possible to import `cleanup` in a global test file. Using that way, it isn't +necessary to run `afterEach(cleanup)` on every test script. + +```js +import 'react-hooks-testing-library/cleanup-after-each' +``` + ## Documentation There are some [work-in-progress docs here](https://react-hooks-testing-library.com/). Please leave diff --git a/cleanup-after-each.js b/cleanup-after-each.js new file mode 100644 index 00000000..bdf5f768 --- /dev/null +++ b/cleanup-after-each.js @@ -0,0 +1 @@ +afterEach(require('./src').cleanup) diff --git a/src/index.js b/src/index.js index 0245a6c3..dfffb221 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ import React, { Suspense } from 'react' import { act, create } from 'react-test-renderer' +const unmounts = [] + function TestHook({ callback, hookProps, onError, children }) { try { children(callback(hookProps)) @@ -51,6 +53,11 @@ function resultContainer() { } } +function cleanup() { + unmounts.forEach((unmount) => unmount()) + unmounts.length = 0 +} + function renderHook(callback, { initialProps, wrapper } = {}) { const { result, setValue, setError, addResolver } = resultContainer() const hookProps = { current: initialProps } @@ -73,6 +80,14 @@ function renderHook(callback, { initialProps, wrapper } = {}) { }) const { unmount, update } = testRenderer + function unmountHook() { + act(() => { + unmount() + }) + } + + unmounts.push(unmountHook) + return { result, waitForNextUpdate: () => new Promise((resolve) => addResolver(resolve)), @@ -82,12 +97,8 @@ function renderHook(callback, { initialProps, wrapper } = {}) { update(toRender()) }) }, - unmount: () => { - act(() => { - unmount() - }) - } + unmount: unmountHook } } -export { renderHook, act } +export { renderHook, cleanup, act } diff --git a/test/cleanup.test.js b/test/cleanup.test.js new file mode 100644 index 00000000..872b5585 --- /dev/null +++ b/test/cleanup.test.js @@ -0,0 +1,50 @@ +import { useEffect } from 'react' +import { renderHook, cleanup } from 'src' + +describe('cleanup tests', () => { + let sideEffect = {} + + function useEffectsCounter({ initialProps }) { + return renderHook( + ({ id }) => { + useEffect(() => { + sideEffect[id] = 0 + return () => { + sideEffect[id] = sideEffect[id] + 1 + } + }, [id]) + }, + { initialProps } + ) + } + + afterEach(() => (sideEffect = {})) + + test('should unmount the tests', () => { + useEffectsCounter({ initialProps: { id: 1 } }) + useEffectsCounter({ initialProps: { id: 10 } }) + useEffectsCounter({ initialProps: { id: 100 } }) + + cleanup() + + expect(sideEffect).toEqual({ 1: 1, 10: 1, 100: 1 }) + }) + + test('should not cleanup a hook that have already unmounted', () => { + const { unmount } = useEffectsCounter({ initialProps: { id: 1 } }) + + unmount() + cleanup() + + expect(sideEffect).toEqual({ 1: 1 }) + }) + + test('should not unmount a hook that have already cleaned up', () => { + const { unmount } = useEffectsCounter({ initialProps: { id: 1 } }) + + cleanup() + unmount() + + expect(sideEffect).toEqual({ 1: 1 }) + }) +}) diff --git a/typings/index.d.ts b/typings/index.d.ts index de113325..319e07d1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -22,3 +22,5 @@ export function renderHook( callback: (props: P) => R, options?: RenderHookOptions

): RenderHookResult + +export function cleanup(): void From df661401ea34fcc787bbffc85bd88689d1fa3b50 Mon Sep 17 00:00:00 2001 From: apostolidhs Date: Tue, 25 Jun 2019 00:37:43 +0300 Subject: [PATCH 2/2] Added contribution --- .all-contributorsrc | 10 ++++++++++ README.md | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 53aaac86..46813049 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -111,6 +111,16 @@ "bug", "code" ] + }, + { + "login": "apostolidhs", + "name": "John Apostolidis", + "avatar_url": "https://avatars3.githubusercontent.com/u/2164902?v=4", + "profile": "http://apostolidis.me", + "contributions": [ + "code", + "doc" + ] } ], "commitConvention": "none" diff --git a/README.md b/README.md index bf6b20c7..e942d2c0 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ [![downloads](https://img.shields.io/npm/dm/@testing-library/react-hooks.svg?style=flat-square)](http://www.npmtrends.com/@testing-library/react-hooks) [![MIT License](https://img.shields.io/npm/l/@testing-library/react-hooks.svg?style=flat-square)](https://github.com/testing-library/react-hooks-testing-library/blob/master/LICENSE.md) -[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) [![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/testing-library/react-hooks-testing-library/blob/master/CODE_OF_CONDUCT.md) [![Netlify Status](https://api.netlify.com/api/v1/badges/9a8f27a5-df38-4910-a248-4908b1ba29a7/deploy-status)](https://app.netlify.com/sites/react-hooks-testing-library/deploys) @@ -154,7 +154,23 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d -
Michael Peyper
Michael Peyper

💻 🎨 📖 🤔 🚇 📦 ⚠️ 🔧
otofu-square
otofu-square

💻
Patrick P. Henley
Patrick P. Henley

🤔 👀
Matheus Marques
Matheus Marques

💻
Dhruv Patel
Dhruv Patel

🐛 👀
Nathaniel Tucker
Nathaniel Tucker

🐛 👀
Sergei Grishchenko
Sergei Grishchenko

💻 📖 🤔
Josep M Sobrepere
Josep M Sobrepere

📖
Marcel Tinner
Marcel Tinner

📖
Daniel K.
Daniel K.

🐛 💻
+ + + + + + + + + + + + + + + + +
Michael Peyper
Michael Peyper

💻 🎨 📖 🤔 🚇 📦 ⚠️ 🔧
otofu-square
otofu-square

💻
Patrick P. Henley
Patrick P. Henley

🤔 👀
Matheus Marques
Matheus Marques

💻
Dhruv Patel
Dhruv Patel

🐛 👀
Nathaniel Tucker
Nathaniel Tucker

🐛 👀
Sergei Grishchenko
Sergei Grishchenko

💻 📖 🤔
Josep M Sobrepere
Josep M Sobrepere

📖
Marcel Tinner
Marcel Tinner

📖
Daniel K.
Daniel K.

🐛 💻
John Apostolidis
John Apostolidis

💻 📖