-
-
Notifications
You must be signed in to change notification settings - Fork 44
Impossible to get FlatList element #12
Comments
Hey Ely, thanks for bringing up this issue, and thanks for all the details! Let me start by saying, I appreciate you referencing the guiding principles in your description. I'd say that as far as the current release goes this likely can't be fixed in a way that adheres to them. The reason for this is that the 2.0.0 release uses the That said, the 3.0.0 release that's currently being worked on sort of pivots from the approach of the react-native jest preset. We'll be including our own jest preset that's a superset of the react-native one which mocks the components we actually allow querying and firing elements on. Current state of the release, FlatList still isn't mocked, but I'm open to seeing a PR of what it would look like. The main reason I didn't include it myself is that mocking it to the degree it would need to be in order to do this seemed non-trivial. Long story short, unfortunately this likely can't be fixed well in ~2.0.0, but I'm open to hearing how you believe we can improve it in ~3.0.0. I think I'd also like to think about it some more and hear some more feedback on this use case. |
I just released 3.0.0, and wanted to be sure to follow up with you. I'll start by saying, you'll want to use the new jest preset following the install guide, and make sure you check out the docs all around, quite a bit changed in this release (mostly around making it more like react testing library). I spent a lot of time thinking about this since you opened this issue, and I have a couple of thoughts: First, I can't really think of a reason you'd need to specifically query the actual FlatList. The source of FlatList shows that it passes its props on to VirtualizedList, so the event will bubble and fire on the VirtualizedList, which is where the FlatList events actually happen (not the FlatList itself). In 2.0, it couldn't bubble there properly, which was bad, but now it can with 3.0. Nothing about the FlatList itself really matters in the test, it'll all work out fine even though you can't query it directly. Second, and this is the big one, the major sticking point for me is following the guiding principles of testing library, and given that events can bubble to a valid target, there's really no reason you'd need to add a testID to the FlatList and select it. You could select anything inside the list, then My official recommendation on this issue:
Let me know if you have any other issues, hope you enjoy the 3.0 release |
I'm going to close this now due to inactivity. With the new release, a lot has changed, and I believe this use case is now covered by following things in the posts I made above. Good luck! |
@elyalvarado, have you figured out how to simulate the pull to refresh event? |
Well, what I'm doing is patching And then I can just fire the native events and run my expectations as usual:
Just be aware that if you do this and are doing snapshot testing, then the component name will probably change. |
Even though from some other parts of the code (events) it looks like is possible to get and interact with a FlatList component, it is not possible to select it with any of the queries as it is rendered as a RCTScrollView and is not whitelisted in the defaultFilter
react-native
orexpo
: react-nativenative-testing-library
version: 2.0.0react-native
version: 0.59.5node
version:8.15.1npm
(oryarn
) version: yarn 1.15.2Relevant code or config:
What you did:
Tried to get a FlatList in some tests
What happened:
Got the following error:
Problem description:
This is a native component, and the user can interact with it by scrolling to the end or pulling to refresh
Suggested solution:
I really don't know if this is within the guiding principles, but I think that the user can interact with the FlatList directly because it has specific behaviour that makes it different to other scrollviews in react-native, specifically the onEndReached and onRefresh events.
The text was updated successfully, but these errors were encountered: