We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 91734c5 commit 01918d9Copy full SHA for 01918d9
.all-contributorsrc
@@ -183,6 +183,17 @@
183
"contributions": [
184
"test"
185
]
186
+ },
187
+ {
188
+ "login": "huchenme",
189
+ "name": "Hu Chen",
190
+ "avatar_url": "https://avatars3.githubusercontent.com/u/2078389?v=4",
191
+ "profile": "https://huchen.dev/",
192
+ "contributions": [
193
+ "code",
194
+ "doc",
195
+ "example"
196
+ ]
197
}
198
],
199
"commitConvention": "none"
README.md
@@ -164,11 +164,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
164
<td align="center"><a href="https://github.com/102"><img src="https://avatars1.githubusercontent.com/u/5839225?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Roman Gusev</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=102" title="Documentation">📖</a></td>
165
<td align="center"><a href="https://github.com/hemlok"><img src="https://avatars2.githubusercontent.com/u/9043345?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Adam Seckel</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=hemlok" title="Code">💻</a></td>
166
<td align="center"><a href="https://keiya01.github.io/portfolio"><img src="https://avatars1.githubusercontent.com/u/34934510?v=4?s=100" width="100px;" alt=""/><br /><sub><b>keiya sasaki</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=keiya01" title="Tests">⚠️</a></td>
167
+ <td align="center"><a href="https://huchen.dev/"><img src="https://avatars3.githubusercontent.com/u/2078389?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hu Chen</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=huchenme" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=huchenme" title="Documentation">📖</a> <a href="#example-huchenme" title="Examples">💡</a></td>
168
</tr>
169
</table>
170
-<!-- markdownlint-enable -->
171
+<!-- markdownlint-restore -->
172
<!-- prettier-ignore-end -->
173
+
174
<!-- ALL-CONTRIBUTORS-LIST:END -->
175
176
This project follows the [all-contributors](https://allcontributors.org/) specification.
docs/api-reference.md
@@ -10,6 +10,7 @@ route: '/reference/api'
10
- [`renderHook`](/reference/api#renderhook)
11
- [`act`](/reference/api#act)
12
- [`cleanup`](/reference/api#cleanup)
13
+- [`addCleanup`](/reference/api#addcleanup)
14
15
---
16
@@ -147,6 +148,31 @@ variable to `true` before importing `@testing-library/react-hooks` will also dis
147
148
149
150
151
+## `addCleanup`
152
153
+```js
154
+function addCleanup(
155
+ callback: function(props?: any): any
156
+): void
157
+```
158
159
+Callback to be called after `cleanup`.
160
161
+In some cases you might want to run some callback after internal `cleanup` happen, especially after
162
+`unmount` happens in `cleanup`. If the sequence matters to you, you could use `addCleanup`.
163
+import { addCleanup } from '@testing-library/react-hooks'
+jest.useFakeTimers()
+addCleanup(() => {
+ jest.runOnlyPendingTimers()
+})
+---
## Async Utilities
177
178
### `waitForNextUpdate`
src/cleanup.js
@@ -1,19 +1,25 @@
1
import flushMicroTasks from './flush-microtasks'
2
3
-let cleanupCallbacks = []
+let internalCleanupCbs = []
4
+let cleanupCbs = []
5
6
async function cleanup() {
7
await flushMicroTasks()
- cleanupCallbacks.forEach((cb) => cb())
8
- cleanupCallbacks = []
+ internalCleanupCbs.forEach((cb) => cb())
9
+ internalCleanupCbs = []
+ cleanupCbs.forEach((cb) => cb())
+}
+function addInternalCleanup(callback) {
+ internalCleanupCbs.push(callback)
17
function addCleanup(callback) {
- cleanupCallbacks.push(callback)
18
+ cleanupCbs.push(callback)
19
20
-function removeCleanup(callback) {
- cleanupCallbacks = cleanupCallbacks.filter((cb) => cb !== callback)
21
+function removeInternalCleanup(callback) {
22
+ internalCleanupCbs = internalCleanupCbs.filter((cb) => cb !== callback)
23
24
-export { cleanup, addCleanup, removeCleanup }
25
+export { cleanup, addCleanup, addInternalCleanup,removeInternalCleanup }
src/pure.js
@@ -1,7 +1,7 @@
import React, { Suspense } from 'react'
import { act, create } from 'react-test-renderer'
import asyncUtils from './asyncUtils'
-import { cleanup, addCleanup, removeCleanup } from './cleanup'
+import { cleanup, addCleanup, addInternalCleanup,removeInternalCleanup } from './cleanup'
function TestHook({ callback, hookProps, onError, children }) {
try {
@@ -84,12 +84,12 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
84
85
function unmountHook() {
86
act(() => {
87
- removeCleanup(unmountHook)
+ removeInternalCleanup(unmountHook)
88
unmount()
89
})
90
91
92
- addCleanup(unmountHook)
+ addInternalCleanup(unmountHook)
93
94
return {
95
result,
@@ -99,4 +99,4 @@ function renderHook(callback, { initialProps, wrapper } = {}) {
99
100
101
102
-export { renderHook, cleanup, act }
+export { renderHook, cleanup, act,addCleanup }
test/addCleanup.test.js
@@ -0,0 +1,27 @@
+import { useEffect } from 'react'
+import { renderHook, addCleanup } from 'src'
+let callSequence = []
+ callSequence.push('cleanup')
+ callSequence.push('another cleanup')
+describe('addCleanup tests', () => {
+ test('first', () => {
+ const hookWithCleanup = () => {
+ useEffect(() => {
+ return () => {
+ callSequence.push('unmount')
+ }
+ })
+ renderHook(() => hookWithCleanup())
+ test('second', () => {
+ expect(callSequence).toEqual(['unmount', 'cleanup', 'another cleanup'])
26
27
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments