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 11import * as React from 'react' ;
22import { View } from 'react-native' ;
33
4- import { cleanup , render } from '../pure' ;
4+ import { cleanupAsync , render , renderAsync } from '../pure' ;
55
66class Test extends React . Component < { onUnmount : ( ) => void } > {
77 componentWillUnmount ( ) {
@@ -14,13 +14,24 @@ class Test extends React.Component<{ onUnmount: () => void }> {
1414 }
1515}
1616
17- test ( 'cleanup' , ( ) => {
17+ test ( 'cleanup after render' , async ( ) => {
1818 const fn = jest . fn ( ) ;
1919
2020 render ( < Test onUnmount = { fn } /> ) ;
2121 render ( < Test onUnmount = { fn } /> ) ;
2222 expect ( fn ) . not . toHaveBeenCalled ( ) ;
2323
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 ( ) ;
2536 expect ( fn ) . toHaveBeenCalledTimes ( 2 ) ;
2637} ) ;
Original file line number Diff line number Diff line change 11import { clearRenderResult } from './screen' ;
22
33type CleanUpFunction = ( ) => void ;
4+ type CleanUpFunctionAsync = ( ) => Promise < void > ;
45
5- const cleanupQueue = new Set < CleanUpFunction > ( ) ;
6+ const cleanupQueue = new Set < CleanUpFunction | CleanUpFunctionAsync > ( ) ;
67
78export default function cleanup ( ) {
89 clearRenderResult ( ) ;
@@ -11,6 +12,16 @@ export default function cleanup() {
1112 cleanupQueue . clear ( ) ;
1213}
1314
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 ) {
1526 cleanupQueue . add ( fn ) ;
1627}
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import './matchers/extend-expect';
33
44import { getIsReactActEnvironment , setReactActEnvironment } from './act' ;
55import { flushMicroTasks } from './flush-micro-tasks' ;
6- import { cleanup } from './pure' ;
6+ import { cleanupAsync } from './pure' ;
77
88if ( ! process ?. env ?. RNTL_SKIP_AUTO_CLEANUP ) {
99 // If we're running in a test runner that supports afterEach
@@ -14,7 +14,7 @@ if (!process?.env?.RNTL_SKIP_AUTO_CLEANUP) {
1414 if ( typeof afterEach === 'function' ) {
1515 afterEach ( async ( ) => {
1616 await flushMicroTasks ( ) ;
17- cleanup ( ) ;
17+ await cleanupAsync ( ) ;
1818 } ) ;
1919 }
2020
Original file line number Diff line number Diff line change 11export { default as act } from './act' ;
2- export { default as cleanup } from './cleanup' ;
2+ export { default as cleanup , cleanupAsync } from './cleanup' ;
33export { default as fireEvent , fireEventAsync } from './fire-event' ;
44export { default as render } from './render' ;
55export { default as renderAsync } from './render-async' ;
Original file line number Diff line number Diff line change @@ -124,7 +124,7 @@ function buildRenderResult(
124124 } ) ;
125125 } ;
126126
127- addToCleanupQueue ( unmount ) ;
127+ addToCleanupQueue ( unmountAsync ) ;
128128
129129 const result = {
130130 ...getQueriesForElement ( instance ) ,
You can’t perform that action at this time.
0 commit comments