Skip to content

Commit 04b42b4

Browse files
committed
flatten event builder
1 parent 9318788 commit 04b42b4

File tree

12 files changed

+205
-237
lines changed

12 files changed

+205
-237
lines changed

src/event-builder/common.ts

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { baseSyntheticEvent } from './base';
55
* - iOS: `{"changedTouches": [[Circular]], "identifier": 1, "locationX": 253, "locationY": 30.333328247070312, "pageX": 273, "pageY": 141.3333282470703, "target": 75, "timestamp": 875928682.0450834, "touches": [[Circular]]}`
66
* - Android: `{"changedTouches": [[Circular]], "identifier": 0, "locationX": 160, "locationY": 40.3636360168457, "pageX": 180, "pageY": 140.36363220214844, "target": 53, "targetSurface": -1, "timestamp": 10290805, "touches": [[Circular]]}`
77
*/
8-
function touch() {
8+
export function buildTouchEvent() {
99
return {
1010
...baseSyntheticEvent(),
1111
nativeEvent: {
@@ -23,50 +23,46 @@ function touch() {
2323
};
2424
}
2525

26-
export type TouchEvent = ReturnType<typeof touch>;
26+
export type TouchEvent = ReturnType<typeof buildTouchEvent>;
2727

28-
export const CommonEventBuilder = {
29-
touch,
30-
31-
responderGrant: () => {
32-
return {
33-
...touch(),
34-
dispatchConfig: { registrationName: 'onResponderGrant' },
35-
};
36-
},
28+
export function buildResponderGrantEvent() {
29+
return {
30+
...buildTouchEvent(),
31+
dispatchConfig: { registrationName: 'onResponderGrant' },
32+
};
33+
}
3734

38-
responderRelease: () => {
39-
return {
40-
...touch(),
41-
dispatchConfig: { registrationName: 'onResponderRelease' },
42-
};
43-
},
35+
export function buildResponderReleaseEvent() {
36+
return {
37+
...buildTouchEvent(),
38+
dispatchConfig: { registrationName: 'onResponderRelease' },
39+
};
40+
}
4441

45-
/**
46-
* Experimental values:
47-
* - iOS: `{"eventCount": 0, "target": 75, "text": ""}`
48-
* - Android: `{"target": 53}`
49-
*/
50-
focus: () => {
51-
return {
52-
...baseSyntheticEvent(),
53-
nativeEvent: {
54-
target: 0,
55-
},
56-
};
57-
},
42+
/**
43+
* Experimental values:
44+
* - iOS: `{"eventCount": 0, "target": 75, "text": ""}`
45+
* - Android: `{"target": 53}`
46+
*/
47+
export function buildFocusEvent() {
48+
return {
49+
...baseSyntheticEvent(),
50+
nativeEvent: {
51+
target: 0,
52+
},
53+
};
54+
}
5855

59-
/**
60-
* Experimental values:
61-
* - iOS: `{"eventCount": 0, "target": 75, "text": ""}`
62-
* - Android: `{"target": 53}`
63-
*/
64-
blur: () => {
65-
return {
66-
...baseSyntheticEvent(),
67-
nativeEvent: {
68-
target: 0,
69-
},
70-
};
71-
},
72-
};
56+
/**
57+
* Experimental values:
58+
* - iOS: `{"eventCount": 0, "target": 75, "text": ""}`
59+
* - Android: `{"target": 53}`
60+
*/
61+
export function buildBlurEvent() {
62+
return {
63+
...baseSyntheticEvent(),
64+
nativeEvent: {
65+
target: 0,
66+
},
67+
};
68+
}

src/event-builder/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import { CommonEventBuilder } from './common';
2-
import { ScrollViewEventBuilder } from './scroll-view';
3-
import { TextInputEventBuilder } from './text-input';
4-
5-
export const EventBuilder = {
6-
Common: CommonEventBuilder,
7-
ScrollView: ScrollViewEventBuilder,
8-
TextInput: TextInputEventBuilder,
9-
};
1+
export * from './common';
2+
export * from './scroll';
3+
export * from './text';
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@ export type ScrollEventOptions = {
1414
* - 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}`
1515
* - 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}}`
1616
*/
17-
export const ScrollViewEventBuilder = {
18-
scroll: (offset: Point = { y: 0, x: 0 }, options?: ScrollEventOptions) => {
19-
return {
20-
...baseSyntheticEvent(),
21-
nativeEvent: {
22-
contentInset: { bottom: 0, left: 0, right: 0, top: 0 },
23-
contentOffset: { y: offset.y, x: offset.x },
24-
contentSize: {
25-
height: options?.contentSize?.height ?? 0,
26-
width: options?.contentSize?.width ?? 0,
27-
},
28-
layoutMeasurement: {
29-
height: options?.layoutMeasurement?.height ?? 0,
30-
width: options?.layoutMeasurement?.width ?? 0,
31-
},
32-
responderIgnoreScroll: true,
33-
target: 0,
34-
velocity: { y: 0, x: 0 },
17+
export function buildScrollEvent(offset: Point = { y: 0, x: 0 }, options?: ScrollEventOptions) {
18+
return {
19+
...baseSyntheticEvent(),
20+
nativeEvent: {
21+
contentInset: { bottom: 0, left: 0, right: 0, top: 0 },
22+
contentOffset: { y: offset.y, x: offset.x },
23+
contentSize: {
24+
height: options?.contentSize?.height ?? 0,
25+
width: options?.contentSize?.width ?? 0,
3526
},
36-
};
37-
},
38-
};
27+
layoutMeasurement: {
28+
height: options?.layoutMeasurement?.height ?? 0,
29+
width: options?.layoutMeasurement?.width ?? 0,
30+
},
31+
responderIgnoreScroll: true,
32+
target: 0,
33+
velocity: { y: 0, x: 0 },
34+
},
35+
};
36+
}

src/event-builder/text-input.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/event-builder/text.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { baseSyntheticEvent } from './base';
2+
import type { Size, TextRange } from './types';
3+
4+
/**
5+
* Experimental values:
6+
* - iOS: `{"eventCount": 4, "target": 75, "text": "Test"}`
7+
* - Android: `{"eventCount": 6, "target": 53, "text": "Tes"}`
8+
*/
9+
export function buildTextChangeEvent(text: string) {
10+
return {
11+
...baseSyntheticEvent(),
12+
nativeEvent: { text, target: 0, eventCount: 0 },
13+
};
14+
}
15+
16+
/**
17+
* Experimental values:
18+
* - iOS: `{"eventCount": 3, "key": "a", "target": 75}`
19+
* - Android: `{"key": "a"}`
20+
*/
21+
export function buildKeyPressEvent(key: string) {
22+
return {
23+
...baseSyntheticEvent(),
24+
nativeEvent: { key },
25+
};
26+
}
27+
28+
/**
29+
* Experimental values:
30+
* - iOS: `{"eventCount": 4, "target": 75, "text": "Test"}`
31+
* - Android: `{"target": 53, "text": "Test"}`
32+
*/
33+
export function buildSubmitEditingEvent(text: string) {
34+
return {
35+
...baseSyntheticEvent(),
36+
nativeEvent: { text, target: 0 },
37+
};
38+
}
39+
40+
/**
41+
* Experimental values:
42+
* - iOS: `{"eventCount": 4, "target": 75, "text": "Test"}`
43+
* - Android: `{"target": 53, "text": "Test"}`
44+
*/
45+
export function buildEndEditingEvent(text: string) {
46+
return {
47+
...baseSyntheticEvent(),
48+
nativeEvent: { text, target: 0 },
49+
};
50+
}
51+
52+
/**
53+
* Experimental values:
54+
* - iOS: `{"selection": {"end": 4, "start": 4}, "target": 75}`
55+
* - Android: `{"selection": {"end": 4, "start": 4}}`
56+
*/
57+
export function buildTextSelectionChangeEvent({ start, end }: TextRange) {
58+
return {
59+
...baseSyntheticEvent(),
60+
nativeEvent: { selection: { start, end } },
61+
};
62+
}
63+
64+
/**
65+
* Experimental values:
66+
* - iOS: `{"contentSize": {"height": 21.666666666666668, "width": 11.666666666666666}, "target": 75}`
67+
* - Android: `{"contentSize": {"height": 61.45454406738281, "width": 352.7272644042969}, "target": 53}`
68+
*/
69+
export function buildContentSizeChangeEvent({ width, height }: Size) {
70+
return {
71+
...baseSyntheticEvent(),
72+
nativeEvent: { contentSize: { width, height }, target: 0 },
73+
};
74+
}

src/fire-event.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
import type { Fiber, HostElement } from 'test-renderer';
99

1010
import { act } from './act';
11-
import { EventBuilder } from './event-builder';
11+
import { buildScrollEvent, buildTouchEvent } from './event-builder';
1212
import type { Point } from './event-builder/types';
1313
import type { EventHandler } from './event-handler';
1414
import { getEventHandlerFromProps } from './event-handler';
@@ -150,7 +150,7 @@ fireEvent.changeText = async (element: HostElement, text: string) =>
150150
await fireEvent(element, 'changeText', text);
151151

152152
fireEvent.press = async (element: HostElement, eventProps?: EventProps) => {
153-
const event = EventBuilder.Common.touch();
153+
const event = buildTouchEvent();
154154
if (eventProps) {
155155
mergeEventProps(event, eventProps);
156156
}
@@ -159,7 +159,7 @@ fireEvent.press = async (element: HostElement, eventProps?: EventProps) => {
159159
};
160160

161161
fireEvent.scroll = async (element: HostElement, eventProps?: EventProps) => {
162-
const event = EventBuilder.ScrollView.scroll();
162+
const event = buildScrollEvent();
163163
if (eventProps) {
164164
mergeEventProps(event, eventProps);
165165
}

src/user-event/clear.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type { HostElement } from 'test-renderer';
22

3-
import { EventBuilder } from '../event-builder';
3+
import {
4+
buildBlurEvent,
5+
buildEndEditingEvent,
6+
buildFocusEvent,
7+
buildTextSelectionChangeEvent,
8+
} from '../event-builder';
49
import { ErrorWithStack } from '../helpers/errors';
510
import { isHostTextInput } from '../helpers/host-component-names';
611
import { isPointerEventEnabled } from '../helpers/pointer-events';
@@ -22,19 +27,15 @@ export async function clear(this: UserEventInstance, element: HostElement): Prom
2227
}
2328

2429
// 1. Enter element
25-
await dispatchEvent(element, 'focus', EventBuilder.Common.focus());
30+
await dispatchEvent(element, 'focus', buildFocusEvent());
2631

2732
// 2. Select all
2833
const textToClear = getTextInputValue(element);
2934
const selectionRange = {
3035
start: 0,
3136
end: textToClear.length,
3237
};
33-
await dispatchEvent(
34-
element,
35-
'selectionChange',
36-
EventBuilder.TextInput.selectionChange(selectionRange),
37-
);
38+
await dispatchEvent(element, 'selectionChange', buildTextSelectionChangeEvent(selectionRange));
3839

3940
// 3. Press backspace with selected text
4041
const emptyText = '';
@@ -46,6 +47,6 @@ export async function clear(this: UserEventInstance, element: HostElement): Prom
4647

4748
// 4. Exit element
4849
await wait(this.config);
49-
await dispatchEvent(element, 'endEditing', EventBuilder.TextInput.endEditing(emptyText));
50-
await dispatchEvent(element, 'blur', EventBuilder.Common.blur());
50+
await dispatchEvent(element, 'endEditing', buildEndEditingEvent(emptyText));
51+
await dispatchEvent(element, 'blur', buildBlurEvent());
5152
}

0 commit comments

Comments
 (0)