Skip to content

Commit 5288be0

Browse files
author
pierrezimmermann
committed
feat: add autocomplete for fireEvent event names
1 parent 5bb5d2d commit 5288be0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/fireEvent.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import { ReactTestInstance } from 'react-test-renderer';
2+
import {
3+
ViewProps,
4+
TextProps,
5+
ScrollViewProps,
6+
PressableProps,
7+
} from 'react-native';
28
import act from './act';
39
import { isHostElement } from './helpers/component-tree';
410
import { getHostComponentNames } from './helpers/host-component-names';
@@ -108,9 +114,24 @@ function getEventHandlerName(eventName: string) {
108114
return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
109115
}
110116

117+
// Allows any string but will provide autocomplete for type T
118+
type StringWithAutoComplete<T> = T | (string & Record<never, never>);
119+
120+
// String union type of keys of T that start with on, stripped from on
121+
type OnKeys<T> = keyof {
122+
[K in keyof T as K extends `on${infer Rest}` ? Rest : never]: T[K];
123+
};
124+
125+
type EventName = StringWithAutoComplete<
126+
| OnKeys<ViewProps>
127+
| OnKeys<TextProps>
128+
| OnKeys<ScrollViewProps>
129+
| OnKeys<PressableProps>
130+
>;
131+
111132
function fireEvent(
112133
element: ReactTestInstance,
113-
eventName: string,
134+
eventName: EventName,
114135
...data: unknown[]
115136
) {
116137
const handler = findEventHandler(element, eventName);

0 commit comments

Comments
 (0)