Skip to content

Commit a90964f

Browse files
committed
fix: accessible name calculation
1 parent 7ce8b09 commit a90964f

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/helpers/__tests__/accessibility.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,14 @@ describe('computeAccessibleName', () => {
864864
);
865865
});
866866

867+
test('concatenates inline text children without extra spaces', async () => {
868+
const user = 'admin';
869+
870+
await render(<Text testID="subject">Welcome {user}!</Text>);
871+
872+
expect(computeAccessibleName(screen.getByTestId('subject'))).toBe('Welcome admin!');
873+
});
874+
867875
test('TextInput placeholder is used only for the element itself', async () => {
868876
await render(
869877
<>

src/helpers/accessibility.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ export function computeAccessibleName(
295295
}
296296
}
297297

298-
return parts.join(' ');
298+
// Text children are already part of one inline phrase and contain their own spacing.
299+
// Other elements contribute separate accessible names, so separate them with spaces.
300+
const separator = isHostText(instance) ? '' : ' ';
301+
return parts.join(separator);
299302
}
300303

301304
type RoleSupportMap = Partial<Record<Role | AccessibilityRole, true>>;

src/queries/__tests__/role.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ describe('supports name option', () => {
207207
expect(screen.getByRole('header', { name: 'About' }).props.testID).toBe('target-header');
208208
});
209209

210+
test('returns an element when inline text children form the name', async () => {
211+
const user = 'admin';
212+
213+
await render(
214+
<Text accessibilityRole="header" testID="target-header">
215+
Welcome {user}!
216+
</Text>,
217+
);
218+
219+
expect(screen.getByRole('header', { name: 'Welcome admin!' })).toBe(
220+
screen.getByTestId('target-header'),
221+
);
222+
});
223+
210224
test('returns an element with nested Text as children', async () => {
211225
await render(
212226
<Text accessibilityRole="header" testID="parent">

0 commit comments

Comments
 (0)