11import React from 'react' ;
2- import { renderHook } from '@testing-library/react-hooks ' ;
3- import { perf , wait } from 'react-performance-testing' ;
2+ import { act , renderHook , render } from '@testing-library/react-native ' ;
3+ import { perf , wait } from 'react-performance-testing/native ' ;
44import useIMGElementState from '../useIMGElementState' ;
55import { Image } from 'react-native' ;
66import { waitFor } from '@testing-library/react-native' ;
77
8+ /**
9+ * Renders the hook inside a named component to allow extracting perf results
10+ */
11+ function renderHookForPerf ( renderFn : ( ) => void ) {
12+ function TestComponent ( ) {
13+ renderFn ( ) ;
14+ return null ;
15+ }
16+
17+ return render ( React . createElement ( TestComponent ) ) ;
18+ }
19+
820describe ( 'useIMGElementState' , ( ) => {
921 const props = {
1022 contentWidth : 300 ,
@@ -14,7 +26,8 @@ describe('useIMGElementState', () => {
1426 } ;
1527 it ( 'should render at most twice when width and height physical dimensions are not provided, prior and after fetching physical dimensions' , async ( ) => {
1628 const { renderCount } = perf < { TestComponent : unknown } > ( React ) ;
17- renderHook ( ( ) => useIMGElementState ( props ) ) ;
29+
30+ renderHookForPerf ( ( ) => useIMGElementState ( props ) ) ;
1831 await wait ( ( ) => {
1932 expect ( renderCount . current . TestComponent . value ) . toBeLessThan ( 2 ) ;
2033 } ) ;
@@ -23,15 +36,15 @@ describe('useIMGElementState', () => {
2336 const { renderCount } = perf < { TestComponent : unknown } > ( React ) ;
2437 const source = { uri : 'http://via.placeholder.com/640x360' , headers : { } } ;
2538 const localProps = { ...props , source } ;
26- renderHook ( ( ) => useIMGElementState ( localProps ) ) ;
39+ renderHookForPerf ( ( ) => useIMGElementState ( localProps ) ) ;
2740 await wait ( ( ) => {
2841 expect ( renderCount . current . TestComponent . value ) . toBeLessThan ( 2 ) ;
2942 } ) ;
3043 expect ( Image . getSizeWithHeaders ) . toHaveBeenCalled ( ) ;
3144 } ) ;
3245 it ( 'should render once when width and height physical dimensions are provided, bypassing the fetching of physical dimensions' , async ( ) => {
3346 const { renderCount } = perf < { TestComponent : unknown } > ( React ) ;
34- renderHook ( ( ) =>
47+ renderHookForPerf ( ( ) =>
3548 useIMGElementState ( {
3649 ...props ,
3750 width : 600 ,
0 commit comments