-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: add toBeVisible matcher #73
feat: add toBeVisible matcher #73
Conversation
48b38ae
to
9fd8ec3
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.
Great work! This matcher would be quite useful. I left a few comments to consider.
e7afe06
to
17eaad4
Compare
Thank you very much @dcalhoun for taking a look at this PR 🙇. Following your comments and suggestions, I've applied some updates to the changes. At this point, the PR can be considered as ready to be reviewed in order to include the new |
Looks like most of the unit tests are failing with the following error:
However, I managed to reproduce the same issue by running the tests in the |
17eaad4
to
d81bc01
Compare
@mdjastrzebski @thymikee would either of you be willing to review this work please? Totally understand if you do not have time currently. Just wanted to check in. Thanks! 🙇🏻 |
Hi all! Any update on this? Would be quite useful 🙌 |
I will try to work on that next week |
Don't know if this would be of any help @mdjastrzebski . I'm using this:
|
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.
src/to-be-visible.js
Outdated
} | ||
|
||
function isAttributeVisible(element) { | ||
return element.type !== 'Modal' || element.props.visible !== false; |
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.
Directly checking modal type looks kinda hacky, maybe we could check some accessiblity props here: accessibilityElementsHidden
, etc
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.
does it support bottom sheet from gorhom ?
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.
Hard to say without checking, depends on how well that library supports accessibility props. I think we should first concentrate on supporting built-in components, e.g. built-in Modal
from RN, and handle the external libraries afterwards on a case-by-case basis, as in some cases the proper place to support them will be here, but in other case it might be by submitting the PR to the library authors to improve their accessibility support.
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.
@fluiddot I wonder how Modal
actually performs hidding of its children when visibility=false
. Instead of relying on querying the element for type and visible
prop, we could check the underlying code. Maybe it just does not render the children at all.
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.
Instead of relying on querying the element for type and visible prop, we could check the underlying code. Maybe it just does not render the children at all.
@mdjastrzebski I checked when testing that Modal
component doesn't actually render children, so this matcher will throw an exception when using it for children elements of a not visible modal.
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.
Directly checking modal type looks kinda hacky, maybe we could check some accessiblity props here: accessibilityElementsHidden, etc
@mdjastrzebski good point. To be honest, I added the modal type check as a special case, since it's likely that we want to check the visibility of a modal using this matcher. In any case, I'm open to reconsider this condition in favor of a better approach or even not including it.
Regarding the accessibility props, I went ahead and updated the matcher to also check them in this commit, similar to the logic of isInaccessible
function.
I would be very happy to use this! 👍 |
@fluiddot Do you have capacity to work on updating this PR? @dcalhoun, @Norfeldt & others: maybe you are willing to take over this. re |
Hey @mdjastrzebski, my apologies for not being very active in the PR. I'd like to resume the work on this and review the feedback as soon as possible, but my bandwidth nowadays is usually low. If someone is willing to take over this, please feel to do it. Otherwise, I'll try to make some progress in the next weeks. |
src/__tests__/to-be-visible.js
Outdated
import { render } from '@testing-library/react-native'; | ||
|
||
describe('.toBeVisible', () => { | ||
test.each([ |
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.
Github/link seems to be complaining about missing key
prop, Could you refactor these tests to be regular. not .each
tests for readability.
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.
I fixed this issue when migrating the test file to TypeScript in this commit.
src/to-be-visible.js
Outdated
|
||
function isStyleVisible(element) { | ||
const style = element.props.style || {}; | ||
const { display = 'flex', opacity = 1 } = Array.isArray(style) ? mergeAll(style) : style; |
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.
Use StyleSheet.flatten
to calculate flat style
object.
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 catch, I've applied this suggestion in this commit.
src/to-be-visible.js
Outdated
|
||
function isStyleVisible(element) { | ||
const style = element.props.style || {}; | ||
const { display = 'flex', opacity = 1 } = Array.isArray(style) ? mergeAll(style) : style; |
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.
No need for default values, if we later compare them to known values.
const { display = 'flex', opacity = 1 } = Array.isArray(style) ? mergeAll(style) : style; | |
const { display, opacity } = Array.isArray(style) ? mergeAll(style) : style; |
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.
Agreed, I've applied this suggestion in this commit.
@fluiddot Overall the current implementation looks solid. I think it's sensible approach to base it on Jest DOM matcher code with some tweaks to adjust for different props in RN than in Web. |
# Conflicts: # extend-expect.d.ts # src/extend-expect.ts
@fluiddot would you be able to rebase the code and migrate it to typescript as the current codebase uses it now. |
Co-authored-by: Maciej Jastrzebski <[email protected]>
@mdjastrzebski 😅 Sorry for the long delay on addressing the comments of the PR and migrating the changes to TypeScript. After the recent changes I pushed, the PR should be ready for another review. Let me know if there's anything else I can contribute with in order to conclude the feature. Thank you very much for the help 🙇 . |
Codecov Report
@@ Coverage Diff @@
## main #73 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 8 9 +1
Lines 151 175 +24
Branches 47 57 +10
=========================================
+ Hits 151 175 +24
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@fluiddot looks pretty solid. I think the code is ready, I've added some tweak to code comments. Pls fix code coverage back to 100%. It should be a matter of adding a single test probably. |
Co-authored-by: Maciej Jastrzebski <[email protected]>
Co-authored-by: Maciej Jastrzebski <[email protected]>
Thanks for the review @mdjastrzebski 🙇. I've just applied your suggestions and typo fixes. I'll take a look at the code coverage and try to add another test to provide 100% 👍. |
I added a new test for checking that the matcher throws an error when the expectation is not matched. With this, the coverage should go back to 100% 🎊. |
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.
Looks good! Happy to finally merge it :-) Thank you @fluiddot for this contribution!
🎉 This PR is included in version 5.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Thank you so much @mdjastrzebski for the review and help of this contribution 🙇! Happy to know that it's finally merged 😃! |
What:
Add
toBeVisible
matcher following a similar logic as thejest-dom
matcher (reference).Why:
This matcher provides a useful way to verify that a queried element is actually visible.
How:
The matcher has been introduced based on the implementation of other matchers and trying to reproduce a similar logic as the
jest-dom
matcher (reference), but taking into account the React Native nuances.Checklist:
docs