Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to pass contentSize and layoutMeasurement when calling scrollTo #1543

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/user-event/event-builder/scroll-view.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ScrollToOptions } from '../scroll';

/**
* Experimental values:
* - iOS: `{"contentInset": {"bottom": 0, "left": 0, "right": 0, "top": 0}, "contentOffset": {"x": 0, "y": 5.333333333333333}, "contentSize": {"height": 1676.6666259765625, "width": 390}, "layoutMeasurement": {"height": 753, "width": 390}, "zoomScale": 1}`
Expand All @@ -13,15 +15,21 @@ export interface ContentOffset {
}

export const ScrollViewEventBuilder = {
scroll: (offset: ContentOffset = { y: 0, x: 0 }) => {
scroll: (
offset: ContentOffset = { y: 0, x: 0 },
options?: ScrollToOptions
) => {
return {
nativeEvent: {
contentInset: { bottom: 0, left: 0, right: 0, top: 0 },
contentOffset: { y: offset.y, x: offset.x },
contentSize: { height: 0, width: 0 },
contentSize: {
height: options?.contentSize?.height ?? 0,
width: options?.contentSize?.width ?? 0,
},
layoutMeasurement: {
height: 0,
width: 0,
height: options?.layoutMeasurement?.height ?? 0,
width: options?.layoutMeasurement?.width ?? 0,
},
responderIgnoreScroll: true,
target: 0,
Expand Down
53 changes: 41 additions & 12 deletions src/user-event/scroll/scroll-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ import { isHostScrollView } from '../../helpers/host-component-names';
import { pick } from '../../helpers/object';
import { dispatchEvent, wait } from '../utils';
import { ContentOffset } from '../event-builder/scroll-view';
import fireEvent from '../../fire-event';
import {
createScrollSteps,
inertialInterpolator,
linearInterpolator,
} from './utils';
import { getElementScrollOffset, setElementScrollOffset } from './state';

export interface VerticalScrollToOptions {
interface CommonScrollToOptions {
contentSize?: {
height: number;
width: number;
};

layoutMeasurement?: {
height: number;
width: number;
};
}

export interface VerticalScrollToOptions extends CommonScrollToOptions {
y: number;
momentumY?: number;

Expand All @@ -23,7 +36,7 @@ export interface VerticalScrollToOptions {
momentumX?: never;
}

export interface HorizontalScrollToOptions {
export interface HorizontalScrollToOptions extends CommonScrollToOptions {
x: number;
momentumX?: number;

Expand All @@ -50,31 +63,46 @@ export async function scrollTo(

ensureScrollViewDirection(element, options);

emitContentSizeChangeEvent(element, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ The sequencing of this event matches our experiments with RN runtime:
https://github.com/callstack/react-native-testing-library/wiki/ScrollView-Events


const initialPosition = getElementScrollOffset(element);
const dragSteps = createScrollSteps(
{ y: options.y, x: options.x },
initialPosition,
linearInterpolator
);
await emitDragScrollEvents(this.config, element, dragSteps);
await emitDragScrollEvents(this.config, element, dragSteps, options);

const momentumStart = dragSteps.at(-1) ?? initialPosition;
const momentumSteps = createScrollSteps(
{ y: options.momentumY, x: options.momentumX },
momentumStart,
inertialInterpolator
);
await emitMomentumScrollEvents(this.config, element, momentumSteps);
await emitMomentumScrollEvents(this.config, element, momentumSteps, options);

const finalPosition =
momentumSteps.at(-1) ?? dragSteps.at(-1) ?? initialPosition;
setElementScrollOffset(element, finalPosition);
}

function emitContentSizeChangeEvent(
element: ReactTestInstance,
options: ScrollToOptions
) {
fireEvent(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with dispatchEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dispatchEvent has a different signature than fireEvent. It expects event to be its last argument and passes that directly to the handler, while fireEvent accepts variadic arguments and spreads them when passing to the handler.

contentSizeChange handler expects width and height as separate arguments: https://github.com/facebook/react-native/blob/d45a01d6d15530e356065704adb869b1cbbb2576/packages/virtualized-lists/Lists/VirtualizedList.js#L1574

Should I also change the signature of dispatchEvent to accept variadic arguments, or is fireEvent fine in that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I'll add some dispatch function for such cases.

element,
'contentSizeChange',
options.contentSize?.width ?? 0,
options.contentSize?.height ?? 0
);
}

async function emitDragScrollEvents(
config: UserEventConfig,
element: ReactTestInstance,
scrollSteps: ContentOffset[]
scrollSteps: ContentOffset[],
scrollOptions: ScrollToOptions
) {
if (scrollSteps.length === 0) {
return;
Expand All @@ -84,7 +112,7 @@ async function emitDragScrollEvents(
dispatchEvent(
element,
'scrollBeginDrag',
EventBuilder.ScrollView.scroll(scrollSteps[0])
EventBuilder.ScrollView.scroll(scrollSteps[0], scrollOptions)
);

// Note: experimentally, in case of drag scroll the last scroll step
Expand All @@ -95,7 +123,7 @@ async function emitDragScrollEvents(
dispatchEvent(
element,
'scroll',
EventBuilder.ScrollView.scroll(scrollSteps[i])
EventBuilder.ScrollView.scroll(scrollSteps[i], scrollOptions)
);
}

Expand All @@ -104,14 +132,15 @@ async function emitDragScrollEvents(
dispatchEvent(
element,
'scrollEndDrag',
EventBuilder.ScrollView.scroll(lastStep)
EventBuilder.ScrollView.scroll(lastStep, scrollOptions)
);
}

async function emitMomentumScrollEvents(
config: UserEventConfig,
element: ReactTestInstance,
scrollSteps: ContentOffset[]
scrollSteps: ContentOffset[],
scrollOptions: ScrollToOptions
) {
if (scrollSteps.length === 0) {
return;
Expand All @@ -121,7 +150,7 @@ async function emitMomentumScrollEvents(
dispatchEvent(
element,
'momentumScrollBegin',
EventBuilder.ScrollView.scroll(scrollSteps[0])
EventBuilder.ScrollView.scroll(scrollSteps[0], scrollOptions)
);

// Note: experimentally, in case of momentum scroll the last scroll step
Expand All @@ -132,7 +161,7 @@ async function emitMomentumScrollEvents(
dispatchEvent(
element,
'scroll',
EventBuilder.ScrollView.scroll(scrollSteps[i])
EventBuilder.ScrollView.scroll(scrollSteps[i], scrollOptions)
);
}

Expand All @@ -141,7 +170,7 @@ async function emitMomentumScrollEvents(
dispatchEvent(
element,
'momentumScrollEnd',
EventBuilder.ScrollView.scroll(lastStep)
EventBuilder.ScrollView.scroll(lastStep, scrollOptions)
);
}

Expand Down
6 changes: 6 additions & 0 deletions website/docs/UserEvent.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ scrollTo(
options: {
y: number,
momentumY?: number,
contentSize?: { width: number, height: number },
layoutMeasurement?: { width: number, height: number },
} | {
x: number,
momentumX?: number,
contentSize?: { width: number, height: number },
layoutMeasurement?: { width: number, height: number },
}
```

Expand All @@ -221,6 +225,8 @@ This helper simulates user scrolling a host `ScrollView` element.

This function supports only host `ScrollView` elements, passing other element types will result in error. Note that `FlatList` is accepted as it renders to a host `ScrolLView` element, however in the current iteration we focus only on base `ScrollView` only features.

If you want to simulate scrolling a `FlatList` or any other `VirtualizedList`, you should also pass `contentSize` and `layoutMeasurement` options, to run underlying logic that updates the currently visible window.

Scroll interaction should match `ScrollView` element direction. For vertical scroll view (default or explicit `horizontal={false}`) you should pass only `y` (and optionally also `momentumY`) option, for horizontal scroll view (`horizontal={true}`) you should pass only `x` (and optionally `momentumX`) option.

Each scroll interaction consists of a mandatory drag scroll part which simulates user dragging the scroll view with his finger (`y` or `x` option). This may optionally be followed by a momentum scroll movement which simulates the inertial movement of scroll view content after the user lifts his finger up (`momentumY` or `momentumX` options).
Expand Down