Skip to content

Commit baa68a3

Browse files
committed
feat: autocomplete
1 parent 16b9e49 commit baa68a3

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cSpell.words": ["Pressable", "RNTL"]
2+
"cSpell.words": ["Pressable", "RNTL", "Uncapitalize"]
33
}

src/fire-event.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,20 @@ function getEventHandlerName(eventName: string) {
105105
return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
106106
}
107107

108-
// Allows any string but will provide autocomplete for type T
109-
type StringWithAutoComplete<T> = T | (string & Record<never, never>);
110-
111-
// String union type of keys of T that start with on, stripped from on
108+
// String union type of keys of T that start with on, stripped of 'on'
112109
type OnKeys<T> = keyof {
113110
[K in keyof T as K extends `on${infer Rest}` ? Uncapitalize<Rest> : never]: T[K];
114111
};
115112

116-
type EventName = StringWithAutoComplete<
113+
// TS autocomplete trick
114+
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
115+
type EventName =
117116
| OnKeys<ViewProps>
118117
| OnKeys<TextProps>
119118
| OnKeys<TextInputProps>
120119
| OnKeys<PressableProps>
121120
| OnKeys<ScrollViewProps>
122-
>;
121+
| (string & {});
123122

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

src/queries/role.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2+
import type { AccessibilityRole, Role } from 'react-native';
23
import {
34
accessibilityStateKeys,
45
accessiblityValueKeys,
@@ -28,6 +29,10 @@ import type {
2829
} from './make-queries';
2930
import { CommonQueryOptions } from './options';
3031

32+
// TS autocomplete trick
33+
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
34+
export type RoleMatcher = AccessibilityRole | Role | (string & {}) | RegExp;
35+
3136
type ByRoleOptions = CommonQueryOptions &
3237
AccessibilityStateMatcher & {
3338
name?: TextMatch;
@@ -52,7 +57,7 @@ const matchAccessibilityValueIfNeeded = (
5257
return value != null ? matchAccessibilityValue(node, value) : true;
5358
};
5459

55-
const queryAllByRole = (instance: ReactTestInstance): QueryAllByQuery<TextMatch, ByRoleOptions> =>
60+
const queryAllByRole = (instance: ReactTestInstance): QueryAllByQuery<RoleMatcher, ByRoleOptions> =>
5661
function queryAllByRoleFn(role, options) {
5762
return findAll(
5863
instance,
@@ -102,12 +107,12 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
102107
);
103108

104109
export type ByRoleQueries = {
105-
getByRole: GetByQuery<TextMatch, ByRoleOptions>;
106-
getAllByRole: GetAllByQuery<TextMatch, ByRoleOptions>;
107-
queryByRole: QueryByQuery<TextMatch, ByRoleOptions>;
108-
queryAllByRole: QueryAllByQuery<TextMatch, ByRoleOptions>;
109-
findByRole: FindByQuery<TextMatch, ByRoleOptions>;
110-
findAllByRole: FindAllByQuery<TextMatch, ByRoleOptions>;
110+
getByRole: GetByQuery<RoleMatcher, ByRoleOptions>;
111+
getAllByRole: GetAllByQuery<RoleMatcher, ByRoleOptions>;
112+
queryByRole: QueryByQuery<RoleMatcher, ByRoleOptions>;
113+
queryAllByRole: QueryAllByQuery<RoleMatcher, ByRoleOptions>;
114+
findByRole: FindByQuery<RoleMatcher, ByRoleOptions>;
115+
findAllByRole: FindAllByQuery<RoleMatcher, ByRoleOptions>;
111116
};
112117

113118
export const bindByRoleQueries = (instance: ReactTestInstance): ByRoleQueries => ({

0 commit comments

Comments
 (0)