-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash from non-adjustable unit RangeCell a11y activation (#30636)
* Avoid invoking possible null callback Previously, the unit picker callback was always invoked when a11y tools activated the element. This caused the app to crash whenever the element did not have adjustable units. * Add test coverage for RangeCell accessibility actions
- Loading branch information
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/components/src/mobile/bottom-sheet/test/range-cell.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { AccessibilityInfo } from 'react-native'; | ||
import { create, act } from 'react-test-renderer'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import RangeCell from '../range-cell'; | ||
|
||
// Avoid errors due to mocked stylesheet files missing required selectors | ||
jest.mock( '@wordpress/compose', () => ( { | ||
...jest.requireActual( '@wordpress/compose' ), | ||
withPreferredColorScheme: jest.fn( ( Component ) => ( props ) => ( | ||
<Component | ||
{ ...props } | ||
preferredColorScheme={ {} } | ||
getStylesFromColorScheme={ jest.fn( () => ( {} ) ) } | ||
/> | ||
) ), | ||
} ) ); | ||
|
||
const isScreenReaderEnabled = Promise.resolve( true ); | ||
beforeAll( () => { | ||
// Mock async native module to avoid act warning | ||
AccessibilityInfo.isScreenReaderEnabled = jest.fn( | ||
() => isScreenReaderEnabled | ||
); | ||
} ); | ||
|
||
it( 'allows modifying units via a11y actions', async () => { | ||
const mockOpenUnitPicker = jest.fn(); | ||
const renderer = create( | ||
<RangeCell | ||
label="Opacity" | ||
minimumValue={ 0 } | ||
maximumValue={ 100 } | ||
value={ 50 } | ||
onChange={ jest.fn() } | ||
openUnitPicker={ mockOpenUnitPicker } | ||
/> | ||
); | ||
// Await async update to component state to avoid act warning | ||
await act( () => isScreenReaderEnabled ); | ||
const { onAccessibilityAction } = renderer.toJSON().props; | ||
|
||
onAccessibilityAction( { nativeEvent: { actionName: 'activate' } } ); | ||
expect( mockOpenUnitPicker ).toHaveBeenCalled(); | ||
} ); | ||
|
||
describe( 'when range lacks an adjustable unit', () => { | ||
it( 'disallows modifying units via a11y actions', async () => { | ||
const renderer = create( | ||
<RangeCell | ||
label="Opacity" | ||
minimumValue={ 0 } | ||
maximumValue={ 100 } | ||
value={ 50 } | ||
onChange={ jest.fn() } | ||
/> | ||
); | ||
// Await async update to component state to avoid act warning | ||
await act( () => isScreenReaderEnabled ); | ||
const { onAccessibilityAction } = renderer.toJSON().props; | ||
|
||
expect( () => | ||
onAccessibilityAction( { nativeEvent: { actionName: 'activate' } } ) | ||
).not.toThrow(); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters