@@ -8,10 +8,10 @@ import {
8
8
} from 'react-native' ;
9
9
import act from './act' ;
10
10
import { isHostElement } from './helpers/component-tree' ;
11
- import { isHostTextInput } from './helpers/host-component-names' ;
11
+ import { isHostScrollView , isHostTextInput } from './helpers/host-component-names' ;
12
12
import { isPointerEventEnabled } from './helpers/pointer-events' ;
13
13
import { isTextInputEditable } from './helpers/text-input' ;
14
- import { StringWithAutocomplete } from './types' ;
14
+ import { Point , StringWithAutocomplete } from './types' ;
15
15
import { nativeState } from './native-state' ;
16
16
17
17
type EventHandler = ( ...args : unknown [ ] ) => unknown ;
@@ -147,6 +147,14 @@ fireEvent.scroll = (element: ReactTestInstance, ...data: unknown[]) =>
147
147
148
148
export default fireEvent ;
149
149
150
+ const scrollEventNames = new Set ( [
151
+ 'scroll' ,
152
+ 'scrollBeginDrag' ,
153
+ 'scrollEndDrag' ,
154
+ 'momentumScrollBegin' ,
155
+ 'momentumScrollEnd' ,
156
+ ] ) ;
157
+
150
158
function setNativeStateIfNeeded ( element : ReactTestInstance , eventName : string , value : unknown ) {
151
159
if (
152
160
eventName === 'changeText' &&
@@ -156,4 +164,30 @@ function setNativeStateIfNeeded(element: ReactTestInstance, eventName: string, v
156
164
) {
157
165
nativeState . valueForElement . set ( element , value ) ;
158
166
}
167
+
168
+ if ( scrollEventNames . has ( eventName ) && isHostScrollView ( element ) ) {
169
+ const contentOffset = tryGetContentOffset ( value ) ;
170
+ if ( contentOffset ) {
171
+ nativeState . contentOffsetForElement . set ( element , contentOffset ) ;
172
+ }
173
+ }
174
+ }
175
+
176
+ function tryGetContentOffset ( value : unknown ) : Point | null {
177
+ try {
178
+ // @ts -expect-error: try to extract contentOffset from the event value
179
+ const contentOffset = value ?. nativeEvent ?. contentOffset ;
180
+ const x = contentOffset ?. x ;
181
+ const y = contentOffset ?. y ;
182
+ if ( typeof x === 'number' || typeof y === 'number' ) {
183
+ return {
184
+ x : Number . isFinite ( x ) ? x : 0 ,
185
+ y : Number . isFinite ( y ) ? y : 0 ,
186
+ } ;
187
+ }
188
+ } catch {
189
+ // Do nothing
190
+ }
191
+
192
+ return null ;
159
193
}
0 commit comments