@@ -5,17 +5,24 @@ import { expect, it, vi } from "vite-plus/test";
55
66const harness = vi . hoisted ( ( ) => ( {
77 failImage : ( ) => { } ,
8+ loadImage : ( _event : { source : { width : number ; height : number } } ) => { } ,
89 Passthrough : ( { children } : { children ?: unknown } ) => children ,
910} ) ) ;
1011
1112vi . mock ( "react-native" , ( ) => ( {
1213 Pressable : harness . Passthrough ,
1314 StyleSheet : { absoluteFill : { } } ,
14- View : harness . Passthrough ,
15+ View : ( { children, style } : { children ?: ReactNode ; style ?: unknown } ) => (
16+ < div data-style = { JSON . stringify ( style ) } > { children } </ div >
17+ ) ,
1518} ) ) ;
1619vi . mock ( "expo-image" , ( ) => ( {
17- Image : ( props : { onError : ( ) => void } ) => {
20+ Image : ( props : {
21+ onError : ( ) => void ;
22+ onLoad : ( event : { source : { width : number ; height : number } } ) => void ;
23+ } ) => {
1824 harness . failImage = props . onError ;
25+ harness . loadImage = props . onLoad ;
1926 return < img alt = "" /> ;
2027 } ,
2128} ) ) ;
@@ -41,7 +48,43 @@ import { AtomRegistry } from "effect/reactivity";
4148import { fetch } from "expo/fetch" ;
4249import { parse } from "react-native-svg" ;
4350
44- import { ThreadMarkdownImageView } from "./ThreadMarkdownImage" ;
51+ import { MarkdownImageAvailableWidthContext , ThreadMarkdownImageView } from "./ThreadMarkdownImage" ;
52+
53+ it ( "keeps the frame size when the same image reports a different decoded width" , async ( ) => {
54+ ( globalThis as { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
55+ const container = document . createElement ( "div" ) ;
56+ const root = createRoot ( container ) ;
57+ const frameSize = ( ) =>
58+ [ ...container . querySelectorAll < HTMLElement > ( "div[data-style]" ) ]
59+ . map ( ( element ) => JSON . parse ( element . dataset . style ?? "null" ) as Record < string , unknown > )
60+ . find ( ( style ) => typeof style ?. width === "number" ) ;
61+ try {
62+ await act ( async ( ) =>
63+ root . render (
64+ < MarkdownImageAvailableWidthContext value = { 345 } >
65+ < ThreadMarkdownImageView
66+ uri = "https://example.test/hero.png"
67+ sourceKey = "workspace:hero.png"
68+ unavailable = { false }
69+ alt = { null }
70+ knownSize = { { width : 1000 , height : 585 } }
71+ onPressPreview = { ( ) => { } }
72+ />
73+ </ MarkdownImageAvailableWidthContext > ,
74+ ) ,
75+ ) ;
76+ // Android reports the bitmap downsampled for the current frame, so the same file
77+ // alternates between widths (969 and 970 of 1000) as the frame changes size.
78+ await act ( async ( ) => harness . loadImage ( { source : { width : 970 , height : 567 } } ) ) ;
79+ const first = frameSize ( ) ;
80+ expect ( first ) . toBeDefined ( ) ;
81+
82+ await act ( async ( ) => harness . loadImage ( { source : { width : 969 , height : 567 } } ) ) ;
83+ expect ( frameSize ( ) ) . toEqual ( first ) ;
84+ } finally {
85+ await act ( async ( ) => root . unmount ( ) ) ;
86+ }
87+ } ) ;
4588
4689it ( "retries a failed image once its URI comes back through null" , async ( ) => {
4790 ( globalThis as { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
0 commit comments