-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathfireEvent.js
51 lines (40 loc) · 1.46 KB
/
fireEvent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// @flow
import ErrorWithStack from './helpers/errorWithStack';
const findEventHandler = (element: ReactTestInstance, eventName: string) => {
const eventHandler = toEventHandlerName(eventName);
if (typeof element.props[eventHandler] === 'function') {
return element.props[eventHandler];
}
// Do not bubble event to the root element
if (element.parent === null || element.parent.parent === null) {
throw new ErrorWithStack(
`No handler function found for event: ${eventName}`,
invokeEvent
);
}
return findEventHandler(element.parent, eventName);
};
const invokeEvent = (
element: ReactTestInstance,
eventName: string,
data?: *
) => {
const handler = findEventHandler(element, eventName);
return handler(data);
};
const toEventHandlerName = (eventName: string) =>
`on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
const pressHandler = (element: ReactTestInstance) =>
invokeEvent(element, 'press');
const doublePressHandler = (element: ReactTestInstance) =>
invokeEvent(element, 'doublePress');
const changeTextHandler = (element: ReactTestInstance, data?: *) =>
invokeEvent(element, 'changeText', data);
const scrollHandler = (element: ReactTestInstance, data?: *) =>
invokeEvent(element, 'scroll', data);
const fireEvent = invokeEvent;
fireEvent.press = pressHandler;
fireEvent.doublePress = doublePressHandler;
fireEvent.changeText = changeTextHandler;
fireEvent.scroll = scrollHandler;
export default fireEvent;