-
Notifications
You must be signed in to change notification settings - Fork 276
feat: isInaccessible
API form RTL/DTL
#1128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9978bfe
feat: isInaccessible API
mdjastrzebski 8ac8ac6
feat: basic implementation & tests
mdjastrzebski 512d794
feature: add more checks & tests
mdjastrzebski a14fd26
feat: support accessibilityViewIsModal prop
mdjastrzebski f761d9c
chore: add component tree tests
mdjastrzebski 1f32dce
refactor: self code review
mdjastrzebski 8036113
docs: improve API formatting
mdjastrzebski 9972cb4
fix: typo
mdjastrzebski a1a3c7b
refactor: self code review
mdjastrzebski 6c33584
Update website/docs/API.md
mdjastrzebski e2c2c82
refactor: remove importantForAccessibility="no" check as incorrect
mdjastrzebski f1153e0
docs: update API doc
mdjastrzebski a6ad21b
docs: slight tweak
mdjastrzebski db9109f
docs: more tweaks
mdjastrzebski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,74 @@ | ||
import React from 'react'; | ||
import { View, Text, TextInput } from 'react-native'; | ||
import { render, isInaccessible } from '../..'; | ||
|
||
test('returns false for accessible elements', () => { | ||
expect( | ||
isInaccessible(render(<View testID="subject" />).getByTestId('subject')) | ||
).toBe(false); | ||
|
||
expect( | ||
isInaccessible( | ||
render(<Text testID="subject">Hello</Text>).getByTestId('subject') | ||
) | ||
).toBe(false); | ||
|
||
expect( | ||
isInaccessible( | ||
render(<TextInput testID="subject" />).getByTestId('subject') | ||
) | ||
).toBe(false); | ||
}); | ||
|
||
test('detects elements with display=none', () => { | ||
const view = render(<View testID="subject" style={{ display: 'none' }} />); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(true); | ||
}); | ||
|
||
test('detects nested elements with display=none', () => { | ||
const view = render( | ||
<View style={{ display: 'none' }}> | ||
<View testID="subject" /> | ||
</View> | ||
); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(true); | ||
}); | ||
|
||
test('detects elements with display=none with complex style', () => { | ||
const view = render( | ||
<View | ||
testID="subject" | ||
style={[{ display: 'flex' }, [{ display: 'flex' }], { display: 'none' }]} | ||
/> | ||
); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(true); | ||
}); | ||
|
||
test('detects elements with opacity=0', () => { | ||
const view = render(<View testID="subject" style={{ opacity: 0 }} />); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(true); | ||
}); | ||
|
||
test('detects nested elements with opacity=0', () => { | ||
const view = render( | ||
<View style={{ opacity: 0 }}> | ||
<View testID="subject" /> | ||
</View> | ||
); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(true); | ||
}); | ||
|
||
test('detects elements with opacity=0 with complex styles', () => { | ||
const view = render( | ||
<View | ||
testID="subject" | ||
style={[[{ opacity: 1 }], { opacity: 1 }, [{ opacity: 0 }]]} | ||
/> | ||
); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(true); | ||
}); | ||
|
||
test('is not trigged by opacity > 0', () => { | ||
const view = render(<View testID="subject" style={{ opacity: 0.0001 }} />); | ||
expect(isInaccessible(view.getByTestId('subject'))).toBe(false); | ||
}); |
This file contains hidden or 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,52 @@ | ||
import { StyleSheet } from 'react-native'; | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
|
||
export function isInaccessible(instance: ReactTestInstance | null): boolean { | ||
if (!instance) { | ||
return true; | ||
} | ||
|
||
// Android: importantForAccessibility | ||
// See: https://reactnative.dev/docs/accessibility#importantforaccessibility-android | ||
if (instance.props.importantForAccessibility === 'no') return true; | ||
|
||
let current: ReactTestInstance | null = instance; | ||
while (current) { | ||
if (isSubtreeInaccessible(current)) { | ||
return true; | ||
} | ||
|
||
current = current.parent; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isSubtreeInaccessible( | ||
instance: ReactTestInstance | null | undefined | ||
): boolean { | ||
if (!instance) { | ||
return true; | ||
} | ||
|
||
// TODO implement iOS: accessibilityViewIsModal | ||
// The hard part is to implement this to look only for host views | ||
// See: https://reactnative.dev/docs/accessibility#accessibilityviewismodal-ios | ||
// if (instance.parent?.children.some((child) => child.accessibilityViewIsModal)) | ||
// return true; | ||
|
||
// iOS: accessibilityElementsHidden | ||
// See: https://reactnative.dev/docs/accessibility#accessibilityelementshidden-ios | ||
if (instance.props.accessibilityElementsHidden) return true; | ||
|
||
// Android: importantForAccessibility | ||
// See: https://reactnative.dev/docs/accessibility#importantforaccessibility-android | ||
if (instance.props.importantForAccessibility === 'no-hide-descendants') | ||
return true; | ||
|
||
const flatStyle = StyleSheet.flatten(instance.props.style) ?? {}; | ||
if (flatStyle.display === 'none') return true; | ||
if (flatStyle.opacity === 0) return true; | ||
mdjastrzebski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return false; | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.