Skip to content

Commit 13ba272

Browse files
author
apostolidhs
committed
fixup! Added cleanup method
1 parent df66140 commit 13ba272

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ npm install --save-dev react-test-renderer@^x.y.z
126126
Both of these dependecies must be installed as at least version `16.8.0` to be compatible with
127127
`react-hooks-testing-library`.
128128

129-
### `cleanup()`
129+
### `unmountAll()`
130130

131131
Unmounts any of the Hooks that were mounted with `renderHook`.
132132

133-
Optionally, it is possible to import `cleanup` in a global test file. Using that way, it isn't
134-
necessary to run `afterEach(cleanup)` on every test script.
133+
Optionally, it is possible to import `unmountAll` in a global test file. Using that way, it isn't
134+
necessary to run `afterEach(unmountAll)` on every test script.
135135

136136
```js
137137
import 'react-hooks-testing-library/cleanup-after-each'

cleanup-after-each.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
afterEach(require('./src').cleanup)
1+
afterEach(require('./lib').unmountAll)

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Suspense } from 'react'
22
import { act, create } from 'react-test-renderer'
33

4-
const unmounts = []
4+
let unmounts = []
55

66
function TestHook({ callback, hookProps, onError, children }) {
77
try {
@@ -53,9 +53,8 @@ function resultContainer() {
5353
}
5454
}
5555

56-
function cleanup() {
56+
function unmountAll() {
5757
unmounts.forEach((unmount) => unmount())
58-
unmounts.length = 0
5958
}
6059

6160
function renderHook(callback, { initialProps, wrapper } = {}) {
@@ -82,6 +81,7 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
8281

8382
function unmountHook() {
8483
act(() => {
84+
unmounts = unmounts.filter((u) => u !== unmountHook)
8585
unmount()
8686
})
8787
}
@@ -101,4 +101,4 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
101101
}
102102
}
103103

104-
export { renderHook, cleanup, act }
104+
export { renderHook, unmountAll, act }

test/cleanup.test.js renamed to test/unmountAll.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from 'react'
2-
import { renderHook, cleanup } from 'src'
2+
import { renderHook, unmountAll } from 'src'
33

4-
describe('cleanup tests', () => {
4+
describe('unmountAll tests', () => {
55
let sideEffect = {}
66

77
function useEffectsCounter({ initialProps }) {
@@ -25,24 +25,24 @@ describe('cleanup tests', () => {
2525
useEffectsCounter({ initialProps: { id: 10 } })
2626
useEffectsCounter({ initialProps: { id: 100 } })
2727

28-
cleanup()
28+
unmountAll()
2929

3030
expect(sideEffect).toEqual({ 1: 1, 10: 1, 100: 1 })
3131
})
3232

33-
test('should not cleanup a hook that have already unmounted', () => {
33+
test('should not unmountAll a hook that have already unmounted', () => {
3434
const { unmount } = useEffectsCounter({ initialProps: { id: 1 } })
3535

3636
unmount()
37-
cleanup()
37+
unmountAll()
3838

3939
expect(sideEffect).toEqual({ 1: 1 })
4040
})
4141

42-
test('should not unmount a hook that have already cleaned up', () => {
42+
test('should not unmount a hook that have already unmounted up', () => {
4343
const { unmount } = useEffectsCounter({ initialProps: { id: 1 } })
4444

45-
cleanup()
45+
unmountAll()
4646
unmount()
4747

4848
expect(sideEffect).toEqual({ 1: 1 })

typings/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ export function renderHook<P, R>(
2323
options?: RenderHookOptions<P>
2424
): RenderHookResult<P, R>
2525

26-
export function cleanup(): void
26+
export function unmountAll(): void

0 commit comments

Comments
 (0)