From 5288be0a14305e77ae394115c1e30a796bac8b95 Mon Sep 17 00:00:00 2001 From: pierrezimmermann Date: Tue, 18 Jul 2023 09:42:18 +0200 Subject: [PATCH 1/4] feat: add autocomplete for fireEvent event names --- src/fireEvent.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/fireEvent.ts b/src/fireEvent.ts index cf87ec9ff..07e7f5ac0 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -1,4 +1,10 @@ import { ReactTestInstance } from 'react-test-renderer'; +import { + ViewProps, + TextProps, + ScrollViewProps, + PressableProps, +} from 'react-native'; import act from './act'; import { isHostElement } from './helpers/component-tree'; import { getHostComponentNames } from './helpers/host-component-names'; @@ -108,9 +114,24 @@ 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}` ? Rest : never]: T[K]; +}; + +type EventName = StringWithAutoComplete< + | OnKeys + | OnKeys + | OnKeys + | OnKeys +>; + function fireEvent( element: ReactTestInstance, - eventName: string, + eventName: EventName, ...data: unknown[] ) { const handler = findEventHandler(element, eventName); From 43d64d9634679b71dab504093230c92571cadc22 Mon Sep 17 00:00:00 2001 From: pierrezimmermann Date: Thu, 20 Jul 2023 16:45:53 +0200 Subject: [PATCH 2/4] feat: uncapitalize fireEvent autocomplete propositions --- src/fireEvent.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/fireEvent.ts b/src/fireEvent.ts index 07e7f5ac0..e6c12b169 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -119,7 +119,9 @@ 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}` ? Rest : never]: T[K]; + [K in keyof T as K extends `on${infer Rest}` + ? Uncapitalize + : never]: T[K]; }; type EventName = StringWithAutoComplete< From ac75ab6c35db06381c82644576f7f79b5d265d97 Mon Sep 17 00:00:00 2001 From: pierrezimmermann Date: Thu, 20 Jul 2023 16:46:34 +0200 Subject: [PATCH 3/4] feat: add textinput props to the autocomplete --- src/fireEvent.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fireEvent.ts b/src/fireEvent.ts index e6c12b169..01f197afa 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -4,6 +4,7 @@ import { TextProps, ScrollViewProps, PressableProps, + TextInput, } from 'react-native'; import act from './act'; import { isHostElement } from './helpers/component-tree'; @@ -129,6 +130,7 @@ type EventName = StringWithAutoComplete< | OnKeys | OnKeys | OnKeys + | OnKeys >; function fireEvent( From 696d516ea77946e1dd3164490d60986d32e65aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 20 Jul 2023 17:00:49 +0200 Subject: [PATCH 4/4] refactor: fix textinput props reference --- src/fireEvent.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fireEvent.ts b/src/fireEvent.ts index 01f197afa..ca492d8ca 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -2,9 +2,9 @@ import { ReactTestInstance } from 'react-test-renderer'; import { ViewProps, TextProps, - ScrollViewProps, + TextInputProps, PressableProps, - TextInput, + ScrollViewProps, } from 'react-native'; import act from './act'; import { isHostElement } from './helpers/component-tree'; @@ -128,9 +128,9 @@ type OnKeys = keyof { type EventName = StringWithAutoComplete< | OnKeys | OnKeys - | OnKeys + | OnKeys | OnKeys - | OnKeys + | OnKeys >; function fireEvent(