diff --git a/src/components/Attachments/AttachmentView/AttachmentViewImage/index.tsx b/src/components/Attachments/AttachmentView/AttachmentViewImage/index.tsx index 337f75f2d713..31901e0a47d5 100644 --- a/src/components/Attachments/AttachmentView/AttachmentViewImage/index.tsx +++ b/src/components/Attachments/AttachmentView/AttachmentViewImage/index.tsx @@ -11,7 +11,7 @@ import React from 'react'; import type {AttachmentViewProps} from '..'; type AttachmentViewImageProps = Pick & { - url: string; + url: string | number; loadComplete: boolean; diff --git a/src/components/Attachments/AttachmentView/index.tsx b/src/components/Attachments/AttachmentView/index.tsx index 7c685f449d7a..9ee0ca887cc3 100644 --- a/src/components/Attachments/AttachmentView/index.tsx +++ b/src/components/Attachments/AttachmentView/index.tsx @@ -258,7 +258,7 @@ function AttachmentView({ // will appear with a source that is a blob const isSourcePDF = typeof source === 'string' && Str.isPDF(source); const isFilePDF = file && Str.isPDF(file.name ?? translate('attachmentView.unknownFilename')); - if (!hasPDFFailedToLoad && !isUploading && (isSourcePDF || isFilePDF)) { + if (!hasPDFFailedToLoad && !isUploading && typeof source === 'string' && (isSourcePDF || isFilePDF)) { // Every mounted PDF viewer is a full PDF.js document parse (its own worker + parsed document), so in a // carousel the memory cost scales with the number of PDF attachments — enough to OOM the WebContent // process on iOS Safari and reload the tab when several PDFs are added at once. Only mount the viewer @@ -274,7 +274,7 @@ function AttachmentView({ /> ); } - const encryptedSourceUrl = isAuthTokenRequired ? addEncryptedAuthTokenToURL(source as string, encryptedAuthToken) : (source as string); + const encryptedSourceUrl = isAuthTokenRequired ? addEncryptedAuthTokenToURL(source, encryptedAuthToken) : source; const onPDFLoadComplete = (path: string) => { const id = transaction?.transactionID ?? reportActionID; @@ -323,7 +323,8 @@ function AttachmentView({ const isImage = isFileImage || (!file?.name && isLocalSourceImage); - if (isImage) { + // ImageView renders URLs and static image assets; other image sources cannot be shown there. + if (isImage && (typeof source === 'string' || typeof source === 'number')) { if (imageError && (typeof fallbackSource === 'number' || typeof fallbackSource === 'function')) { return ( @@ -352,7 +353,7 @@ function AttachmentView({ ); } - let imageSource = imageError && fallbackSource ? (fallbackSource as string) : (cachedSource ?? (source as string)); + let imageSource = imageError && typeof fallbackSource === 'string' && fallbackSource ? fallbackSource : (cachedSource ?? source); if (isHighResolution) { if (!isUploaded) { diff --git a/src/components/ImageView/index.tsx b/src/components/ImageView/index.tsx index f50b78c89848..245bb196e9d4 100644 --- a/src/components/ImageView/index.tsx +++ b/src/components/ImageView/index.tsx @@ -159,7 +159,7 @@ function ImageView({isAuthTokenRequired = false, url, fileName, onError}: ImageV {/* eslint-disable-next-line react-native-a11y/has-valid-accessibility-ignores-invert-colors -- Custom Image wrapper does not support this prop. */} & { /** Whether source url requires authentication */ isAuthTokenRequired?: boolean; - /** URL to full-sized image */ - url: string; + /** URL to full-sized image, or a static image asset on native */ + url: string | number; fileName: string; diff --git a/src/components/Lightbox/index.tsx b/src/components/Lightbox/index.tsx index 049f61fbc523..b4c987a1cfaf 100644 --- a/src/components/Lightbox/index.tsx +++ b/src/components/Lightbox/index.tsx @@ -32,7 +32,7 @@ const FALLBACK_OFFSET = 2; const LOADING_CONTEXT = {context: 'Lightbox'}; -const cachedImageDimensions = new Map(); +const cachedImageDimensions = new Map(); function getImagePriority(isActive: boolean, isLightboxVisible: boolean) { if (isActive) { @@ -48,8 +48,8 @@ type LightboxProps = Pick & { /** Whether source url requires authentication */ isAuthTokenRequired?: boolean; - /** URI to full-sized attachment */ - uri: string; + /** URI to full-sized attachment, or a static image asset on native */ + uri: string | number; /** Triggers whenever the zoom scale changes */ onScaleChanged?: OnScaleChangedCallback; @@ -263,7 +263,7 @@ function Lightbox({attachmentID, isAuthTokenRequired = false, uri, onScaleChange {/* eslint-disable-next-line react-native-a11y/has-valid-accessibility-ignores-invert-colors -- Custom Image wrapper does not support this prop. */} {/* eslint-disable-next-line react-native-a11y/has-valid-accessibility-ignores-invert-colors -- Custom Image wrapper does not support this prop. */} (); + +jest.mock('@components/Image', () => (props: ImageProps) => { + mockImageProps(props); + return null; +}); +jest.mock('@libs/DeviceCapabilities', () => ({canUseTouchScreen: () => false})); + +// Jest prefers .native files, so load the web implementation by its file name. +const ImageView = jest.requireActual('../../src/components/ImageView/index.tsx').default; + +describe('ImageView source', () => { + beforeEach(() => { + mockImageProps.mockClear(); + }); + + it('passes a static image asset to the image unchanged', () => { + // Given a static image asset, which React Native represents as a number + const staticImage = 42; + + // When the image view renders it + render( + , + ); + + // Then the image gets the asset itself, because wrapping it as {uri: 42} cannot load + expect(mockImageProps).toHaveBeenLastCalledWith(expect.objectContaining({source: staticImage})); + }); + + it('wraps a URL in a uri source', () => { + // Given a remote image URL + const url = 'https://www.expensify.com/receipt.png'; + + // When the image view renders it + render( + , + ); + + // Then the image gets a uri source, as before + expect(mockImageProps).toHaveBeenLastCalledWith(expect.objectContaining({source: {uri: url}})); + }); +});