Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: by role ts hints #1596

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["Pressable", "RNTL"]
"cSpell.words": ["Pressable", "RNTL", "Uncapitalize"]
}
20 changes: 9 additions & 11 deletions src/fire-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isHostElement } from './helpers/component-tree';
import { isHostTextInput } from './helpers/host-component-names';
import { isPointerEventEnabled } from './helpers/pointer-events';
import { isTextInputEditable } from './helpers/text-input';
import { StringWithAutocomplete } from './types';

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

Expand Down Expand Up @@ -105,20 +106,17 @@ 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> = T | (string & Record<never, never>);

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

type EventName = StringWithAutoComplete<
| OnKeys<ViewProps>
| OnKeys<TextProps>
| OnKeys<TextInputProps>
| OnKeys<PressableProps>
| OnKeys<ScrollViewProps>
type EventName = StringWithAutocomplete<
| EventNameExtractor<ViewProps>
| EventNameExtractor<TextProps>
| EventNameExtractor<TextInputProps>
| EventNameExtractor<PressableProps>
| EventNameExtractor<ScrollViewProps>
>;

function fireEvent(element: ReactTestInstance, eventName: EventName, ...data: unknown[]) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/__tests__/accessiblity.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { View, Text, TextInput, Pressable, Switch, TouchableOpacity } from 'react-native';
import { render, isHiddenFromAccessibility, isInaccessible, screen } from '../..';
import { isAccessibilityElement } from '../accessiblity';
import { isAccessibilityElement } from '../accessibility';

describe('isHiddenFromAccessibility', () => {
test('returns false for accessible elements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const accessibilityStateKeys: (keyof AccessibilityState)[] = [
'expanded',
];

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

export function isHiddenFromAccessibility(
element: ReactTestInstance | null,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/find-all.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactTestInstance } from 'react-test-renderer';
import { getConfig } from '../config';
import { isHiddenFromAccessibility } from './accessiblity';
import { isHiddenFromAccessibility } from './accessibility';
import { HostTestInstance, isHostElement } from './component-tree';

interface FindAllOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/matchers/match-accessibility-state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AccessibilityState } from 'react-native';
import { ReactTestInstance } from 'react-test-renderer';
import { accessibilityStateKeys, getAccessibilityState } from '../accessiblity';
import { accessibilityStateKeys, getAccessibilityState } from '../accessibility';

// This type is the same as AccessibilityState from `react-native` package
// It is re-declared here due to issues with migration from `@types/react-native` to
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/matchers/match-accessibility-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactTestInstance } from 'react-test-renderer';
import { getAccessibilityValue } from '../accessiblity';
import { getAccessibilityValue } from '../accessibility';
import { TextMatch } from '../../matches';
import { matchStringProp } from './match-string-prop';

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/matchers/match-label-text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactTestInstance } from 'react-test-renderer';
import { matches, TextMatch, TextMatchOptions } from '../../matches';
import { getAccessibilityLabel, getAccessibilityLabelledBy } from '../accessiblity';
import { getAccessibilityLabel, getAccessibilityLabelledBy } from '../accessibility';
import { findAll } from '../find-all';
import { matchTextContent } from './match-text-content';

Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-busy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { isElementBusy } from '../helpers/accessiblity';
import { isElementBusy } from '../helpers/accessibility';
import { checkHostElement, formatElement } from './utils';

export function toBeBusy(this: jest.MatcherContext, element: ReactTestInstance) {
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-checked.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getAccessibilityCheckedState,
getAccessibilityRole,
isAccessibilityElement,
} from '../helpers/accessiblity';
} from '../helpers/accessibility';
import { ErrorWithStack } from '../helpers/errors';
import { checkHostElement, formatElement } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-collapsed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { isElementCollapsed } from '../helpers/accessiblity';
import { isElementCollapsed } from '../helpers/accessibility';
import { checkHostElement, formatElement } from './utils';

export function toBeCollapsed(this: jest.MatcherContext, element: ReactTestInstance) {
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-expanded.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { isElementExpanded } from '../helpers/accessiblity';
import { isElementExpanded } from '../helpers/accessibility';
import { checkHostElement, formatElement } from './utils';

export function toBeExpanded(this: jest.MatcherContext, element: ReactTestInstance) {
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-partially-checked.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getAccessibilityCheckedState,
getAccessibilityRole,
isAccessibilityElement,
} from '../helpers/accessiblity';
} from '../helpers/accessibility';
import { ErrorWithStack } from '../helpers/errors';
import { checkHostElement, formatElement } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-selected.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { isElementSelected } from '../helpers/accessiblity';
import { isElementSelected } from '../helpers/accessibility';
import { checkHostElement, formatElement } from './utils';

