@@ -24,6 +24,12 @@ interface RenderApp {
2424 pinch : ( options : { value : number ; center ?: [ number , number ] } ) => void ;
2525}
2626
27+ const waitForPreviousActionToEnd = ( ) => {
28+ // Synchronous await for 10ms to wait for the previous event to finish (pinching or touching)
29+ const startTime = Date . now ( ) ;
30+ while ( Date . now ( ) - startTime < 10 ) { }
31+ } ;
32+
2733function getPinchTouches (
2834 content : HTMLElement ,
2935 center : [ number , number ] ,
@@ -95,7 +101,7 @@ export const renderApp = ({
95101 { ...{ contentHeight, contentWidth, wrapperHeight, wrapperWidth } }
96102 /> ,
97103 ) ;
98- // // controls buttons
104+ // controls buttons
99105 const zoomInBtn = screen . getByTestId ( "zoom-in" ) ;
100106 const zoomOutBtn = screen . getByTestId ( "zoom-out" ) ;
101107 const resetBtn = screen . getByTestId ( "reset" ) ;
@@ -147,6 +153,8 @@ export const renderApp = ({
147153 const { value, center = [ 0 , 0 ] } = options ;
148154 if ( ! ref . current ) throw new Error ( "ref.current is null" ) ;
149155
156+ waitForPreviousActionToEnd ( ) ;
157+
150158 const isZoomIn = ref . current . instance . state . scale < value ;
151159 const from = isZoomIn ? 1 : 2 ;
152160 const step = 0.1 ;
@@ -169,9 +177,9 @@ export const renderApp = ({
169177 const scaleDifference = Math . abs (
170178 ref . current . instance . state . scale - value ,
171179 ) ;
172- const isNearScale = scaleDifference < 0.5 ;
180+ const isNearScale = scaleDifference < 0.05 ;
173181
174- const newStep = isNearScale ? step / 2 : step ;
182+ const newStep = isNearScale ? step / 6 : step ;
175183
176184 pinchValue = pinchValue + newStep ;
177185 touches = getPinchTouches ( content , center , pinchValue , from ) ;
@@ -184,7 +192,9 @@ export const renderApp = ({
184192 }
185193 }
186194
187- fireEvent . touchEnd ( content ) ;
195+ fireEvent . touchEnd ( content , {
196+ touches,
197+ } ) ;
188198 } ;
189199
190200 const pan : RenderApp [ "pan" ] = ( { x, y } ) => {
@@ -196,17 +206,18 @@ export const renderApp = ({
196206 } ;
197207
198208 const touchPan : RenderApp [ "touchPan" ] = ( { x, y } ) => {
199- const touches = [
200- {
201- pageX : 0 ,
202- pageY : 0 ,
203- clientX : 0 ,
204- clientY : 0 ,
205- target : content ,
206- } ,
207- ] ;
209+ waitForPreviousActionToEnd ( ) ;
210+
208211 fireEvent . touchStart ( content , {
209- touches,
212+ touches : [
213+ {
214+ pageX : 0 ,
215+ pageY : 0 ,
216+ clientX : 0 ,
217+ clientY : 0 ,
218+ target : content ,
219+ } ,
220+ ] ,
210221 } ) ;
211222 fireEvent . touchMove ( content , {
212223 touches : [
@@ -219,7 +230,17 @@ export const renderApp = ({
219230 } ,
220231 ] ,
221232 } ) ;
222- fireEvent . touchEnd ( content ) ;
233+ fireEvent . touchEnd ( content , {
234+ touches : [
235+ {
236+ pageX : x ,
237+ pageY : y ,
238+ clientX : x ,
239+ clientY : y ,
240+ target : content ,
241+ } ,
242+ ] ,
243+ } ) ;
223244 } ;
224245
225246 return {
0 commit comments