Skip to content
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

Query by role and accessibilityState and improve ByRole query errors #1161

Merged

Conversation

AugustinLF
Copy link
Collaborator

Summary

RTL does have some form of validation to make sure that we don't use incompatible combinations (roles/states that don't make sense, like a checked button), but I'm not sure about that, and it is out of scope, we can create an additional issue if we feel like it.

Closes #1134

src/queries/role.ts Outdated Show resolved Hide resolved
src/queries/role.ts Outdated Show resolved Hide resolved
src/queries/role.ts Outdated Show resolved Hide resolved
Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kudos for implementing this feature @AugustinLF

  1. This implementation should automatically support upcoming aria-states props, when checked on host components, as they are taken into consideration when passing them from exported composite component. See here, not sure if these are present in the current released version of RN.

  2. It seems that some components take into account other props as well, when calcualting accessibilityState that gets passed to host component, e.g. TouchableOpacity also checks disabled prop. The current implementation already handles that but we might add some tests to verify it.

src/queries/role.ts Outdated Show resolved Hide resolved
src/queries/role.ts Outdated Show resolved Hide resolved
src/queries/role.ts Outdated Show resolved Hide resolved
expect(queryByRole('button', { disabled: false })).toBe(null);
});

test('returns elements using the built-in disabled prop', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify this test to only contain a single TouchableOpacity with disabled={true}?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those three RN component use different implementation behind the hood, so my goal was to test all of them (TouchableOpacity and TouchableHighlight extend TouchableWithoutFeedback), and I felt like covering our bases.

On the other hand I do see your point, since we're just testing RN internal implementation.

I'll keep the three ones, if you feel strongly about that (or splitting it in three tests) let me know).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to test all three TouchablesXxx and I would also metion Pressable in that context. Maybe you can use test.each to automate testing over these component variants?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, but the difference in types (Button doesn't use children), it leads to pretty ugly code, and I'd say that it's not worth it, it'll make the tests harder to read, and here it's fairly simple.

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall structure looks correct now 💯

I've added some comments related to various implementation details to polish things out.

We also need to have our documentation updated to account for new options.

I've logged a separate #1165 to fix make *ByA11yStates to have the same matching behaviour as in this PR.

src/queries/role.ts Outdated Show resolved Hide resolved
@AugustinLF
Copy link
Collaborator Author

@mdjastrzebski should have addressed all your comments I believe!

@mdjastrzebski
Copy link
Member

@AugustinLF I'll need pause a couple of days on open source work as I've got cold-like symptoms. I'll review the PRs as soon as I get better.

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done a final round of review. Overall looks very good for me:

  • All the logic seems correct
  • we have a comprehensive set of tests

I've left two readability/style final comments & requested to document behaviour around defaulting or not to false for each prop.

I didn't give approve, only because as I worry that @thymikee could merge it before addressing these ;-)

src/queries/role.ts Outdated Show resolved Hide resolved
src/queries/role.ts Outdated Show resolved Hide resolved
website/docs/Queries.md Outdated Show resolved Hide resolved
@AugustinLF
Copy link
Collaborator Author

@mdjastrzebski a bit delayed, but everything should have been addressed now :)

@AugustinLF AugustinLF force-pushed the feat/by-role-accessibilityStates-support branch from 2f11472 to 3e01734 Compare October 19, 2022 14:12
@AugustinLF
Copy link
Collaborator Author

Also added typedefs following the changes in #1175, see 3e01734

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @AugustinLF!

*ByRole gets really powerful these days: first name, and now a11y states

@mdjastrzebski
Copy link
Member

Let's merge it!

@mdjastrzebski mdjastrzebski merged commit 7ae763f into callstack:main Oct 20, 2022
@AugustinLF
Copy link
Collaborator Author

Thanks for the help! When are we shipping a new release, we've had at least two improvements to getByRole in since 11.2 👀

@AugustinLF AugustinLF deleted the feat/by-role-accessibilityStates-support branch October 20, 2022 11:30
@mdjastrzebski
Copy link
Member

I could do a release on Friday if I will have enough time

@mdjastrzebski
Copy link
Member

@AugustinLF we've published 11.3.0 a couple of days ago, but I just created a release docs in GH: https://github.com/callstack/react-native-testing-library/releases/tag/v11.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Supports accessibilityStates in ByRole queries
2 participants