11import React from 'react' ;
22import { renderHook , render } from '@testing-library/react-native' ;
3- import { perf , wait } from 'react-performance-testing/native' ;
43import useIMGElementState from '../useIMGElementState' ;
54import { Image } from 'react-native' ;
65import { waitFor } from '@testing-library/react-native' ;
76
8- /**
9- * Renders the hook inside a named component to allow extracting perf results
10- */
117function renderHookForPerf ( renderFn : ( ) => void ) {
8+ let renderCount = 0 ;
129 function TestComponent ( ) {
10+ renderCount ++ ;
1311 renderFn ( ) ;
1412 return null ;
1513 }
16-
17- return render ( React . createElement ( TestComponent ) ) ;
14+ const utils = render ( React . createElement ( TestComponent ) ) ;
15+ return { ... utils , getRenderCount : ( ) => renderCount } ;
1816}
1917
2018describe ( 'useIMGElementState' , ( ) => {
@@ -25,34 +23,30 @@ describe('useIMGElementState', () => {
2523 computeMaxWidth : ( contentWidth : number ) => contentWidth
2624 } ;
2725 it ( 'should render at most twice when width and height physical dimensions are not provided, prior and after fetching physical dimensions' , async ( ) => {
28- const { renderCount } = perf < { TestComponent : unknown } > ( React ) ;
29-
30- renderHookForPerf ( ( ) => useIMGElementState ( props ) ) ;
31- await wait ( ( ) => {
32- expect ( renderCount . current . TestComponent . value ) . toBeLessThan ( 2 ) ;
26+ const { getRenderCount } = renderHookForPerf ( ( ) => useIMGElementState ( props ) ) ;
27+ await waitFor ( ( ) => {
28+ expect ( getRenderCount ( ) ) . toBeLessThan ( 2 ) ;
3329 } ) ;
3430 } ) ;
3531 it ( 'should use Image.getSizeWithHeaders when source has `headers`' , async ( ) => {
36- const { renderCount } = perf < { TestComponent : unknown } > ( React ) ;
3732 const source = { uri : 'http://via.placeholder.com/640x360' , headers : { } } ;
3833 const localProps = { ...props , source } ;
39- renderHookForPerf ( ( ) => useIMGElementState ( localProps ) ) ;
40- await wait ( ( ) => {
41- expect ( renderCount . current . TestComponent . value ) . toBeLessThan ( 2 ) ;
34+ const { getRenderCount } = renderHookForPerf ( ( ) => useIMGElementState ( localProps ) ) ;
35+ await waitFor ( ( ) => {
36+ expect ( getRenderCount ( ) ) . toBeLessThan ( 2 ) ;
4237 } ) ;
4338 expect ( Image . getSizeWithHeaders ) . toHaveBeenCalled ( ) ;
4439 } ) ;
4540 it ( 'should render once when width and height physical dimensions are provided, bypassing the fetching of physical dimensions' , async ( ) => {
46- const { renderCount } = perf < { TestComponent : unknown } > ( React ) ;
47- renderHookForPerf ( ( ) =>
41+ const { getRenderCount } = renderHookForPerf ( ( ) =>
4842 useIMGElementState ( {
4943 ...props ,
5044 width : 600 ,
5145 height : 300
5246 } )
5347 ) ;
54- await wait ( ( ) => {
55- expect ( renderCount . current . TestComponent . value ) . toBe ( 1 ) ;
48+ await waitFor ( ( ) => {
49+ expect ( getRenderCount ( ) ) . toBe ( 1 ) ;
5650 } ) ;
5751 } ) ;
5852 it ( 'should start in loading state with dimensions set to initialDimensions' , async ( ) => {
0 commit comments