|
1 | 1 | import type { ReactTestInstance } from 'react-test-renderer';
|
2 | 2 | import { matcherHint } from 'jest-matcher-utils';
|
3 |
| -import { matchAccessibleName } from '../helpers/matchers/accessibilityName'; |
4 |
| -import { |
5 |
| - getAccessibilityLabel, |
6 |
| - getAccessibilityLabelledBy, |
7 |
| -} from '../helpers/accessiblity'; |
8 |
| -import { TextMatch, TextMatchOptions } from '../matches'; |
9 |
| -import { getTextContent } from '../helpers/text-content'; |
10 |
| -import { getUnsafeRootElement } from '../helpers/component-tree'; |
| 3 | +import { TextMatch, TextMatchOptions, matches } from '../matches'; |
| 4 | +import { getAccessibleName } from '../helpers/accessiblity'; |
11 | 5 | import { checkHostElement, formatMessage } from './utils';
|
12 | 6 |
|
13 |
| -export function getAccessibleName( |
14 |
| - element: ReactTestInstance |
15 |
| -): string | undefined { |
16 |
| - const labelTextFromLabel = getAccessibilityLabel(element); |
17 |
| - const labelTextFromLabelledBy = getAccessibilityLabelledBy(element); |
18 |
| - const rootElement = getUnsafeRootElement(element); |
19 |
| - |
20 |
| - const labelledByElement = labelTextFromLabelledBy |
21 |
| - ? rootElement?.findByProps({ |
22 |
| - nativeID: labelTextFromLabelledBy, |
23 |
| - }) |
24 |
| - : undefined; |
| 7 | +export function matchAccessibleName( |
| 8 | + node: ReactTestInstance, |
| 9 | + expectedName?: TextMatch, |
| 10 | + normalizer?: TextMatchOptions['normalizer'], |
| 11 | + exact?: TextMatchOptions['exact'] |
| 12 | +): boolean { |
| 13 | + const accessibleName = getAccessibleName(node); |
25 | 14 |
|
26 |
| - const nameFromLabel = labelTextFromLabelledBy |
27 |
| - ? labelledByElement && getTextContent(labelledByElement) |
28 |
| - : labelTextFromLabel; |
| 15 | + if (expectedName) { |
| 16 | + return matches(expectedName, accessibleName, normalizer, exact); |
| 17 | + } |
29 | 18 |
|
30 |
| - return nameFromLabel || getTextContent(element) || undefined; |
| 19 | + return !!accessibleName; |
31 | 20 | }
|
32 | 21 |
|
33 | 22 | export function toHaveAccessibleName(
|
|
0 commit comments