Skip to content

Commit f7e3b1a

Browse files
fix: expose common base synthetic event methods (#1589)
* fix: add stup base synthetic event methods * chore: update snapshots * refactor: self code review * refactor: improvements * chore: add test
1 parent b9c9d04 commit f7e3b1a

File tree

12 files changed

+861
-23
lines changed

12 files changed

+861
-23
lines changed

src/user-event/__tests__/__snapshots__/clear.test.tsx.snap

Lines changed: 168 additions & 0 deletions
Large diffs are not rendered by default.

src/user-event/event-builder/base.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BaseSyntheticEvent } from 'react';
2+
3+
/** Builds base syntentic event stub, with prop values as inspected in RN runtime. */
4+
export function baseSyntheticEvent(): Partial<BaseSyntheticEvent<{}, unknown, unknown>> {
5+
return {
6+
currentTarget: {},
7+
target: {},
8+
preventDefault: () => {},
9+
isDefaultPrevented: () => false,
10+
stopPropagation: () => {},
11+
isPropagationStopped: () => false,
12+
persist: () => {},
13+
// @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
14+
isPersistent: () => false,
15+
timeStamp: 0,
16+
};
17+
}

src/user-event/event-builder/common.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { baseSyntheticEvent } from './base';
2+
13
/**
24
* Experimental values:
35
* - iOS: `{"changedTouches": [[Circular]], "identifier": 1, "locationX": 253, "locationY": 30.333328247070312, "pageX": 273, "pageY": 141.3333282470703, "target": 75, "timestamp": 875928682.0450834, "touches": [[Circular]]}`
46
* - Android: `{"changedTouches": [[Circular]], "identifier": 0, "locationX": 160, "locationY": 40.3636360168457, "pageX": 180, "pageY": 140.36363220214844, "target": 53, "targetSurface": -1, "timestamp": 10290805, "touches": [[Circular]]}`
57
*/
68
function touch() {
79
return {
10+
...baseSyntheticEvent(),
811
nativeEvent: {
912
changedTouches: [],
1013
identifier: 0,
@@ -16,9 +19,7 @@ function touch() {
1619
timestamp: Date.now(),
1720
touches: [],
1821
},
19-
persist: () => {},
2022
currentTarget: { measure: () => {} },
21-
target: {},
2223
};
2324
}
2425

@@ -46,11 +47,10 @@ export const CommonEventBuilder = {
4647
*/
4748
focus: () => {
4849
return {
50+
...baseSyntheticEvent(),
4951
nativeEvent: {
5052
target: 0,
5153
},
52-
currentTarget: {},
53-
target: {},
5454
};
5555
},
5656

@@ -61,11 +61,10 @@ export const CommonEventBuilder = {
6161
*/
6262
blur: () => {
6363
return {
64+
...baseSyntheticEvent(),
6465
nativeEvent: {
6566
target: 0,
6667
},
67-
currentTarget: {},
68-
target: {},
6968
};
7069
},
7170
};

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { baseSyntheticEvent } from './base';
2+
13
/**
24
* Scroll position of a scrollable element.
35
*/
@@ -28,6 +30,7 @@ export type ScrollEventOptions = {
2830
export const ScrollViewEventBuilder = {
2931
scroll: (offset: ContentOffset = { y: 0, x: 0 }, options?: ScrollEventOptions) => {
3032
return {
33+
...baseSyntheticEvent(),
3134
nativeEvent: {
3235
contentInset: { bottom: 0, left: 0, right: 0, top: 0 },
3336
contentOffset: { y: offset.y, x: offset.x },
@@ -43,8 +46,6 @@ export const ScrollViewEventBuilder = {
4346
target: 0,
4447
velocity: { y: 0, x: 0 },
4548
},
46-
currentTarget: {},
47-
target: {},
4849
};
4950
},
5051
};

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ContentSize } from '../utils/content-size';
22
import { TextRange } from '../utils/text-range';
3+
import { baseSyntheticEvent } from './base';
34

45
export const TextInputEventBuilder = {
56
/**
@@ -9,9 +10,8 @@ export const TextInputEventBuilder = {
910
*/
1011
change: (text: string) => {
1112
return {
13+
...baseSyntheticEvent(),
1214
nativeEvent: { text, target: 0, eventCount: 0 },
13-
currentTarget: {},
14-
target: {},
1515
};
1616
},
1717

@@ -22,9 +22,8 @@ export const TextInputEventBuilder = {
2222
*/
2323
keyPress: (key: string) => {
2424
return {
25+
...baseSyntheticEvent(),
2526
nativeEvent: { key },
26-
currentTarget: {},
27-
target: {},
2827
};
2928
},
3029

@@ -35,9 +34,8 @@ export const TextInputEventBuilder = {
3534
*/
3635
submitEditing: (text: string) => {
3736
return {
37+
...baseSyntheticEvent(),
3838
nativeEvent: { text, target: 0 },
39-
currentTarget: {},
40-
target: {},
4139
};
4240
},
4341

@@ -48,9 +46,8 @@ export const TextInputEventBuilder = {
4846
*/
4947
endEditing: (text: string) => {
5048
return {
49+
...baseSyntheticEvent(),
5150
nativeEvent: { text, target: 0 },
52-
currentTarget: {},
53-
target: {},
5451
};
5552
},
5653

@@ -61,9 +58,8 @@ export const TextInputEventBuilder = {
6158
*/
6259
selectionChange: ({ start, end }: TextRange) => {
6360
return {
61+
...baseSyntheticEvent(),
6462
nativeEvent: { selection: { start, end } },
65-
currentTarget: {},
66-
target: {},
6763
};
6864
},
6965

@@ -74,14 +70,13 @@ export const TextInputEventBuilder = {
7470
*/
7571
textInput: (text: string, previousText: string) => {
7672
return {
73+
...baseSyntheticEvent(),
7774
nativeEvent: {
7875
text,
7976
previousText,
8077
range: { start: text.length, end: text.length },
8178
target: 0,
8279
},
83-
currentTarget: {},
84-
target: {},
8580
};
8681
},
8782

@@ -92,9 +87,8 @@ export const TextInputEventBuilder = {
9287
*/
9388
contentSizeChange: ({ width, height }: ContentSize) => {
9489
return {
90+
...baseSyntheticEvent(),
9591
nativeEvent: { contentSize: { width, height }, target: 0 },
96-
currentTarget: {},
97-
target: {},
9892
};
9993
},
10094
};

src/user-event/press/__tests__/__snapshots__/longPress.test.tsx.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ exports[`userEvent.longPress with fake timers calls onLongPress if the delayLong
1111
"dispatchConfig": {
1212
"registrationName": "onResponderGrant",
1313
},
14+
"isDefaultPrevented": [Function],
15+
"isPersistent": [Function],
16+
"isPropagationStopped": [Function],
1417
"nativeEvent": {
1518
"changedTouches": [],
1619
"identifier": 0,
@@ -23,7 +26,10 @@ exports[`userEvent.longPress with fake timers calls onLongPress if the delayLong
2326
"touches": [],
2427
},
2528
"persist": [Function],
29+
"preventDefault": [Function],
30+
"stopPropagation": [Function],
2631
"target": {},
32+
"timeStamp": 0,
2733
},
2834
},
2935
]

src/user-event/press/__tests__/__snapshots__/press.test.tsx.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
1111
"dispatchConfig": {
1212
"registrationName": "onResponderGrant",
1313
},
14+
"isDefaultPrevented": [Function],
15+
"isPersistent": [Function],
16+
"isPropagationStopped": [Function],
1417
"nativeEvent": {
1518
"changedTouches": [],
1619
"identifier": 0,
@@ -23,7 +26,10 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
2326
"touches": [],
2427
},
2528
"persist": [Function],
29+
"preventDefault": [Function],
30+
"stopPropagation": [Function],
2631
"target": {},
32+
"timeStamp": 0,
2733
},
2834
},
2935
{
@@ -35,6 +41,9 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
3541
"dispatchConfig": {
3642
"registrationName": "onResponderRelease",
3743
},
44+
"isDefaultPrevented": [Function],
45+
"isPersistent": [Function],
46+
"isPropagationStopped": [Function],
3847
"nativeEvent": {
3948
"changedTouches": [],
4049
"identifier": 0,
@@ -47,7 +56,10 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
4756
"touches": [],
4857
},
4958
"persist": [Function],
59+
"preventDefault": [Function],
60+
"stopPropagation": [Function],
5061
"target": {},
62+
"timeStamp": 0,
5163
},
5264
},
5365
{
@@ -59,6 +71,9 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
5971
"dispatchConfig": {
6072
"registrationName": "onResponderRelease",
6173
},
74+
"isDefaultPrevented": [Function],
75+
"isPersistent": [Function],
76+
"isPropagationStopped": [Function],
6277
"nativeEvent": {
6378
"changedTouches": [],
6479
"identifier": 0,
@@ -71,7 +86,10 @@ exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOu
7186
"touches": [],
7287
},
7388
"persist": [Function],
89+
"preventDefault": [Function],
90+
"stopPropagation": [Function],
7491
"target": {},
92+
"timeStamp": 0,
7593
},
7694
},
7795
]

src/user-event/scroll/__tests__/__snapshots__/scroll-to-flat-list.test.tsx.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
66
"name": "scrollBeginDrag",
77
"payload": {
88
"currentTarget": {},
9+
"isDefaultPrevented": [Function],
10+
"isPersistent": [Function],
11+
"isPropagationStopped": [Function],
912
"nativeEvent": {
1013
"contentInset": {
1114
"bottom": 0,
@@ -32,13 +35,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
3235
"y": 0,
3336
},
3437
},
38+
"persist": [Function],
39+
"preventDefault": [Function],
40+
"stopPropagation": [Function],
3541
"target": {},
42+
"timeStamp": 0,
3643
},
3744
},
3845
{
3946
"name": "scroll",
4047
"payload": {
4148
"currentTarget": {},
49+
"isDefaultPrevented": [Function],
50+
"isPersistent": [Function],
51+
"isPropagationStopped": [Function],
4252
"nativeEvent": {
4353
"contentInset": {
4454
"bottom": 0,
@@ -65,13 +75,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
6575
"y": 0,
6676
},
6777
},
78+
"persist": [Function],
79+
"preventDefault": [Function],
80+
"stopPropagation": [Function],
6881
"target": {},
82+
"timeStamp": 0,
6983
},
7084
},
7185
{
7286
"name": "scroll",
7387
"payload": {
7488
"currentTarget": {},
89+
"isDefaultPrevented": [Function],
90+
"isPersistent": [Function],
91+
"isPropagationStopped": [Function],
7592
"nativeEvent": {
7693
"contentInset": {
7794
"bottom": 0,
@@ -98,13 +115,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
98115
"y": 0,
99116
},
100117
},
118+
"persist": [Function],
119+
"preventDefault": [Function],
120+
"stopPropagation": [Function],
101121
"target": {},
122+
"timeStamp": 0,
102123
},
103124
},
104125
{
105126
"name": "scroll",
106127
"payload": {
107128
"currentTarget": {},
129+
"isDefaultPrevented": [Function],
130+
"isPersistent": [Function],
131+
"isPropagationStopped": [Function],
108132
"nativeEvent": {
109133
"contentInset": {
110134
"bottom": 0,
@@ -131,13 +155,20 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
131155
"y": 0,
132156
},
133157
},
158+
"persist": [Function],
159+
"preventDefault": [Function],
160+
"stopPropagation": [Function],
134161
"target": {},
162+
"timeStamp": 0,
135163
},
136164
},
137165
{
138166
"name": "scrollEndDrag",
139167
"payload": {
140168
"currentTarget": {},
169+
"isDefaultPrevented": [Function],
170+
"isPersistent": [Function],
171+
"isPropagationStopped": [Function],
141172
"nativeEvent": {
142173
"contentInset": {
143174
"bottom": 0,
@@ -164,7 +195,11 @@ exports[`scrollTo() with FlatList supports vertical drag scroll: scrollTo({ y: 1
164195
"y": 0,
165196
},
166197
},
198+
"persist": [Function],
199+
"preventDefault": [Function],
200+
"stopPropagation": [Function],
167201
"target": {},
202+
"timeStamp": 0,
168203
},
169204
},
170205
]

0 commit comments

Comments
 (0)