Skip to content

Commit 553c243

Browse files
authored
test: Fix failures due to image component modifications (#58213)
The mobile image component introduce new asynchronous effects. We must await the result of those effect, in the form of asserting UI changes from the subsequent state updates, to satisfy the React test utilities expectation that unexpected re-renders do not occur after the test completes. Additionally, there were instances of awaiting `act` invocations that were not asynchronous. The erroneous usage of `await` for these synchronous `act` calls resulted in cryptic errors. In reality, the logic run within these `act` calls are synchronous and should merely be wrapped in `act` to signal that they result in a re-render.
1 parent e71e4bf commit 553c243

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

packages/components/src/mobile/image/index.native.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ const ImageComponent = ( {
305305
resizeMethod: 'scale',
306306
} ) }
307307
resizeMode={ imageResizeMode }
308+
testID={ `network-image-${ url }` }
308309
/>
309310
) }
310311
{ ! networkImageLoaded && ! networkURL && (
@@ -334,6 +335,11 @@ const ImageComponent = ( {
334335
resizeMethod: 'scale',
335336
} ) }
336337
resizeMode={ imageResizeMode }
338+
testID={ `network-image-${
339+
networkURL && networkImageLoaded
340+
? networkURL
341+
: localURL || url
342+
}` }
337343
/>
338344
<Image
339345
source={ { uri: networkURL } }

packages/edit-post/src/test/editor.native.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ describe( 'Editor', () => {
103103
await initializeEditor();
104104

105105
// Act
106-
await act( () => mediaAppendCallback( MEDIA[ 0 ] ) );
107-
await act( () => mediaAppendCallback( MEDIA[ 2 ] ) );
106+
act( () => mediaAppendCallback( MEDIA[ 0 ] ) );
107+
act( () => mediaAppendCallback( MEDIA[ 2 ] ) );
108+
await screen.findByTestId( `network-image-${ MEDIA[ 0 ].serverUrl }` );
109+
await screen.findByTestId( `network-image-${ MEDIA[ 2 ].serverUrl }` );
108110

109111
// Assert
110112
expect( getEditorHtml() ).toMatchSnapshot();
@@ -122,10 +124,11 @@ describe( 'Editor', () => {
122124
await initializeEditor();
123125

124126
// Act
125-
await act( () => mediaAppendCallback( MEDIA[ 0 ] ) );
127+
act( () => mediaAppendCallback( MEDIA[ 0 ] ) );
126128
// Unsupported type (PDF file)
127-
await act( () => mediaAppendCallback( MEDIA[ 1 ] ) );
128-
await act( () => mediaAppendCallback( MEDIA[ 3 ] ) );
129+
act( () => mediaAppendCallback( MEDIA[ 1 ] ) );
130+
act( () => mediaAppendCallback( MEDIA[ 3 ] ) );
131+
await screen.findByTestId( `network-image-${ MEDIA[ 0 ].serverUrl }` );
129132

130133
// Assert
131134
expect( getEditorHtml() ).toMatchSnapshot();

test/native/setup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ jest.spyOn( Image, 'getSize' ).mockImplementation( ( url, success ) =>
284284
success( 0, 0 )
285285
);
286286

287+
jest.spyOn( Image, 'prefetch' ).mockImplementation(
288+
( url, callback = () => {} ) => {
289+
const mockRequestId = `mockRequestId-${ url }`;
290+
callback( mockRequestId );
291+
return Promise.resolve( true );
292+
}
293+
);
294+
287295
jest.mock( 'react-native/Libraries/Utilities/BackHandler', () => {
288296
return jest.requireActual(
289297
'react-native/Libraries/Utilities/__mocks__/BackHandler.js'

0 commit comments

Comments
 (0)