Skip to content

Commit

Permalink
feat: toHaveAccessibleName matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
anishamalde authored and mdjastrzebski committed Oct 31, 2023
1 parent 4445c33 commit 6cd8734
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
23 changes: 22 additions & 1 deletion src/helpers/accessiblity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
StyleSheet,
} from 'react-native';
import { ReactTestInstance } from 'react-test-renderer';
import { getHostSiblings } from './component-tree';
import { getTextContent } from './text-content';
import { getHostSiblings, getUnsafeRootElement } from './component-tree';
import { getHostComponentNames } from './host-component-names';

type IsInaccessibleOptions = {
Expand Down Expand Up @@ -233,3 +234,23 @@ export function isElementSelected(
const { accessibilityState, 'aria-selected': ariaSelected } = element.props;
return ariaSelected ?? accessibilityState?.selected ?? false;
}

export function getAccessibleName(
element: ReactTestInstance
): string | undefined {
const labelTextFromLabel = getAccessibilityLabel(element);
const labelTextFromLabelledBy = getAccessibilityLabelledBy(element);
const rootElement = getUnsafeRootElement(element);

const labelledByElement = labelTextFromLabelledBy
? rootElement?.findByProps({
nativeID: labelTextFromLabelledBy,
})
: undefined;

const nameFromLabel = labelTextFromLabelledBy
? labelledByElement && getTextContent(labelledByElement)
: labelTextFromLabel;

return nameFromLabel || getTextContent(element) || undefined;
}
18 changes: 0 additions & 18 deletions src/helpers/matchers/accessibilityName.ts

This file was deleted.

37 changes: 13 additions & 24 deletions src/matchers/to-have-accessible-name.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { matchAccessibleName } from '../helpers/matchers/accessibilityName';
import {
getAccessibilityLabel,
getAccessibilityLabelledBy,
} from '../helpers/accessiblity';
import { TextMatch, TextMatchOptions } from '../matches';
import { getTextContent } from '../helpers/text-content';
import { getUnsafeRootElement } from '../helpers/component-tree';
import { TextMatch, TextMatchOptions, matches } from '../matches';
import { getAccessibleName } from '../helpers/accessiblity';
import { checkHostElement, formatMessage } from './utils';

export function getAccessibleName(
element: ReactTestInstance
): string | undefined {
const labelTextFromLabel = getAccessibilityLabel(element);
const labelTextFromLabelledBy = getAccessibilityLabelledBy(element);
const rootElement = getUnsafeRootElement(element);

const labelledByElement = labelTextFromLabelledBy
? rootElement?.findByProps({
nativeID: labelTextFromLabelledBy,
})
: undefined;
export function matchAccessibleName(
node: ReactTestInstance,
expectedName?: TextMatch,
normalizer?: TextMatchOptions['normalizer'],
exact?: TextMatchOptions['exact']
): boolean {
const accessibleName = getAccessibleName(node);

const nameFromLabel = labelTextFromLabelledBy
? labelledByElement && getTextContent(labelledByElement)
: labelTextFromLabel;
if (expectedName) {
return matches(expectedName, accessibleName, normalizer, exact);
}

return nameFromLabel || getTextContent(element) || undefined;
return !!accessibleName;
}

export function toHaveAccessibleName(
Expand Down

0 comments on commit 6cd8734

Please sign in to comment.