export function toBeSelected(this: jest.MatcherContext, element: ReactTestInstance) {
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-be-visible.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { StyleSheet } from 'react-native';
import { isHiddenFromAccessibility } from '../helpers/accessiblity';
import { isHiddenFromAccessibility } from '../helpers/accessibility';
import { getHostParent } from '../helpers/component-tree';
import { isHostModal } from '../helpers/host-component-names';
import { checkHostElement, formatElement } from './utils';
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-have-accessibility-value.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint, stringify } from 'jest-matcher-utils';
import { getAccessibilityValue } from '../helpers/accessiblity';
import { getAccessibilityValue } from '../helpers/accessibility';
import {
AccessibilityValueMatcher,
matchAccessibilityValue,
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/to-have-accessible-name.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { getAccessibleName } from '../helpers/accessiblity';
import { getAccessibleName } from '../helpers/accessibility';
import { TextMatch, TextMatchOptions, matches } from '../matches';
import { checkHostElement, formatMessage } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { default as waitForElementToBeRemoved } from './wait-for-element-to-be-r
export { within, getQueriesForElement } from './within';

export { configure, resetToDefaults } from './config';
export { isHiddenFromAccessibility, isInaccessible } from './helpers/accessiblity';
export { isHiddenFromAccessibility, isInaccessible } from './helpers/accessibility';
export { getDefaultNormalizer } from './matches';
export { renderHook } from './render-hook';
export { screen } from './screen';
Expand Down
2 changes: 1 addition & 1 deletion src/queries/accessibility-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { accessibilityStateKeys } from '../helpers/accessiblity';
import { accessibilityStateKeys } from '../helpers/accessibility';
import { deprecateQueries } from '../helpers/deprecation';
import { findAll } from '../helpers/find-all';
import {
Expand Down
4 changes: 2 additions & 2 deletions src/queries/accessibility-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { accessiblityValueKeys } from '../helpers/accessiblity';
import { accessibilityValueKeys } from '../helpers/accessibility';
import { deprecateQueries } from '../helpers/deprecation';
import { findAll } from '../helpers/find-all';
import {
Expand Down Expand Up @@ -27,7 +27,7 @@ const queryAllByA11yValue = (
const formatQueryParams = (matcher: AccessibilityValueMatcher) => {
const params: string[] = [];

accessiblityValueKeys.forEach((valueKey) => {
accessibilityValueKeys.forEach((valueKey) => {
if (matcher[valueKey] !== undefined) {
params.push(`${valueKey} value: ${matcher[valueKey]}`);
}
Expand Down
28 changes: 17 additions & 11 deletions src/queries/role.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ReactTestInstance } from 'react-test-renderer';
import type { AccessibilityRole, Role } from 'react-native';
import {
accessibilityStateKeys,
accessiblityValueKeys,
accessibilityValueKeys,
getAccessibilityRole,
isAccessibilityElement,
} from '../helpers/accessiblity';
} from '../helpers/accessibility';
import { findAll } from '../helpers/find-all';
import {
AccessibilityStateMatcher,
Expand All @@ -16,6 +17,7 @@ import {
} from '../helpers/matchers/match-accessibility-value';
import { matchStringProp } from '../helpers/matchers/match-string-prop';
import type { TextMatch } from '../matches';
import { StringWithAutocomplete } from '../types';
import { getQueriesForElement } from '../within';
import { makeQueries } from './make-queries';
import type {
Expand All @@ -28,7 +30,9 @@ import type {
} from './make-queries';
import { CommonQueryOptions } from './options';

type ByRoleOptions = CommonQueryOptions &
export type ByRoleMatcher = StringWithAutocomplete<AccessibilityRole | Role> | RegExp;

export type ByRoleOptions = CommonQueryOptions &
AccessibilityStateMatcher & {
name?: TextMatch;
value?: AccessibilityValueMatcher;
Expand All @@ -52,7 +56,9 @@ const matchAccessibilityValueIfNeeded = (
return value != null ? matchAccessibilityValue(node, value) : true;
};

const queryAllByRole = (instance: ReactTestInstance): QueryAllByQuery<TextMatch, ByRoleOptions> =>
const queryAllByRole = (
instance: ReactTestInstance,
): QueryAllByQuery<ByRoleMatcher, ByRoleOptions> =>
function queryAllByRoleFn(role, options) {
return findAll(
instance,
Expand Down Expand Up @@ -80,7 +86,7 @@ const formatQueryParams = (role: TextMatch, options: ByRoleOptions = {}) => {
}
});

accessiblityValueKeys.forEach((valueKey) => {
accessibilityValueKeys.forEach((valueKey) => {
if (options?.value?.[valueKey] !== undefined) {
params.push(`${valueKey} value: ${options?.value?.[valueKey]}`);
}
Expand All @@ -102,12 +108,12 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
);

export type ByRoleQueries = {
getByRole: GetByQuery<TextMatch, ByRoleOptions>;
getAllByRole: GetAllByQuery<TextMatch, ByRoleOptions>;
queryByRole: QueryByQuery<TextMatch, ByRoleOptions>;
queryAllByRole: QueryAllByQuery<TextMatch, ByRoleOptions>;
findByRole: FindByQuery<TextMatch, ByRoleOptions>;
findAllByRole: FindAllByQuery<TextMatch, ByRoleOptions>;
getByRole: GetByQuery<ByRoleMatcher, ByRoleOptions>;
getAllByRole: GetAllByQuery<ByRoleMatcher, ByRoleOptions>;
queryByRole: QueryByQuery<ByRoleMatcher, ByRoleOptions>;
queryAllByRole: QueryAllByQuery<ByRoleMatcher, ByRoleOptions>;
findByRole: FindByQuery<ByRoleMatcher, ByRoleOptions>;
findAllByRole: FindAllByQuery<ByRoleMatcher, ByRoleOptions>;
};

export const bindByRoleQueries = (instance: ReactTestInstance): ByRoleQueries => ({
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TS autocomplete trick
// Ref: https://github.com/microsoft/TypeScript/issues/29729#issuecomment-567871939
export type StringWithAutocomplete<T> = T | (string & {});
Loading