diff --git a/src/components/Chip/Chip.tsx b/src/components/Chip/Chip.tsx index bfdebbaa52..c8265c2575 100644 --- a/src/components/Chip/Chip.tsx +++ b/src/components/Chip/Chip.tsx @@ -223,8 +223,10 @@ const Chip = ({ children, icon, avatar, - selected = false, + selected, disabled = false, + accessibilityState: _accessibilityState, + 'aria-checked': _ariaChecked, background, 'aria-label': ariaLabel, role = 'button', @@ -352,7 +354,11 @@ const Chip = ({ disabled={disabled} aria-label={ariaLabel} role={role} - aria-selected={selected} + {...(Platform.OS === 'web' + ? selected !== undefined + ? { 'aria-pressed': selected } + : undefined + : { 'aria-selected': selected ?? false })} aria-disabled={disabled} testID={testID} theme={theme} diff --git a/src/components/__tests__/Chip.test.tsx b/src/components/__tests__/Chip.test.tsx index cd8562325b..08cc571c49 100644 --- a/src/components/__tests__/Chip.test.tsx +++ b/src/components/__tests__/Chip.test.tsx @@ -1,7 +1,14 @@ -import { Pressable } from 'react-native'; +import { Platform, Pressable } from 'react-native'; import type { GestureResponderEvent } from 'react-native'; -import { describe, expect, it, jest } from '@jest/globals'; +import { + afterEach, + beforeEach, + describe, + expect, + it, + jest, +} from '@jest/globals'; import { userEvent } from '@testing-library/react-native'; import color from 'color'; @@ -362,6 +369,146 @@ describe('getChipColor - border color', () => { }); }); +describe('accessibility', () => { + const originalPlatform = Platform.OS; + + afterEach(() => { + Platform.OS = originalPlatform; + }); + + describe('web', () => { + beforeEach(() => { + Platform.OS = 'web'; + }); + + it('does not expose aria-pressed for an action Chip when selected is omitted', async () => { + await render( {}}>Action Chip); + + const button = screen.getByRole('button'); + expect(button).not.toHaveProp('aria-pressed'); + }); + + it('exposes aria-pressed as true when selected is true', async () => { + await render( + {}} selected={true}> + Selected Chip + + ); + + const button = screen.getByRole('button'); + expect(button).toHaveProp('aria-pressed', true); + }); + + it('exposes aria-pressed as false when selected is explicitly false', async () => { + await render( + {}} selected={false}> + Unselected Chip + + ); + + const button = screen.getByRole('button'); + expect(button).toHaveProp('aria-pressed', false); + }); + + it('removes aria-selected from the interactive button', async () => { + await render( + {}} selected={true}> + Toggle Chip + + ); + + const button = screen.getByRole('button'); + expect(button).not.toHaveProp('aria-selected'); + }); + + it('removes aria-selected when selected is omitted', async () => { + await render( {}}>Action Chip); + + const button = screen.getByRole('button'); + expect(button).not.toHaveProp('aria-selected'); + }); + }); + + describe('native', () => { + it('preserves selected state when selected is true', async () => { + await render( + {}} selected={true}> + Selected Chip + + ); + + const button = screen.getByRole('button'); + expect(button).toHaveProp( + 'accessibilityState', + expect.objectContaining({ selected: true }) + ); + }); + + it('produces native selected state false when selected is explicitly false', async () => { + await render( + {}} selected={false}> + Unselected Chip + + ); + + const button = screen.getByRole('button'); + expect(button).toHaveProp( + 'accessibilityState', + expect.objectContaining({ selected: false }) + ); + }); + + it('produces native selected state false when selected is omitted', async () => { + await render( {}}>Default Chip); + + const button = screen.getByRole('button'); + expect(button).toHaveProp( + 'accessibilityState', + expect.objectContaining({ selected: false }) + ); + }); + }); + + it('does not forward accessibilityState or aria-checked to the outer Surface or interactive TouchableRipple', async () => { + await render( + {}} + testID="chip-touchable" + accessibilityState={{ checked: true, busy: true }} + aria-checked={true} + > + Chip + + ); + + const touchable = screen.getByTestId('chip-touchable'); + const surface = touchable.parent; + + expect(surface).not.toHaveProp('accessibilityState'); + expect(surface).not.toHaveProp('aria-checked'); + + expect(touchable).not.toHaveProp('aria-checked'); + expect(touchable).toHaveProp( + 'accessibilityState', + expect.not.objectContaining({ + checked: expect.anything(), + busy: expect.anything(), + }) + ); + }); + + it('does not forward accessibilityState or aria-checked to the interactive button by default', async () => { + await render( {}}>Default Chip); + + const button = screen.getByRole('button'); + expect(button).not.toHaveProp('aria-checked'); + expect(button).toHaveProp( + 'accessibilityState', + expect.not.objectContaining({ checked: expect.anything() }) + ); + }); +}); + describe('close affordance', () => { it('is not rendered without onClose', async () => { await render( {}}>Example);