diff --git a/packages/react-native/Libraries/Image/__tests__/Image-test.js b/packages/react-native/Libraries/Image/__tests__/Image-test.js index 46124adc88ef2a..6cdedf05178f27 100644 --- a/packages/react-native/Libraries/Image/__tests__/Image-test.js +++ b/packages/react-native/Libraries/Image/__tests__/Image-test.js @@ -13,6 +13,8 @@ import type {ElementRef} from 'react'; +import NativeImageLoaderAndroid from '../NativeImageLoaderAndroid'; +import NativeImageLoaderIOS from '../NativeImageLoaderIOS'; import {act, create} from 'react-test-renderer'; const render = require('../../../jest/renderer'); @@ -291,4 +293,62 @@ describe('', () => { expect(secondInstance).toBe(null); expect(imageInstancesFromCallback.size).toBe(0); }); + + it('should resolve asset source even when Image module is mocked', async () => { + jest.mock('../Image'); + const resolvedSource = Image.resolveAssetSource({uri: 'foo-bar.jpg'}); + expect(resolvedSource).toEqual({uri: 'foo-bar.jpg'}); + }); + + it('should compute image size even when Image module is mocked', async () => { + jest.mock('../Image'); + const mockOnGetSizeSuccess = jest.fn((width, height) => undefined); + const mockSuccessCallback = (width: number, height: number) => + mockOnGetSizeSuccess(width, height); + + await Image.getSize('foo-bar.jpg', mockSuccessCallback); + await jest.runAllTicks(); + + expect(mockOnGetSizeSuccess).toHaveBeenCalledWith(320, 240); + + await Image.getSizeWithHeaders( + 'foo-bar.jpg', + {header: 'foo'}, + mockSuccessCallback, + ); + + expect(mockOnGetSizeSuccess).toHaveBeenCalledWith(333, 222); + }); + + it('should call native prefetch methods when calling JS prefetch methods', async () => { + jest.mock('../Image'); + await Image.prefetch('foo-bar.jpg'); + expect(NativeImageLoaderIOS.prefetchImage).toHaveBeenCalledWith( + 'foo-bar.jpg', + ); + expect(NativeImageLoaderAndroid.prefetchImage).toHaveBeenCalledWith( + 'foo-bar.jpg', + ); + + await Image.prefetchWithMetadata('foo-bar.jpg', 'foo-queryRootName'); + expect(NativeImageLoaderIOS.prefetchImageWithMetadata).toHaveBeenCalledWith( + 'foo-bar.jpg', + 'foo-queryRootName', + 0, + ); + expect(NativeImageLoaderAndroid.prefetchImage).toHaveBeenCalledWith( + 'foo-bar.jpg', + ); + }); + + it('should call native queryCache method when JS queryCache method is called', async () => { + jest.mock('../Image'); + await Image.queryCache(['foo-bar.jpg']); + expect(NativeImageLoaderIOS.queryCache).toHaveBeenCalledWith([ + 'foo-bar.jpg', + ]); + expect(NativeImageLoaderIOS.queryCache).toHaveBeenCalledWith([ + 'foo-bar.jpg', + ]); + }); }); diff --git a/packages/react-native/jest/setup.js b/packages/react-native/jest/setup.js index 86cab65f6db1ef..096623e349d338 100644 --- a/packages/react-native/jest/setup.js +++ b/packages/react-native/jest/setup.js @@ -112,17 +112,9 @@ jest Constants: {}, }, })) - .mock('../Libraries/Image/Image', () => { - const Image = mockComponent('../Libraries/Image/Image'); - Image.getSize = jest.fn(); - Image.getSizeWithHeaders = jest.fn(); - Image.prefetch = jest.fn(); - Image.prefetchWithMetadata = jest.fn(); - Image.queryCache = jest.fn(); - Image.resolveAssetSource = jest.fn(); - - return Image; - }) + .mock('../Libraries/Image/Image', () => + mockComponent('../Libraries/Image/Image'), + ) .mock('../Libraries/Text/Text', () => mockComponent('../Libraries/Text/Text', MockNativeMethods), ) @@ -261,7 +253,12 @@ jest }, ImageLoader: { getSize: jest.fn(url => Promise.resolve([320, 240])), + getSizeWithHeaders: jest.fn((url, headers) => + Promise.resolve({height: 222, width: 333}), + ), prefetchImage: jest.fn(), + prefetchImageWithMetadata: jest.fn(), + queryCache: jest.fn(), }, ImageViewManager: { getSize: jest.fn((uri, success) =>