From 51024e8ea1ac1d93a60cb376d9d5a029ccf55929 Mon Sep 17 00:00:00 2001 From: jinhyuk9714 Date: Sun, 17 May 2026 16:59:39 +0900 Subject: [PATCH] fix: support accessibilityLabelledBy arrays --- src/helpers/accessibility.ts | 3 ++- src/queries/__tests__/role.test.tsx | 34 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/helpers/accessibility.ts b/src/helpers/accessibility.ts index de8fab7bf..b69ee06b9 100644 --- a/src/helpers/accessibility.ts +++ b/src/helpers/accessibility.ts @@ -153,10 +153,11 @@ export function computeAriaLabel(instance: TestInstance): string | undefined { const labelElementId = instance.props['aria-labelledby'] ?? instance.props.accessibilityLabelledBy; if (labelElementId) { + const labelElementIds = Array.isArray(labelElementId) ? labelElementId : [labelElementId]; const container = getContainerInstance(instance); const labelInstance = findAll( container, - (node) => isTestInstance(node) && node.props.nativeID === labelElementId, + (node) => isTestInstance(node) && labelElementIds.includes(node.props.nativeID), { includeHiddenElements: true }, ); if (labelInstance.length > 0) { diff --git a/src/queries/__tests__/role.test.tsx b/src/queries/__tests__/role.test.tsx index c07c4c92a..e6c120765 100644 --- a/src/queries/__tests__/role.test.tsx +++ b/src/queries/__tests__/role.test.tsx @@ -195,6 +195,40 @@ describe('supports name option', () => { expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button'); }); + test('returns an element that has the corresponding role and a matching accessibilityLabelledBy', async () => { + await render( + <> + Save + + , + ); + + // assert on the testId to be sure that the returned element is the one with the accessibilityRole + expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button'); + }); + + test('returns an element that has the corresponding role and a matching accessibilityLabelledBy array', async () => { + await render( + <> + Save + + , + ); + + // assert on the testId to be sure that the returned element is the one with the accessibilityRole + expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button'); + }); + test('returns an element when the direct child is text', async () => { await render(