Skip to content

Commit

Permalink
refactor: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Dec 15, 2023
1 parent 711722e commit c1f58dd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import { FlatList, ScrollViewProps, Text, View } from 'react-native';
import { EventEntry, createEventLogger } from '../../../test-utils';
import { render, screen } from '../../..';
import { userEvent } from '../..';
import '../../../matchers/extend-expect';
import { EventEntry, createEventLogger } from '../../../test-utils';
import { userEvent } from '../..';

const data = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];

Expand Down
19 changes: 6 additions & 13 deletions src/user-event/scroll/scroll-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export async function scrollTo(

ensureScrollViewDirection(element, options);

emitContentSizeChangeEvent(element, options);
dispatchEvent(
element,
'contentSizeChange',
options.contentSize?.width ?? 0,
options.contentSize?.height ?? 0
);

const initialPosition = getElementScrollOffset(element);
const dragSteps = createScrollSteps(
Expand All @@ -84,18 +89,6 @@ export async function scrollTo(
setElementScrollOffset(element, finalPosition);
}

function emitContentSizeChangeEvent(
element: ReactTestInstance,
options: ScrollToOptions
) {
dispatchEvent(
element,
'contentSizeChange',
options.contentSize?.width ?? 0,
options.contentSize?.height ?? 0
);
}

async function emitDragScrollEvents(
config: UserEventConfig,
element: ReactTestInstance,
Expand Down
12 changes: 7 additions & 5 deletions website/docs/UserEvent.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,24 +223,26 @@ await user.scrollTo(scrollView, { y: 100, momentumY: 200 });
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.
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.
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).
### Options {#type-options}
### Options {#scroll-to-options}
- `y` - target vertical drag scroll position
- `x` - target horizontal drag scroll position
- `momentumY` - target vertical momentum scroll position
- `momentumX` - target horizontal momentum scroll position
- `contentSize` - passed to `ScrollView` events and enabling `FlatList` updates
- `layoutMeasurement` - passed to `ScrollView` events and enabling `FlatList` updates
User Event will generate a number of intermediate scroll steps to simulate user scroll interaction. You should not rely on exact number or values of these scrolls steps as they might be change in the future version.
This function will remember where the last scroll ended, so subsequent scroll interaction will starts from that positition. The initial scroll position will be assumed to be `{ y: 0, x: 0 }`.
This function will remember where the last scroll ended, so subsequent scroll interaction will starts from that position. The initial scroll position will be assumed to be `{ y: 0, x: 0 }`.
In order to simulate a `FlatList` (and other controls based on `VirtualizedList`) scrolling, you should pass `contentSize` and `layoutMeasurement` options, which enable the underlying logic to update the currently visible window.
### Sequence of events
Expand Down

0 comments on commit c1f58dd

Please sign in to comment.