test(matchers/to-have-accessible-name): validate accessible name matcher checks element accessibility labels

This commit is contained in:
Maciej Jastrzębski
2026-01-08 23:55:50 +01:00
parent 91f1deff90
commit b07e7c9255
2 changed files with 25 additions and 0 deletions
@@ -0,0 +1,23 @@
import * as React from 'react';
import { Text, View } from 'react-native';
import { render, screen } from '../..';
it('checks if element has accessible name', async () => {
const Component = () => (
<View>
<Text testID="labeled-text" accessibilityLabel="Submit button">
Click me
</Text>
<Text testID="unlabeled-text">Plain text</Text>
</View>
);
await render(<Component />);
const labeledElement = screen.getByTestId('labeled-text');
const unlabeledElement = screen.getByTestId('unlabeled-text');
expect(labeledElement).toHaveAccessibleName('Submit button');
expect(unlabeledElement).toHaveAccessibleName();
});
+2
View File
@@ -7,6 +7,7 @@ test(queries/label-text): Validates getByLabelText finds elements by their acces
test(queries/hint-text): Validates getByHintText finds elements by their accessibility hint with exact matching - critical for testing accessibility where hints provide additional context to screen readers. Coverage: 97.93% statements (was 93.81%), 83.33% branches, 66.66% functions for hint-text.ts. Covered getNodeByHintText function with exact option and query logic. Uncovered: error message functions (getMultipleError, getMissingError).
test(queries/test-id): Validates getByTestId with exact:false option for partial testID matching - critical for testing components with dynamic or prefixed testIDs. Coverage: 96.42% statements (was 89.28%), 83.33% branches, 66.66% functions for test-id.ts. Covered matchTestId function with exact option and queryAllByTestId implementation. Uncovered: error message functions (getMultipleError, getMissingError).
test(wait-for): Validates waitFor timeout behavior when expectation never passes - critical error handling users depend on when async conditions fail. Coverage: 55% statements (was 12%), 43.47% branches, 85.71% functions for wait-for.ts. Covered timeout error path and lastError handling. Uncovered: fake timers path, promise-returning expectations, onTimeout callback, timer switching errors.
test(wait-for): Validates waitFor resolves when expectation returns a promise that resolves - critical feature users depend on for async expectations that return promises. Coverage: 64% statements (was 55%), 64.28% branches (was 43.47%), 100% functions for wait-for.ts. Covered promise-returning expectation path, promise resolution handling. Uncovered: fake timers path, onTimeout callback, timer switching errors, type checking.
test(wait-for-element-to-be-removed): Validates error when element is already removed at call time - critical error handling users depend on when misusing the API. Coverage: 61.9% statements (was 9.52%), 66.66% branches, 100% functions for wait-for-element-to-be-removed.ts. Covered isRemoved helper and initial validation error path. Uncovered: wait logic (lines 27-42).
test(render-hook): Validates renderHook with initial props and rerender functionality - critical API users depend on for testing React hooks with props. Coverage: 100% statements (was 5.66%), 85.71% branches, 100% functions for render-hook.tsx. Covered hook rendering, initialProps, rerender with new props. Uncovered: line 41 (options destructuring edge case).
test(fire-event): Validates fireEvent.press invokes event handler on element - critical API users depend on for testing user interactions. Coverage: 77.27% statements (was 33.33%), 53.33% branches, 87.5% functions for fire-event.ts. Covered main fireEvent function, findEventHandler traversal, handler invocation. Uncovered: isTouchResponder, isEventEnabled edge cases, setNativeStateIfNeeded, tryGetContentOffset.
@@ -16,3 +17,4 @@ test(matchers/to-be-disabled): Validates toBeDisabled and toBeEnabled matchers c
test(matchers/to-contain-element): Validates toContainElement matcher checks if container element contains child element - critical matcher users depend on for testing component hierarchies and element relationships. Coverage: 75.67% statements (was 21.62%), 100% branches, 50% functions for to-contain-element.ts. Covered main matcher function, element containment check via queryAll. Uncovered: error message formatting (lines 27-35).
test(user-event/clear): Validates userEvent.clear() clears text from TextInput - critical API users depend on for clearing form inputs before typing new values. Coverage: 86.27% statements (was 21.56%), 60% branches, 100% functions for clear.ts. Covered main clear function, focus/select/backspace/blur sequence. Uncovered: error handling for non-TextInput elements (lines 14-18), early return for non-editable/disabled inputs (lines 21-22).
test(user-event/paste): Validates userEvent.paste() pastes text into TextInput replacing existing content - critical API users depend on for testing paste interactions in forms. Coverage: 79.41% statements (was 14.7%), 50% branches, 100% functions for paste.ts. Covered main paste function, focus/select/paste/blur sequence, text replacement. Uncovered: error handling for non-TextInput elements (lines 18-22), early return for non-editable/disabled inputs (lines 25-26), multiline contentSizeChange event (lines 56-62).
test(matchers/to-have-accessible-name): Validates toHaveAccessibleName matcher checks if element has accessible name via accessibilityLabel - critical matcher users depend on for testing accessibility where accessible names identify elements for screen readers. Coverage: 77.27% statements (was 20.45%), 40% branches, 50% functions for to-have-accessible-name.ts. Covered main matcher function, computeAccessibleName call, name matching with expected value, presence check without expected value. Uncovered: error message formatting (lines 33-42).