File tree Expand file tree Collapse file tree 5 files changed +31
-9
lines changed Expand file tree Collapse file tree 5 files changed +31
-9
lines changed Original file line number Diff line number Diff line change 1
1
import * as React from 'react' ;
2
2
import { View } from 'react-native' ;
3
3
4
- import { cleanup , render } from '../pure' ;
4
+ import { cleanupAsync , render , renderAsync } from '../pure' ;
5
5
6
6
class Test extends React . Component < { onUnmount : ( ) => void } > {
7
7
componentWillUnmount ( ) {
@@ -14,13 +14,24 @@ class Test extends React.Component<{ onUnmount: () => void }> {
14
14
}
15
15
}
16
16
17
- test ( 'cleanup' , ( ) => {
17
+ test ( 'cleanup after render' , async ( ) => {
18
18
const fn = jest . fn ( ) ;
19
19
20
20
render ( < Test onUnmount = { fn } /> ) ;
21
21
render ( < Test onUnmount = { fn } /> ) ;
22
22
expect ( fn ) . not . toHaveBeenCalled ( ) ;
23
23
24
- cleanup ( ) ;
24
+ await cleanupAsync ( ) ;
25
+ expect ( fn ) . toHaveBeenCalledTimes ( 2 ) ;
26
+ } ) ;
27
+
28
+ test ( 'cleanup after renderAsync' , async ( ) => {
29
+ const fn = jest . fn ( ) ;
30
+
31
+ await renderAsync ( < Test onUnmount = { fn } /> ) ;
32
+ await renderAsync ( < Test onUnmount = { fn } /> ) ;
33
+ expect ( fn ) . not . toHaveBeenCalled ( ) ;
34
+
35
+ await cleanupAsync ( ) ;
25
36
expect ( fn ) . toHaveBeenCalledTimes ( 2 ) ;
26
37
} ) ;
Original file line number Diff line number Diff line change 1
1
import { clearRenderResult } from './screen' ;
2
2
3
3
type CleanUpFunction = ( ) => void ;
4
+ type CleanUpFunctionAsync = ( ) => Promise < void > ;
4
5
5
- const cleanupQueue = new Set < CleanUpFunction > ( ) ;
6
+ const cleanupQueue = new Set < CleanUpFunction | CleanUpFunctionAsync > ( ) ;
6
7
7
8
export default function cleanup ( ) {
8
9
clearRenderResult ( ) ;
@@ -11,6 +12,16 @@ export default function cleanup() {
11
12
cleanupQueue . clear ( ) ;
12
13
}
13
14
14
- export function addToCleanupQueue ( fn : CleanUpFunction ) {
15
+ export async function cleanupAsync ( ) {
16
+ clearRenderResult ( ) ;
17
+
18
+ for ( const fn of cleanupQueue ) {
19
+ await fn ( ) ;
20
+ }
21
+
22
+ cleanupQueue . clear ( ) ;
23
+ }
24
+
25
+ export function addToCleanupQueue ( fn : CleanUpFunction | CleanUpFunctionAsync ) {
15
26
cleanupQueue . add ( fn ) ;
16
27
}
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import './matchers/extend-expect';
3
3
4
4
import { getIsReactActEnvironment , setReactActEnvironment } from './act' ;
5
5
import { flushMicroTasks } from './flush-micro-tasks' ;
6
- import { cleanup } from './pure' ;
6
+ import { cleanupAsync } from './pure' ;
7
7
8
8
if ( ! process ?. env ?. RNTL_SKIP_AUTO_CLEANUP ) {
9
9
// If we're running in a test runner that supports afterEach
@@ -14,7 +14,7 @@ if (!process?.env?.RNTL_SKIP_AUTO_CLEANUP) {
14
14
if ( typeof afterEach === 'function' ) {
15
15
afterEach ( async ( ) => {
16
16
await flushMicroTasks ( ) ;
17
- cleanup ( ) ;
17
+ await cleanupAsync ( ) ;
18
18
} ) ;
19
19
}
20
20
Original file line number Diff line number Diff line change 1
1
export { default as act } from './act' ;
2
- export { default as cleanup } from './cleanup' ;
2
+ export { default as cleanup , cleanupAsync } from './cleanup' ;
3
3
export { default as fireEvent , fireEventAsync } from './fire-event' ;
4
4
export { default as render } from './render' ;
5
5
export { default as renderAsync } from './render-async' ;
Original file line number Diff line number Diff line change @@ -124,7 +124,7 @@ function buildRenderResult(
124
124
} ) ;
125
125
} ;
126
126
127
- addToCleanupQueue ( unmount ) ;
127
+ addToCleanupQueue ( unmountAsync ) ;
128
128
129
129
const result = {
130
130
...getQueriesForElement ( instance ) ,
You can’t perform that action at this time.
0 commit comments