File tree Expand file tree Collapse file tree 6 files changed +92
-7
lines changed Expand file tree Collapse file tree 6 files changed +92
-7
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,45 @@ toJSON(): ReactTestRendererJSON | null
115
115
116
116
Get the rendered component JSON representation, e.g. for snapshot testing.
117
117
118
+ ## ` cleanup `
119
+
120
+ ``` ts
121
+ const cleanup: () => void
122
+ ` ` `
123
+
124
+ Unmounts React trees that were mounted with ` render ` .
125
+
126
+ For example, if you're using the ` jest ` testing framework, then you would need to use the ` afterEach ` hook like so:
127
+
128
+ ` ` ` jsx
129
+ import { cleanup , render } from ' react-native-testing-library'
130
+ import { View } from ' react-native'
131
+
132
+ afterEach (cleanup )
133
+
134
+ it (' renders a view' , () => {
135
+ render (<View />)
136
+ // ...
137
+ })
138
+ ```
139
+
140
+ The ` afterEach(cleanup) ` call also works in ` describe ` blocks:
141
+
142
+ ``` jsx
143
+ describe (' when logged in' , () => {
144
+ afterEach (cleanup)
145
+
146
+ it (' renders the user' , () => {
147
+ render (< SiteHeader / > )
148
+ // ...
149
+ });
150
+ })
151
+ ```
152
+
153
+ Failing to call ` cleanup ` when you've called ` render ` could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
154
+
155
+ The alternative to ` cleanup ` is balancing every ` render ` with an ` unmount ` method call.
156
+
118
157
## ` fireEvent `
119
158
120
159
``` ts
Original file line number Diff line number Diff line change
1
+ // @flow
2
+ /* eslint-disable react/no-multi-comp */
3
+ import React from 'react' ;
4
+ import { View } from 'react-native' ;
5
+ import { cleanup , render } from '..' ;
6
+
7
+ class Test extends React . Component < * > {
8
+ componentWillUnmount ( ) {
9
+ if ( this . props . onUnmount ) {
10
+ this . props . onUnmount ( ) ;
11
+ }
12
+ }
13
+ render ( ) {
14
+ return < View /> ;
15
+ }
16
+ }
17
+
18
+ test ( 'cleanup' , ( ) => {
19
+ const fn = jest . fn ( ) ;
20
+
21
+ render ( < Test onUnmount = { fn } /> ) ;
22
+ render ( < Test onUnmount = { fn } /> ) ;
23
+ expect ( fn ) . not . toHaveBeenCalled ( ) ;
24
+
25
+ cleanup ( ) ;
26
+ expect ( fn ) . toHaveBeenCalledTimes ( 2 ) ;
27
+ } ) ;
Original file line number Diff line number Diff line change
1
+ // @flow
2
+ let cleanupQueue = new Set ( ) ;
3
+
4
+ export default function cleanup ( ) {
5
+ cleanupQueue . forEach ( fn => fn ( ) ) ;
6
+ cleanupQueue . clear ( ) ;
7
+ }
8
+
9
+ export function addToCleanupQueue (
10
+ fn : ( nextElement ?: React$Element < any > ) => void
11
+ ) {
12
+ cleanupQueue. add ( fn ) ;
13
+ }
Original file line number Diff line number Diff line change 1
1
// @flow
2
2
import act from './act' ;
3
- import render from './render' ;
4
- import shallow from './shallow' ;
5
- import flushMicrotasksQueue from './flushMicrotasksQueue' ;
3
+ import cleanup from './cleanup' ;
6
4
import debug from './debug' ;
7
5
import fireEvent from './fireEvent' ;
6
+ import flushMicrotasksQueue from './flushMicrotasksQueue' ;
7
+ import render from './render' ;
8
+ import shallow from './shallow' ;
8
9
import waitForElement from './waitForElement' ;
9
10
10
- export { render } ;
11
- export { shallow } ;
12
- export { flushMicrotasksQueue } ;
11
+ export { act } ;
12
+ export { cleanup } ;
13
13
export { debug } ;
14
14
export { fireEvent } ;
15
+ export { flushMicrotasksQueue } ;
16
+ export { render } ;
17
+ export { shallow } ;
15
18
export { waitForElement } ;
16
- export { act } ;
Original file line number Diff line number Diff line change 2
2
import * as React from 'react' ;
3
3
import TestRenderer , { type ReactTestRenderer } from 'react-test-renderer' ; // eslint-disable-line import/no-extraneous-dependencies
4
4
import act from './act' ;
5
+ import { addToCleanupQueue } from './cleanup' ;
5
6
import { getByAPI } from './helpers/getByAPI' ;
6
7
import { queryByAPI } from './helpers/queryByAPI' ;
7
8
import a11yAPI from './helpers/a11yAPI' ;
@@ -34,6 +35,8 @@ export default function render<T>(
34
35
const update = updateWithAct ( renderer , wrap ) ;
35
36
const instance = renderer . root ;
36
37
38
+ addToCleanupQueue ( renderer . unmount ) ;
39
+
37
40
return {
38
41
...getByAPI ( instance ) ,
39
42
...queryByAPI ( instance ) ,
Original file line number Diff line number Diff line change @@ -135,6 +135,7 @@ export declare const shallow: <P = {}>(
135
135
instance : ReactTestInstance | React . ReactElement < P >
136
136
) => { output : React . ReactElement < P > } ;
137
137
export declare const flushMicrotasksQueue : ( ) => Promise < any > ;
138
+ export declare const cleanup : ( ) => void ;
138
139
export declare const debug : DebugAPI ;
139
140
export declare const fireEvent : FireEventAPI ;
140
141
export declare const waitForElement : WaitForElementFunction ;
You can’t perform that action at this time.
0 commit comments