Skip to content

Commit 70dd329

Browse files
committed
feat: support Image alt prop in *ByLabelText
1 parent e61a42a commit 70dd329

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/helpers/accessibility.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ReactTestInstance } from 'react-test-renderer';
99
import { getHostSiblings, getUnsafeRootElement } from './component-tree';
1010
import {
1111
getHostComponentNames,
12+
isHostImage,
1213
isHostSwitch,
1314
isHostText,
1415
isHostTextInput,
@@ -145,6 +146,10 @@ export function computeAriaModal(element: ReactTestInstance): boolean | undefine
145146
}
146147

147148
export function computeAriaLabel(element: ReactTestInstance): string | undefined {
149+
if (isHostImage(element) && element.props.alt) {
150+
return element.props.alt;
151+
}
152+
148153
return element.props['aria-label'] ?? element.props.accessibilityLabel;
149154
}
150155

src/queries/__tests__/label-text.test.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { View, Text, TextInput, Pressable } from 'react-native';
2+
import { View, Text, TextInput, Image, Pressable } from 'react-native';
33
import { render, screen } from '../..';
44

55
const BUTTON_LABEL = 'cool button';
@@ -220,6 +220,18 @@ test('getByLabelText supports nested aria-labelledby', () => {
220220
expect(screen.getByLabelText(/nested text label/i)).toBe(screen.getByTestId('text-input'));
221221
});
222222

223+
test('getByLabelText supports "Image"" with "alt" prop', () => {
224+
render(
225+
<>
226+
<Image alt="Image Label" testID="image" />
227+
</>,
228+
);
229+
230+
const expectedElement = screen.getByTestId('image');
231+
expect(screen.getByLabelText('Image Label')).toBe(expectedElement);
232+
expect(screen.getByLabelText(/image label/i)).toBe(expectedElement);
233+
});
234+
223235
test('error message renders the element tree, preserving only helpful props', async () => {
224236
render(<Pressable accessibilityLabel="LABEL" key="3" />);
225237

0 commit comments

Comments
 (0)