-
Notifications
You must be signed in to change notification settings - Fork 273
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
byRole queries return only accessibility elements #1244
Conversation
Codecov ReportBase: 94.94% // Head: 95.13% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #1244 +/- ##
==========================================
+ Coverage 94.94% 95.13% +0.18%
==========================================
Files 45 46 +1
Lines 3088 3205 +117
Branches 457 483 +26
==========================================
+ Hits 2932 3049 +117
Misses 156 156
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
0476265
to
6947473
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far looks quite good. Good catch with Switch
component! This PR should use useBreakingChanges
config option so that we will be able to turn it on for vNext.
const element = render( | ||
<View accessible={true} testID="view" /> | ||
).getByTestId('view'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, style: could we separate render
and getByTestId
calls, as it's more readable and matches with what we suggest to our users in docs and examples.
const element = render( | |
<View accessible={true} testID="view" /> | |
).getByTestId('view'); | |
const views = render( | |
<View accessible={true} testID="view" /> | |
) | |
const element = views.getByTestId('view'); |
src/queries/__tests__/role.test.tsx
Outdated
@@ -733,3 +733,12 @@ test('byRole queries support hidden option', () => { | |||
`"Unable to find an element with role: "button""` | |||
); | |||
}); | |||
|
|||
test('takes accessible prop into account', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test('takes accessible prop into account', () => { | |
test('ignores elements with accessible={false}', () => { |
src/queries/__tests__/role.test.tsx
Outdated
@@ -733,3 +733,12 @@ test('byRole queries support hidden option', () => { | |||
`"Unable to find an element with role: "button""` | |||
); | |||
}); | |||
|
|||
test('takes accessible prop into account', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add another test that would pass a View
with accessibilityRole
but without accessible
prop, and check that it does not get matched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
describe('when accessible prop is not defined', () => { | ||
test('returns true for Text component', () => { | ||
const element = render(<Text testID="text">Hey</Text>).getByTestId( | ||
'text' | ||
); | ||
expect(isAccessibilityElement(element)).toEqual(true); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about structuring the tests a bit different form, by grouping default/true/false tests for given component type together. These tests will be both exhaustive and take less LOCs.
describe('when accessible prop is not defined', () => { | |
test('returns true for Text component', () => { | |
const element = render(<Text testID="text">Hey</Text>).getByTestId( | |
'text' | |
); | |
expect(isAccessibilityElement(element)).toEqual(true); | |
}); | |
test('TextInput matching', () => { | |
const { getByTestId } = render(<View> | |
<Text testID="default">Default</Text> | |
<Text testID="true">True</Text> | |
<Text testID="false">False</Text> | |
</View>); | |
expect(isAccessibilityElement(getByTestId("default"))).toBeTrue(); | |
expect(isAccessibilityElement(getByTestId("true"))).toBeTrue(); | |
expect(isAccessibilityElement(getByTestId("false"))).toBeFalse(); | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have such tests for key views that render to host views: View, Text, TextInput, Switch.
Maybe we can also add tests for composite components that render to View with accessible prop: Pressable, TouchableOpacity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea! the only downside of this method is that tests title and tests in general are less explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you prefer? both are fine for me
src/queries/role.ts
Outdated
typeof node.type === 'string' && | ||
isAccessibilityElement(node) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a breaking change so we should apply it only when useBreakingChanges
config is set.
6947473
to
8611599
Compare
@mdjastrzebski thanks for the review! I've fixed everything you mentioned |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good. Let's merge this 🔥
Summary
Solves #1180. It makes byRole queries return only implicit or explicit accessibility elements. Explicit accessibility elements are those with
accessibility
prop set to true. Implicit accessibility elements areText
,TextInput
,Pressable
andSwitch
(tested with VoiceOver forSwitch
to check it was indeed accessible even without setting the accessible prop and I checked through a snapshot, behing the scene, Switch resolves to a host component RCTSwitch).Test plan
isAccessibilityElement
helper