diff --git a/mobile/src/components/MobileRepoIcon.test.tsx b/mobile/src/components/MobileRepoIcon.test.tsx new file mode 100644 index 00000000000..09355287677 --- /dev/null +++ b/mobile/src/components/MobileRepoIcon.test.tsx @@ -0,0 +1,45 @@ +import { Folder } from 'lucide-react-native' +import { createElement } from 'react' +import { Image } from 'react-native' +import { act, create, type ReactTestRenderer } from 'react-test-renderer' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { MobileRepoIcon } from './MobileRepoIcon' + +vi.mock('react-native', () => ({ + Image: 'Image', + StyleSheet: { create: (styles: T) => styles }, + Text: 'Text', + View: 'View' +})) +// Each icon renders as an element named after it. +vi.mock( + 'lucide-react-native', + () => new Proxy({}, { has: () => true, get: (_, name) => String(name) }) +) +vi.mock('../theme/use-mobile-color-scheme', () => ({ useMobileColorScheme: () => 'dark' })) + +let renderer: ReactTestRenderer | null = null +afterEach(() => { + act(() => renderer?.unmount()) + renderer = null +}) + +function imageIcon(src: string) { + return createElement(MobileRepoIcon, { repoIcon: { type: 'image', src, label: 'owner/repo' } }) +} + +describe('MobileRepoIcon', () => { + it('shows the folder after an image fails, and tries a changed image again', () => { + act(() => { + renderer = create(imageIcon('https://github.com/owner.png?size=64')) + }) + act(() => renderer!.root.findByType(Image).props.onError()) + expect(renderer!.root.findAllByType(Image)).toHaveLength(0) + expect(renderer!.root.findAllByType(Folder)).toHaveLength(1) + + act(() => renderer!.update(imageIcon('data:image/png;base64,AAAA'))) + expect(renderer!.root.findByType(Image).props.source).toEqual({ + uri: 'data:image/png;base64,AAAA' + }) + }) +}) diff --git a/mobile/src/components/MobileRepoIcon.tsx b/mobile/src/components/MobileRepoIcon.tsx index 512272a1962..09d1dc1a5e0 100644 --- a/mobile/src/components/MobileRepoIcon.tsx +++ b/mobile/src/components/MobileRepoIcon.tsx @@ -22,6 +22,7 @@ import { SquareTerminal, Wrench } from 'lucide-react-native' +import { useState } from 'react' import { Image, StyleSheet, Text, View } from 'react-native' import type { MobileRenderableRepoIcon } from '../host-screen/host-screen-reply-schema' import { colors, radii } from '../theme/mobile-theme' @@ -61,12 +62,16 @@ type Props = { // Render desktop-style custom image/emoji/lucide with Folder fallback, never bare project dot. export function MobileRepoIcon({ repoIcon, size = 14, color = colors.textSecondary }: Props) { useMobileColorScheme() - if (repoIcon?.type === 'image') { + // Keyed by src so a changed icon gets a fresh try; the page shell refuses every https load. + const [failedSrc, setFailedSrc] = useState(null) + if (repoIcon?.type === 'image' && repoIcon.src !== failedSrc) { + const { src } = repoIcon return ( setFailedSrc(src)} /> ) }