Skip to content

Commit 7587e69

Browse files
pierrezimmermannbampierrezimmermannmdjastrzebski
authored
feat: add autocomplete for fireEvent event names (#1434)
* feat: add autocomplete for fireEvent event names * feat: uncapitalize fireEvent autocomplete propositions * feat: add textinput props to the autocomplete * refactor: fix textinput props reference --------- Co-authored-by: pierrezimmermann <[email protected]> Co-authored-by: Maciej Jastrzębski <[email protected]>
1 parent fa0c86e commit 7587e69

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Diff for: src/fireEvent.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import { ReactTestInstance } from 'react-test-renderer';
2+
import {
3+
ViewProps,
4+
TextProps,
5+
TextInputProps,
6+
PressableProps,
7+
ScrollViewProps,
8+
} from 'react-native';
29
import act from './act';
310
import { isHostElement } from './helpers/component-tree';
411
import { getHostComponentNames } from './helpers/host-component-names';
@@ -108,9 +115,27 @@ function getEventHandlerName(eventName: string) {
108115
return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
109116
}
110117

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

0 commit comments

Comments
 (0)