Skip to content

Commit 37be232

Browse files
committed
refactor: self code review
1 parent d849657 commit 37be232

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

src/helpers/__tests__/component-tree.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,4 @@ describe('getUnsafeRootElement()', () => {
228228
const view = screen.getByTestId('view');
229229
expect(getUnsafeRootElement(view)).toEqual(screen.UNSAFE_root);
230230
});
231-
232-
it('returns null for null', () => {
233-
expect(getUnsafeRootElement(null)).toEqual(null);
234-
});
235231
});

src/helpers/accessibility.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
Role,
66
StyleSheet,
77
} from 'react-native';
8-
import { ReactTestInstance } from 'react-test-renderer';
9-
import { getHostSiblings, getUnsafeRootElement } from './component-tree';
8+
import type { ReactTestInstance } from 'react-test-renderer';
9+
import { getHostSiblings, getUnsafeRootElement, isHostElement } from './component-tree';
10+
import { findAll } from './find-all';
1011
import {
1112
getHostComponentNames,
1213
isHostImage,
@@ -172,9 +173,13 @@ export function computeAriaLabel(element: ReactTestInstance): string | undefined
172173
const labelElementId = element.props['aria-labelledby'] ?? element.props.accessibilityLabelledBy;
173174
if (labelElementId) {
174175
const rootElement = getUnsafeRootElement(element);
175-
const labelElement = rootElement?.findByProps({ nativeID: labelElementId });
176-
if (labelElement) {
177-
return getTextContent(labelElement);
176+
const labelElement = findAll(
177+
rootElement,
178+
(node) => isHostElement(node) && node.props.nativeID === labelElementId,
179+
{ includeHiddenElements: true },
180+
);
181+
if (labelElement.length > 0) {
182+
return getTextContent(labelElement[0]);
178183
}
179184
}
180185

src/helpers/component-tree.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ export function getHostSiblings(element: ReactTestInstance | null): HostTestInst
8787
* @param element The element start traversing from.
8888
* @returns The root element of the tree (host or composite).
8989
*/
90-
export function getUnsafeRootElement(element: ReactTestInstance | null) {
91-
if (element == null) {
92-
return null;
93-
}
94-
90+
export function getUnsafeRootElement(element: ReactTestInstance) {
9591
let current = element;
9692
while (current.parent) {
9793
current = current.parent;

0 commit comments

Comments
 (0)