Skip to content

Commit 9488dcc

Browse files
feat: by role ts hints (#1596)
* feat: autocomplete * refactor: cleanup * chore: fix typos
1 parent 16b9e49 commit 9488dcc

22 files changed

+49
-42
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: 9 additions & 11 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

@@ -105,20 +106,17 @@ function getEventHandlerName(eventName: string) {
105106
return `on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
106107
}
107108

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
112-
type OnKeys<T> = keyof {
109+
// String union type of keys of T that start with on, stripped of 'on'
110+
type EventNameExtractor<T> = keyof {
113111
[K in keyof T as K extends `on${infer Rest}` ? Uncapitalize<Rest> : never]: T[K];
114112
};
115113

116-
type EventName = StringWithAutoComplete<
117-
| OnKeys<ViewProps>
118-
| OnKeys<TextProps>
119-
| OnKeys<TextInputProps>
120-
| OnKeys<PressableProps>
121-
| OnKeys<ScrollViewProps>
114+
type EventName = StringWithAutocomplete<
115+
| EventNameExtractor<ViewProps>
116+
| EventNameExtractor<TextProps>
117+
| EventNameExtractor<TextInputProps>
118+
| EventNameExtractor<PressableProps>
119+
| EventNameExtractor<ScrollViewProps>
122120
>;
123121

124122
function fireEvent(element: ReactTestInstance, eventName: EventName, ...data: unknown[]) {

src/helpers/__tests__/accessiblity.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { View, Text, TextInput, Pressable, Switch, TouchableOpacity } from 'react-native';
33
import { render, isHiddenFromAccessibility, isInaccessible, screen } from '../..';
4-
import { isAccessibilityElement } from '../accessiblity';
4+
import { isAccessibilityElement } from '../accessibility';
55

66
describe('isHiddenFromAccessibility', () => {
77
test('returns false for accessible elements', () => {

src/helpers/accessiblity.ts renamed to src/helpers/accessibility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const accessibilityStateKeys: (keyof AccessibilityState)[] = [
1616
'expanded',
1717
];
1818

19-
export const accessiblityValueKeys: (keyof AccessibilityValue)[] = ['min', 'max', 'now', 'text'];
19+
export const accessibilityValueKeys: (keyof AccessibilityValue)[] = ['min', 'max', 'now', 'text'];
2020

2121
export function isHiddenFromAccessibility(
2222
element: ReactTestInstance | null,

src/helpers/find-all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { getConfig } from '../config';
3-
import { isHiddenFromAccessibility } from './accessiblity';
3+
import { isHiddenFromAccessibility } from './accessibility';
44
import { HostTestInstance, isHostElement } from './component-tree';
55

66
interface FindAllOptions {

src/helpers/matchers/match-accessibility-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AccessibilityState } from 'react-native';
22
import { ReactTestInstance } from 'react-test-renderer';
3-
import { accessibilityStateKeys, getAccessibilityState } from '../accessiblity';
3+
import { accessibilityStateKeys, getAccessibilityState } from '../accessibility';
44

55
// This type is the same as AccessibilityState from `react-native` package
66
// It is re-declared here due to issues with migration from `@types/react-native` to

src/helpers/matchers/match-accessibility-value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactTestInstance } from 'react-test-renderer';
2-
import { getAccessibilityValue } from '../accessiblity';
2+
import { getAccessibilityValue } from '../accessibility';
33
import { TextMatch } from '../../matches';
44
import { matchStringProp } from './match-string-prop';
55

src/helpers/matchers/match-label-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { matches, TextMatch, TextMatchOptions } from '../../matches';
3-
import { getAccessibilityLabel, getAccessibilityLabelledBy } from '../accessiblity';
3+
import { getAccessibilityLabel, getAccessibilityLabelledBy } from '../accessibility';
44
import { findAll } from '../find-all';
55
import { matchTextContent } from './match-text-content';
66

src/matchers/to-be-busy.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactTestInstance } from 'react-test-renderer';
22
import { matcherHint } from 'jest-matcher-utils';
3-
import { isElementBusy } from '../helpers/accessiblity';
3+
import { isElementBusy } from '../helpers/accessibility';
44
import { checkHostElement, formatElement } from './utils';
55

66
export function toBeBusy(this: jest.MatcherContext, element: ReactTestInstance) {

src/matchers/to-be-checked.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getAccessibilityCheckedState,
55
getAccessibilityRole,
66
isAccessibilityElement,
7-
} from '../helpers/accessiblity';
7+
} from '../helpers/accessibility';
88
import { ErrorWithStack } from '../helpers/errors';
99
import { checkHostElement, formatElement } from './utils';
1010

0 commit comments

Comments
 (0)