diff --git a/src/fireEvent.ts b/src/fireEvent.ts index cf87ec9ff..ca492d8ca 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -1,4 +1,11 @@ import { ReactTestInstance } from 'react-test-renderer'; +import { + ViewProps, + TextProps, + TextInputProps, + PressableProps, + ScrollViewProps, +} from 'react-native'; import act from './act'; import { isHostElement } from './helpers/component-tree'; import { getHostComponentNames } from './helpers/host-component-names'; @@ -108,9 +115,27 @@ function getEventHandlerName(eventName: string) { return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`; } +// Allows any string but will provide autocomplete for type T +type StringWithAutoComplete = T | (string & Record); + +// String union type of keys of T that start with on, stripped from on +type OnKeys = keyof { + [K in keyof T as K extends `on${infer Rest}` + ? Uncapitalize + : never]: T[K]; +}; + +type EventName = StringWithAutoComplete< + | OnKeys + | OnKeys + | OnKeys + | OnKeys + | OnKeys +>; + function fireEvent( element: ReactTestInstance, - eventName: string, + eventName: EventName, ...data: unknown[] ) { const handler = findEventHandler(element, eventName);