Skip to content

Commit e3c7718

Browse files
committed
refactor: move scroll state into native state
1 parent e6497d2 commit e3c7718

File tree

6 files changed

+17
-25
lines changed

6 files changed

+17
-25
lines changed

src/native-state.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ReactTestInstance } from 'react-test-renderer';
2+
import { ContentOffset } from './types';
23

34
/**
45
* Simulated native state for unmanaged controls.
@@ -7,13 +8,15 @@ import { ReactTestInstance } from 'react-test-renderer';
78
*/
89
export type NativeState = {
910
elementValues: WeakMap<ReactTestInstance, string>;
11+
scrollPositions: WeakMap<ReactTestInstance, ContentOffset>;
1012
};
1113

1214
export let nativeState: NativeState | null = null;
1315

1416
export function initNativeState(): void {
1517
nativeState = {
1618
elementValues: new WeakMap(),
19+
scrollPositions: new WeakMap(),
1720
};
1821
}
1922

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* Scroll position of a scrollable element.
3+
*/
4+
export interface ContentOffset {
5+
y: number;
6+
x: number;
7+
}
8+
19
// TS autocomplete trick
210
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
311
export type StringWithAutocomplete<T> = T | (string & {});

src/user-event/event-builder/scroll-view.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1+
import { ContentOffset } from '../../types';
12
import { baseSyntheticEvent } from './base';
23

3-
/**
4-
* Scroll position of a scrollable element.
5-
*/
6-
export interface ContentOffset {
7-
y: number;
8-
x: number;
9-
}
10-
114
/**
125
* Other options for constructing a scroll event.
136
*/

src/user-event/scroll/scroll-to.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { EventBuilder } from '../event-builder';
55
import { ErrorWithStack } from '../../helpers/errors';
66
import { isHostScrollView } from '../../helpers/host-component-names';
77
import { pick } from '../../helpers/object';
8-
import { ContentOffset } from '../event-builder/scroll-view';
8+
import { nativeState } from '../../native-state';
9+
import { ContentOffset } from '../../types';
910
import { dispatchEvent, wait } from '../utils';
1011
import { createScrollSteps, inertialInterpolator, linearInterpolator } from './utils';
11-
import { getElementScrollOffset, setElementScrollOffset } from './state';
1212

1313
interface CommonScrollToOptions {
1414
contentSize?: {
@@ -62,7 +62,7 @@ export async function scrollTo(
6262
options.contentSize?.height ?? 0,
6363
);
6464

65-
const initialPosition = getElementScrollOffset(element);
65+
const initialPosition = nativeState?.scrollPositions.get(element) ?? { x: 0, y: 0 };
6666
const dragSteps = createScrollSteps(
6767
{ y: options.y, x: options.x },
6868
initialPosition,
@@ -79,7 +79,7 @@ export async function scrollTo(
7979
await emitMomentumScrollEvents(this.config, element, momentumSteps, options);
8080

8181
const finalPosition = momentumSteps.at(-1) ?? dragSteps.at(-1) ?? initialPosition;
82-
setElementScrollOffset(element, finalPosition);
82+
nativeState?.scrollPositions.set(element, finalPosition);
8383
}
8484

8585
async function emitDragScrollEvents(

src/user-event/scroll/state.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/user-event/scroll/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContentOffset } from '../event-builder/scroll-view';
1+
import { ContentOffset } from '../../types';
22

33
const DEFAULT_STEPS_COUNT = 5;
44

0 commit comments

Comments
 (0)