Skip to content

docs: Port 'How Should I Query?' guide from Native Testing Library #555

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 5 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions website/docs/HowShouldIQuery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
id: how-should-i-query
title: How Should I Query?
---

## Priority

Whatever you do, try to make your tests resemble real use cases. You'll only be confident with your tests if they work the way your users do. With that in mind, we recommend this order of priority:

1. **Queries Users Can Interact With**
- [`getByLabelText`](http://localhost:3000/react-native-testing-library/docs/api-queries#bya11ylabel-byaccessibilitylabel-bylabeltext): Your disabled users are counting on you, so this should likely be everywhere for their sake 😉 use it often, and if you aren't, maybe ask yourself why.
- [`getByHintText`](http://localhost:3000/react-native-testing-library/docs/api-queries#bya11yhint-byaccessibilityhint-byhinttext): You should probably have an accessibility label that you can select by, but if for some reason you set this instead, it's safe to use.
- [`getByPlaceholderText`](http://localhost:3000/react-native-testing-library/docs/api-queries#byplaceholdertext): Great for targeting a `TextInput` element to verify its content.
- [`getByText`](http://localhost:3000/react-native-testing-library/docs/api-queries#bytext): Great for finding a `Text`, `Button`, or `Touchable` node. You'll probably use this one a lot.
- [`getByDisplayValue`](http://localhost:3000/react-native-testing-library/docs/api-queries#bydisplayvalue): This is good because a user can see the value they type into a `TextInput` or whether a `Switch` is on or off.
2. **Queries Users Can Interact With**
- [`getByRole`](http://localhost:3000/react-native-testing-library/docs/api-queries#bya11yrole-byaccessibilityrole-byrole): If your app screens have good hierarchy, this may be a decent option for finding your elements.
3. **Queries Users Don't Even Know About**
- [`getByTestId`](http://localhost:3000/react-native-testing-library/docs/api-queries#bytestid): The user cannot see (or hear) these, so this is only recommended for cases where you can't match by text or it doesn't make sense
2 changes: 1 addition & 1 deletion website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
docs: {
Introduction: ['getting-started'],
'API Reference': ['api', 'api-queries'],
Guides: ['migration-v7', 'migration-v2'],
Guides: ['migration-v7', 'migration-v2', 'how-should-i-query'],
Examples: ['react-navigation', 'redux-integration'],
},
};