Skip to content

Commit 687d07a

Browse files
committed
refactor: simplify types
1 parent e3c7718 commit 687d07a

File tree

7 files changed

+29
-37
lines changed

7 files changed

+29
-37
lines changed

src/native-state.ts

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

44
/**
55
* Simulated native state for unmanaged controls.
@@ -8,7 +8,7 @@ import { ContentOffset } from './types';
88
*/
99
export type NativeState = {
1010
elementValues: WeakMap<ReactTestInstance, string>;
11-
scrollPositions: WeakMap<ReactTestInstance, ContentOffset>;
11+
scrollPositions: WeakMap<ReactTestInstance, Point>;
1212
};
1313

1414
export let nativeState: NativeState | null = null;

src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
/**
2-
* Scroll position of a scrollable element.
2+
* Location of an element.
33
*/
4-
export interface ContentOffset {
4+
export interface Point {
55
y: number;
66
x: number;
77
}
88

9+
/**
10+
* Size of an element.
11+
*/
12+
export interface Size {
13+
height: number;
14+
width: number;
15+
}
16+
917
// TS autocomplete trick
1018
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
1119
export type StringWithAutocomplete<T> = T | (string & {});

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import { ContentOffset } from '../../types';
1+
import { Point, Size } from '../../types';
22
import { baseSyntheticEvent } from './base';
33

44
/**
55
* Other options for constructing a scroll event.
66
*/
77
export type ScrollEventOptions = {
8-
contentSize?: {
9-
height: number;
10-
width: number;
11-
};
12-
layoutMeasurement?: {
13-
height: number;
14-
width: number;
15-
};
8+
contentSize?: Size;
9+
layoutMeasurement?: Size;
1610
};
1711

1812
/**
@@ -21,7 +15,7 @@ export type ScrollEventOptions = {
2115
* - Android: `{"contentInset": {"bottom": 0, "left": 0, "right": 0, "top": 0}, "contentOffset": {"x": 0, "y": 31.619047164916992}, "contentSize": {"height": 1624.761962890625, "width": 411.4285583496094}, "layoutMeasurement": {"height": 785.5238037109375, "width": 411.4285583496094}, "responderIgnoreScroll": true, "target": 139, "velocity": {"x": -1.3633992671966553, "y": -1.3633992671966553}}`
2216
*/
2317
export const ScrollViewEventBuilder = {
24-
scroll: (offset: ContentOffset = { y: 0, x: 0 }, options?: ScrollEventOptions) => {
18+
scroll: (offset: Point = { y: 0, x: 0 }, options?: ScrollEventOptions) => {
2519
return {
2620
...baseSyntheticEvent(),
2721
nativeEvent: {

src/user-event/event-builder/text-input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContentSize } from '../utils/content-size';
1+
import { Size } from '../../types';
22
import { TextRange } from '../utils/text-range';
33
import { baseSyntheticEvent } from './base';
44

@@ -68,7 +68,7 @@ export const TextInputEventBuilder = {
6868
* - iOS: `{"contentSize": {"height": 21.666666666666668, "width": 11.666666666666666}, "target": 75}`
6969
* - Android: `{"contentSize": {"height": 61.45454406738281, "width": 352.7272644042969}, "target": 53}`
7070
*/
71-
contentSizeChange: ({ width, height }: ContentSize) => {
71+
contentSizeChange: ({ width, height }: Size) => {
7272
return {
7373
...baseSyntheticEvent(),
7474
nativeEvent: { contentSize: { width, height }, target: 0 },

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@ import { ErrorWithStack } from '../../helpers/errors';
66
import { isHostScrollView } from '../../helpers/host-component-names';
77
import { pick } from '../../helpers/object';
88
import { nativeState } from '../../native-state';
9-
import { ContentOffset } from '../../types';
9+
import { Point, Size } from '../../types';
1010
import { dispatchEvent, wait } from '../utils';
1111
import { createScrollSteps, inertialInterpolator, linearInterpolator } from './utils';
1212

1313
interface CommonScrollToOptions {
14-
contentSize?: {
15-
height: number;
16-
width: number;
17-
};
18-
layoutMeasurement?: {
19-
height: number;
20-
width: number;
21-
};
14+
contentSize?: Size;
15+
layoutMeasurement?: Size;
2216
}
2317

2418
export interface VerticalScrollToOptions extends CommonScrollToOptions {
@@ -85,7 +79,7 @@ export async function scrollTo(
8579
async function emitDragScrollEvents(
8680
config: UserEventConfig,
8781
element: ReactTestInstance,
88-
scrollSteps: ContentOffset[],
82+
scrollSteps: Point[],
8983
scrollOptions: ScrollToOptions,
9084
) {
9185
if (scrollSteps.length === 0) {
@@ -115,7 +109,7 @@ async function emitDragScrollEvents(
115109
async function emitMomentumScrollEvents(
116110
config: UserEventConfig,
117111
element: ReactTestInstance,
118-
scrollSteps: ContentOffset[],
112+
scrollSteps: Point[],
119113
scrollOptions: ScrollToOptions,
120114
) {
121115
if (scrollSteps.length === 0) {

src/user-event/scroll/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { ContentOffset } from '../../types';
1+
import { Point } from '../../types';
22

33
const DEFAULT_STEPS_COUNT = 5;
44

55
type InterpolatorFn = (end: number, start: number, steps: number) => number[];
66

77
export function createScrollSteps(
8-
target: Partial<ContentOffset>,
9-
initialOffset: ContentOffset,
8+
target: Partial<Point>,
9+
initialOffset: Point,
1010
interpolator: InterpolatorFn,
11-
): ContentOffset[] {
11+
): Point[] {
1212
if (target.y != null) {
1313
return interpolator(target.y, initialOffset.y, DEFAULT_STEPS_COUNT).map((y) => ({
1414
y,

src/user-event/utils/content-size.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
export interface ContentSize {
2-
width: number;
3-
height: number;
4-
}
1+
import { Size } from '../../types';
52

63
/**
74
* Simple function for getting mock the size of given text.
@@ -13,8 +10,7 @@ export interface ContentSize {
1310
* @param text text to be measure
1411
* @returns width and height of the text
1512
*/
16-
17-
export function getTextContentSize(text: string): ContentSize {
13+
export function getTextContentSize(text: string): Size {
1814
const lines = text.split('\n');
1915
const maxLineLength = Math.max(...lines.map((line) => line.length));
2016

0 commit comments

Comments
 (0)