Skip to content

fix: expose common base synthetic event methods #1589

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

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions src/user-event/__tests__/__snapshots__/clear.test.tsx.snap

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/user-event/event-builder/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BaseSyntheticEvent } from 'react';

/** Builds base syntentic event stub, with prop values as inspected in RN runtime. */
export function baseSyntheticEvent(): Partial<BaseSyntheticEvent<{}, unknown, unknown>> {
return {
currentTarget: {},
target: {},
preventDefault: () => {},
isDefaultPrevented: () => false,
stopPropagation: () => {},
isPropagationStopped: () => false,
persist: () => {},
// @ts-expect-error: `isPersistent` is not a standard prop, but it's used in RN runtime. See: https://react.dev/reference/react-dom/components/common#react-event-object-methods
isPersistent: () => false,
timeStamp: 0,
};
}
11 changes: 5 additions & 6 deletions src/user-event/event-builder/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { baseSyntheticEvent } from './base';

/**
* Experimental values:
* - iOS: `{"changedTouches": [[Circular]], "identifier": 1, "locationX": 253, "locationY": 30.333328247070312, "pageX": 273, "pageY": 141.3333282470703, "target": 75, "timestamp": 875928682.0450834, "touches": [[Circular]]}`
* - Android: `{"changedTouches": [[Circular]], "identifier": 0, "locationX": 160, "locationY": 40.3636360168457, "pageX": 180, "pageY": 140.36363220214844, "target": 53, "targetSurface": -1, "timestamp": 10290805, "touches": [[Circular]]}`
*/
function touch() {
return {
...baseSyntheticEvent(),
nativeEvent: {
changedTouches: [],
identifier: 0,
Expand All @@ -16,9 +19,7 @@ function touch() {
timestamp: Date.now(),
touches: [],
},
persist: () => {},
currentTarget: { measure: () => {} },
target: {},
};
}

Expand Down Expand Up @@ -46,11 +47,10 @@ export const CommonEventBuilder = {
*/
focus: () => {
return {
...baseSyntheticEvent(),
nativeEvent: {
target: 0,
},
currentTarget: {},
target: {},
};
},

Expand All @@ -61,11 +61,10 @@ export const CommonEventBuilder = {
*/
blur: () => {
return {
...baseSyntheticEvent(),
nativeEvent: {
target: 0,
},
currentTarget: {},
target: {},
};
},
};
5 changes: 3 additions & 2 deletions src/user-event/event-builder/scroll-view.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { baseSyntheticEvent } from './base';

/**
* Scroll position of a scrollable element.
*/
Expand Down Expand Up @@ -28,6 +30,7 @@ export type ScrollEventOptions = {
export const ScrollViewEventBuilder = {
scroll: (offset: ContentOffset = { y: 0, x: 0 }, options?: ScrollEventOptions) => {
return {
...baseSyntheticEvent(),
nativeEvent: {
contentInset: { bottom: 0, left: 0, right: 0, top: 0 },
contentOffset: { y: offset.y, x: offset.x },
Expand All @@ -43,8 +46,6 @@ export const ScrollViewEventBuilder = {
target: 0,
velocity: { y: 0, x: 0 },
},
currentTarget: {},
target: {},
};
},
};
22 changes: 8 additions & 14 deletions src/user-event/event-builder/text-input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContentSize } from '../utils/content-size';
import { TextRange } from '../utils/text-range';
import { baseSyntheticEvent } from './base';

export const TextInputEventBuilder = {
/**
Expand All @@ -9,9 +10,8 @@ export const TextInputEventBuilder = {
*/
change: (text: string) => {
return {
...baseSyntheticEvent(),
nativeEvent: { text, target: 0, eventCount: 0 },
currentTarget: {},
target: {},
};
},

Expand All @@ -22,9 +22,8 @@ export const TextInputEventBuilder = {
*/
keyPress: (key: string) => {
return {
...baseSyntheticEvent(),
nativeEvent: { key },
currentTarget: {},
target: {},
};
},

Expand All @@ -35,9 +34,8 @@ export const TextInputEventBuilder = {
*/
submitEditing: (text: string) => {
return {
...baseSyntheticEvent(),
nativeEvent: { text, target: 0 },
currentTarget: {},
target: {},
};
},

Expand All @@ -48,9 +46,8 @@ export const TextInputEventBuilder = {
*/
endEditing: (text: string) => {
return {
...baseSyntheticEvent(),
nativeEvent: { text, target: 0 },
currentTarget: {},
target: {},
};
},

Expand All @@ -61,9 +58,8 @@ export const TextInputEventBuilder = {
*/
selectionChange: ({ start, end }: TextRange) => {
return {
...baseSyntheticEvent(),
nativeEvent: { selection: { start, end } },
currentTarget: {},
target: {},
};
},

Expand All @@ -74,14 +70,13 @@ export const TextInputEventBuilder = {
*/
textInput: (text: string, previousText: string) => {
return {
...baseSyntheticEvent(),
nativeEvent: {
text,
previousText,
range: { start: text.length, end: text.length },
target: 0,
},
currentTarget: {},
target: {},
};
},

Expand All @@ -92,9 +87,8 @@ export const TextInputEventBuilder = {
*/
contentSizeChange: ({ width, height }: ContentSize) => {
return {
...baseSyntheticEvent(),
nativeEvent: { contentSize: { width, height }, target: 0 },
currentTarget: {},
target: {},
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exports[`userEvent.longPress with fake timers calls onLongPress if the delayLong
"dispatchConfig": {
"registrationName": "onResponderGrant",
},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"changedTouches": [],
"identifier": 0,
Expand All @@ -23,7 +26,10 @@ exports[`userEvent.longPress with fake timers calls onLongPress if the delayLong
"touches": [],
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
]
Expand Down
18 changes: 18 additions & 0 deletions src/user-event/press/__tests__/__snapshots__/press.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
"dispatchConfig": {
"registrationName": "onResponderGrant",
},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"changedTouches": [],
"identifier": 0,
Expand All @@ -23,7 +26,10 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
"touches": [],
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
{
Expand All @@ -35,6 +41,9 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
"dispatchConfig": {
"registrationName": "onResponderRelease",
},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"changedTouches": [],
"identifier": 0,
Expand All @@ -47,7 +56,10 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
"touches": [],
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
{
Expand All @@ -59,6 +71,9 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
"dispatchConfig": {
"registrationName": "onResponderRelease",
},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"changedTouches": [],
"identifier": 0,
Expand All @@ -71,7 +86,10 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
"touches": [],
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
"name": "scrollBeginDrag",
"payload": {
"currentTarget": {},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"contentInset": {
"bottom": 0,
Expand All @@ -32,13 +35,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
"y": 0,
},
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
{
"name": "scroll",
"payload": {
"currentTarget": {},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"contentInset": {
"bottom": 0,
Expand All @@ -65,13 +75,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
"y": 0,
},
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
{
"name": "scroll",
"payload": {
"currentTarget": {},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"contentInset": {
"bottom": 0,
Expand All @@ -98,13 +115,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
"y": 0,
},
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
{
"name": "scroll",
"payload": {
"currentTarget": {},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"contentInset": {
"bottom": 0,
Expand All @@ -131,13 +155,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
"y": 0,
},
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
{
"name": "scrollEndDrag",
"payload": {
"currentTarget": {},
"isDefaultPrevented": [Function],
"isPersistent": [Function],
"isPropagationStopped": [Function],
"nativeEvent": {
"contentInset": {
"bottom": 0,
Expand All @@ -164,7 +195,11 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
"y": 0,
},
},
"persist": [Function],
"preventDefault": [Function],
"stopPropagation": [Function],
"target": {},
"timeStamp": 0,
},
},
]
Expand Down
Loading
Loading