Skip to content

Commit 32b1867

Browse files
committed
refactor: cleanup
1 parent baa68a3 commit 32b1867

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/fire-event.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { isHostElement } from './helpers/component-tree';
1111
import { isHostTextInput } from './helpers/host-component-names';
1212
import { isPointerEventEnabled } from './helpers/pointer-events';
1313
import { isTextInputEditable } from './helpers/text-input';
14+
import { StringWithAutocomplete } from './types';
1415

1516
type EventHandler = (...args: unknown[]) => unknown;
1617

@@ -106,19 +107,17 @@ function getEventHandlerName(eventName: string) {
106107
}
107108

108109
// String union type of keys of T that start with on, stripped of 'on'
109-
type OnKeys<T> = keyof {
110+
type EventNameExtractor<T> = keyof {
110111
[K in keyof T as K extends `on${infer Rest}` ? Uncapitalize<Rest> : never]: T[K];
111112
};
112113

113-
// TS autocomplete trick
114-
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
115-
type EventName =
116-
| OnKeys<ViewProps>
117-
| OnKeys<TextProps>
118-
| OnKeys<TextInputProps>
119-
| OnKeys<PressableProps>
120-
| OnKeys<ScrollViewProps>
121-
| (string & {});
114+
type EventName = StringWithAutocomplete<
115+
| EventNameExtractor<ViewProps>
116+
| EventNameExtractor<TextProps>
117+
| EventNameExtractor<TextInputProps>
118+
| EventNameExtractor<PressableProps>
119+
| EventNameExtractor<ScrollViewProps>
120+
>;
122121

123122
function fireEvent(element: ReactTestInstance, eventName: EventName, ...data: unknown[]) {
124123
const handler = findEventHandler(element, eventName);

src/queries/role.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '../helpers/matchers/match-accessibility-value';
1818
import { matchStringProp } from '../helpers/matchers/match-string-prop';
1919
import type { TextMatch } from '../matches';
20+
import { StringWithAutocomplete } from '../types';
2021
import { getQueriesForElement } from '../within';
2122
import { makeQueries } from './make-queries';
2223
import type {
@@ -29,11 +30,9 @@ import type {
2930
} from './make-queries';
3031
import { CommonQueryOptions } from './options';
3132

32-
// TS autocomplete trick
33-
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
34-
export type RoleMatcher = AccessibilityRole | Role | (string & {}) | RegExp;
33+
export type RoleMatcher = StringWithAutocomplete<AccessibilityRole | Role> | RegExp;
3534

36-
type ByRoleOptions = CommonQueryOptions &
35+
export type ByRoleOptions = CommonQueryOptions &
3736
AccessibilityStateMatcher & {
3837
name?: TextMatch;
3938
value?: AccessibilityValueMatcher;

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// TS autocomplete trick
2+
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
3+
export type StringWithAutocomplete<T> = T | (string & {});

0 commit comments

Comments
 (0)