@@ -3,19 +3,71 @@ import { act } from "react";
33import { createRoot } from "react-dom/client" ;
44import { expect , it , vi } from "vite-plus/test" ;
55
6+ type HandlerProps = Record < string , ( ( event : unknown ) => void ) | undefined > ;
7+ type GestureHandlers = Record < string , ( event : unknown , manager ?: unknown ) => void > ;
8+ type Transform = [ { translateX : number } , { translateY : number } , { scale : number } ] ;
9+
610const harness = vi . hoisted ( ( ) => ( {
711 failImage : ( ) => { } ,
812 loadImage : ( _event : { source : { width : number ; height : number } } ) => { } ,
913 Passthrough : ( { children } : { children ?: unknown } ) => children ,
14+ pressable : { } as HandlerProps ,
15+ gestures : { } as Record < string , GestureHandlers > ,
16+ contentStyle : ( ) => ( { } ) as { transform ?: Transform } ,
1017} ) ) ;
1118
12- vi . mock ( "react-native" , ( ) => ( {
13- Pressable : harness . Passthrough ,
14- StyleSheet : { absoluteFill : { } } ,
15- View : ( { children, style } : { children ?: ReactNode ; style ?: unknown } ) => (
19+ vi . mock ( "react-native" , ( ) => {
20+ const View = ( { children, style } : { children ?: ReactNode ; style ?: unknown } ) => (
1621 < div data-style = { JSON . stringify ( style ) } > { children } </ div >
17- ) ,
18- } ) ) ;
22+ ) ;
23+ return {
24+ Platform : { OS : "ios" } ,
25+ Pressable : ( props : HandlerProps & { children ?: ReactNode ; style ?: unknown } ) => {
26+ harness . pressable = props ;
27+ return < View style = { props . style } > { props . children } </ View > ;
28+ } ,
29+ StyleSheet : { absoluteFill : { } } ,
30+ View,
31+ } ;
32+ } ) ;
33+ vi . mock ( "react-native-gesture-handler" , ( ) => {
34+ // Records each gesture's callbacks so a test can drive the worklets with native-shaped events.
35+ const builder = ( kind : string ) => ( ) => {
36+ const handlers : GestureHandlers = { } ;
37+ harness . gestures [ kind ] = handlers ;
38+ const gesture : object = new Proxy (
39+ { } ,
40+ {
41+ get : ( _target , name ) => ( callback : GestureHandlers [ string ] ) => {
42+ if ( typeof name === "string" && name . startsWith ( "on" ) ) handlers [ name ] = callback ;
43+ return gesture ;
44+ } ,
45+ } ,
46+ ) ;
47+ return gesture ;
48+ } ;
49+ return {
50+ Gesture : { Pinch : builder ( "pinch" ) , Pan : builder ( "pan" ) , Simultaneous : ( ) => ( { } ) } ,
51+ GestureDetector : ( { children } : { children ?: ReactNode } ) => children ,
52+ } ;
53+ } ) ;
54+ vi . mock ( "react-native-reanimated" , async ( ) => {
55+ const { useState } = await import ( "react" ) ;
56+ return {
57+ default : { View : ( { children } : { children ?: ReactNode } ) => < div > { children } </ div > } ,
58+ runOnJS : ( callback : ( ) => void ) => callback ,
59+ // Reads the UI-thread style on demand, as Reanimated would after a gesture frame.
60+ useAnimatedStyle : ( updater : ( ) => { transform ?: Transform } ) => {
61+ harness . contentStyle = updater ;
62+ return { } ;
63+ } ,
64+ useSharedValue : < T , > ( initial : T ) =>
65+ useState ( ( ) => {
66+ let value = initial ;
67+ return { get : ( ) => value , set : ( next : T ) => void ( value = next ) } ;
68+ } ) [ 0 ] ,
69+ } ;
70+ } ) ;
1971vi . mock ( "expo-image" , ( ) => ( {
2072 Image : ( props : {
2173 onError : ( ) => void ;
@@ -203,3 +255,146 @@ it("does not parse an obsolete SVG response after its last reader unmounts", asy
203255 registry . dispose ( ) ;
204256 }
205257} ) ;
258+
259+ const zoom = ( ) => {
260+ const [ { translateX } , { translateY } , { scale } ] = harness . contentStyle ( ) . transform ! ;
261+ // Clamping at fit can yield -0, which is the same position.
262+ return { scale, x : translateX + 0 , y : translateY + 0 } ;
263+ } ;
264+ const pinch = ( phase : "onStart" | "onUpdate" , focalX : number , focalY : number , scale : number ) =>
265+ act ( async ( ) => harness . gestures . pinch ! [ phase ] ! ( { focalX, focalY, scale, numberOfPointers : 2 } ) ) ;
266+ const endPinch = ( ) => act ( async ( ) => harness . gestures . pinch ! . onFinalize ! ( { } ) ) ;
267+ const layout = ( width : number , height : number ) =>
268+ act ( async ( ) => harness . pressable . onLayout ?.( { nativeEvent : { layout : { width, height } } } ) ) ;
269+ const markdownImage = ( sourceKey : string , onPressPreview : ( ) => void = ( ) => { } ) => (
270+ < MarkdownImageAvailableWidthContext value = { 345 } >
271+ < ThreadMarkdownImageView
272+ uri = { `https://example.test/${ sourceKey } .png` }
273+ sourceKey = { sourceKey }
274+ unavailable = { false }
275+ alt = { null }
276+ knownSize = { { width : 1000 , height : 585 } }
277+ onPressPreview = { onPressPreview }
278+ />
279+ </ MarkdownImageAvailableWidthContext >
280+ ) ;
281+
282+ it ( "does not open the preview when a touch on the inline image pinches or pans it" , async ( ) => {
283+ ( globalThis as { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
284+ const root = createRoot ( document . createElement ( "div" ) ) ;
285+ const onPressPreview = vi . fn ( ) ;
286+ const touch = ( fingers : number ) =>
287+ act ( async ( ) =>
288+ harness . pressable . onTouchStart ?.( {
289+ nativeEvent : { touches : Array . from ( { length : fingers } , ( ) => ( { } ) ) } ,
290+ } ) ,
291+ ) ;
292+ const release = ( ) =>
293+ act ( async ( ) => harness . pressable . onPress ?.( { nativeEvent : { identifier : 1 } } ) ) ;
294+ try {
295+ await act ( async ( ) => root . render ( markdownImage ( "hero" , onPressPreview ) ) ) ;
296+ await touch ( 1 ) ;
297+ await touch ( 2 ) ;
298+ await release ( ) ;
299+ expect ( onPressPreview ) . not . toHaveBeenCalled ( ) ;
300+
301+ await touch ( 1 ) ;
302+ await act ( async ( ) => harness . gestures . pan ! . onStart ! ( { translationX : 5 , translationY : 0 } ) ) ;
303+ await release ( ) ;
304+ expect ( onPressPreview ) . not . toHaveBeenCalled ( ) ;
305+
306+ await touch ( 1 ) ;
307+ await release ( ) ;
308+ expect ( onPressPreview ) . toHaveBeenCalledOnce ( ) ;
309+
310+ // VoiceOver activation has no touch, so an earlier pinch must not swallow it.
311+ await touch ( 2 ) ;
312+ await act ( async ( ) => harness . pressable . onPress ?.( { nativeEvent : { } } ) ) ;
313+ expect ( onPressPreview ) . toHaveBeenCalledTimes ( 2 ) ;
314+ } finally {
315+ await act ( async ( ) => root . unmount ( ) ) ;
316+ }
317+ } ) ;
318+
319+ it ( "zooms the inline image around the pinch and keeps it until the source or frame changes" , async ( ) => {
320+ ( globalThis as { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
321+ const root = createRoot ( document . createElement ( "div" ) ) ;
322+ const touchPan = ( ) => {
323+ const manager = { fail : vi . fn ( ) } ;
324+ harness . gestures . pan ! . onTouchesDown ! ( { } , manager ) ;
325+ return manager ;
326+ } ;
327+ try {
328+ await act ( async ( ) => root . render ( markdownImage ( "a" ) ) ) ;
329+ // The frame center is (172.5, 101); the pinch is off center.
330+ await layout ( 345 , 202 ) ;
331+ await pinch ( "onStart" , 300 , 150 , 1 ) ;
332+ await pinch ( "onUpdate" , 300 , 150 , 2 ) ;
333+ expect ( zoom ( ) ) . toEqual ( { scale : 2 , x : - 127.5 , y : - 49 } ) ;
334+ await pinch ( "onUpdate" , 280 , 140 , 2 ) ;
335+ expect ( zoom ( ) ) . toEqual ( { scale : 2 , x : - 147.5 , y : - 59 } ) ;
336+ await act ( async ( ) =>
337+ harness . gestures . pinch ! . onUpdate ! ( { focalX : 90 , focalY : 90 , scale : 2 , numberOfPointers : 1 } ) ,
338+ ) ;
339+ expect ( zoom ( ) ) . toEqual ( { scale : 2 , x : - 147.5 , y : - 59 } ) ;
340+ await endPinch ( ) ;
341+
342+ await act ( async ( ) => harness . loadImage ( { source : { width : 1000 , height : 585 } } ) ) ;
343+ await act ( async ( ) => root . render ( markdownImage ( "a" ) ) ) ;
344+ await layout ( 345 , 202 ) ;
345+ expect ( zoom ( ) ) . toEqual ( { scale : 2 , x : - 147.5 , y : - 59 } ) ;
346+
347+ await pinch ( "onStart" , 100 , 50 , 1 ) ;
348+ await pinch ( "onUpdate" , 100 , 50 , 1.5 ) ;
349+ expect ( zoom ( ) ) . toEqual ( { scale : 3 , x : - 185 , y : - 63 } ) ;
350+ await pinch ( "onUpdate" , 100 , 50 , 10 ) ;
351+ expect ( zoom ( ) . scale ) . toBe ( 6 ) ;
352+ await endPinch ( ) ;
353+
354+ const zoomedPan = touchPan ( ) ;
355+ await act ( async ( ) => harness . gestures . pan ! . onStart ! ( { translationX : 5 , translationY : 0 } ) ) ;
356+ await act ( async ( ) =>
357+ harness . gestures . pan ! . onUpdate ! ( { translationX : 5000 , translationY : - 5000 } ) ,
358+ ) ;
359+ expect ( zoomedPan . fail ) . not . toHaveBeenCalled ( ) ;
360+ expect ( zoom ( ) ) . toEqual ( { scale : 6 , x : 862.5 , y : - 505 } ) ;
361+
362+ await pinch ( "onStart" , 172 , 100 , 1 ) ;
363+ await pinch ( "onUpdate" , 172 , 100 , 0.01 ) ;
364+ await endPinch ( ) ;
365+ expect ( zoom ( ) ) . toEqual ( { scale : 1 , x : 0 , y : 0 } ) ;
366+ expect ( touchPan ( ) . fail ) . toHaveBeenCalledOnce ( ) ;
367+
368+ await pinch ( "onStart" , 300 , 150 , 1 ) ;
369+ await pinch ( "onUpdate" , 300 , 150 , 2 ) ;
370+ await endPinch ( ) ;
371+ await layout ( 300 , 175 ) ;
372+ expect ( zoom ( ) ) . toEqual ( { scale : 1 , x : 0 , y : 0 } ) ;
373+
374+ await pinch ( "onStart" , 300 , 150 , 1 ) ;
375+ await pinch ( "onUpdate" , 300 , 150 , 2 ) ;
376+ await endPinch ( ) ;
377+ await act ( async ( ) => root . render ( markdownImage ( "b" ) ) ) ;
378+ expect ( zoom ( ) ) . toEqual ( { scale : 1 , x : 0 , y : 0 } ) ;
379+
380+ for ( const panFirst of [ true , false ] ) {
381+ await act ( async ( ) => root . render ( markdownImage ( `handoff-${ panFirst } ` ) ) ) ;
382+ await layout ( 300 , 200 ) ;
383+ await pinch ( "onStart" , 150 , 100 , 1 ) ;
384+ await pinch ( "onUpdate" , 150 , 100 , 2 ) ;
385+ await endPinch ( ) ;
386+ await act ( async ( ) => harness . gestures . pan ! . onStart ! ( { translationX : 0 , translationY : 0 } ) ) ;
387+ await pinch ( "onStart" , 150 , 100 , 1 ) ;
388+ const panUpdate = ( ) =>
389+ act ( async ( ) => harness . gestures . pan ! . onUpdate ! ( { translationX : 10 , translationY : 0 } ) ) ;
390+ if ( panFirst ) await panUpdate ( ) ;
391+ await pinch ( "onUpdate" , 150 , 100 , 1.5 ) ;
392+ if ( ! panFirst ) await panUpdate ( ) ;
393+ await endPinch ( ) ;
394+ await act ( async ( ) => harness . gestures . pan ! . onUpdate ! ( { translationX : 20 , translationY : 0 } ) ) ;
395+ expect ( zoom ( ) ) . toEqual ( { scale : 3 , x : 10 , y : 0 } ) ;
396+ }
397+ } finally {
398+ await act ( async ( ) => root . unmount ( ) ) ;
399+ }
400+ } ) ;
0 commit comments