-
-
Notifications
You must be signed in to change notification settings - Fork 44
fix(getBy*): throw an error if more than one element is found #7
Conversation
BREAKING CHANGE: `queryBy` and `getBy` now throw when multiple elements are returned and prompt users to use other queries if multiple results were intentional. This was done to remain in feature parity with dom-testing-library.
Codecov Report
@@ Coverage Diff @@
## next #7 +/- ##
=====================================
Coverage ? 100%
=====================================
Files ? 23
Lines ? 277
Branches ? 46
=====================================
Hits ? 277
Misses ? 0
Partials ? 0
Continue to review full report at Codecov.
|
const getMultipleError = (c, hint) => | ||
`Found multiple elements with the accessibilityHint of: ${hint}`; | ||
const getMissingError = (c, hint) => | ||
`Unable to find an element with the accessibilityHint of: ${hint}`; |
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.
These error messages are shared across almost everything - consider pulling out some helper so you can just do:
getMissingErrorMessage('accessibilityHint', hint)
For custom ones (like getByText), you can always just opt not to use the helper, or compose the helper with additional text.
Mostly I'm just worried about eventually introducing a typo, especially since the tests only partially check the error message.
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.
Let me think about this one. I’ll definitely fix the other two, my only concern with this one is maintainability (with regards to keeping in parity with dom-testing-library). Whenever there’s a big refactor over there, it might make it hard to pull stuff over here.
Right now, it’s easy to keep up to date with the rest of the family because it’s so similar. I think at some point we’ll have to decide if we wanna split from *-testing-library on code style like this. I don’t think it’s bad so long as the API and feel for users is the same, it just might make it tough to keep up.
🎉 This PR is included in version 3.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This refactors queries to handle some abstraction, and also throws if more than one result is returned from
getBy
,findBy
, andqueryBy
. This is done to maintain compatibility w/ dom-testing-library.This also makes the
value
query better because it limits to only components that have a value prop.For reference: DTL PR