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

Fix finding props with testId #357

Merged
merged 2 commits into from
Apr 29, 2022
Merged

Fix finding props with testId #357

merged 2 commits into from
Apr 29, 2022

Conversation

fortmarek
Copy link
Contributor

Description

Shop has had issues with triggering onViewableItemsChanged when getting the FlashList instance via getByTestId from @testing-library/react-native. Triggering flatList.props.onViewableItemsChanged returned undefined here.

The reason for this is that getByTestId does not get you FlashList component directly but whatever it renders as the last component. In our case, it's ScrollComponent (see here).

One possible fix (present in this PR) is to remove onViewableItemsChanged (which was unnecessary) from destructured props and pass it to ProgressiveListView which will in turn pass it to ScrollComponent. I have checked the failing test and now it correctly triggers the method.

Reviewers’ hat-rack 🎩

  • [ ]

Screenshots or videos (if needed)

Checklist

@naqvitalha
Copy link
Collaborator

@fortmarek Does this mean that everything needs to be passed to ScrollComponent? It may not be feasible in cases where we're reimplementing an existing ScrollView prop.

Is it possible to suggest an alternative instead? Can we change the query?

@fortmarek
Copy link
Contributor Author

Can we change the query?

The current behaviour is the expected one, so I don't think we can change the query. This test case inside the react-native-testing-library showcases that only native components are supposed to be returned - which corresponds to my findings where RCTScrollView is returned and FlashList not.

Is it possible to suggest an alternative instead?

I am actually not sure if react-native-testing-library is the right tool for the job here. It's supposed to be used for e2e tests and mimic user behaviour - so of course, you should be able to only interact with native components. If Shop used for those kinds of tests @quilted/react-testing, they could leverage the find method which finds a React element.

I don't like the solution proposed in this PR - although it fixes the issue, we should not have to pass all the props of FlashList to the native view. Instead, it seems to me the test is not written correctly.

@linddominic, what do you think about this?

@linddominic
Copy link

Can we change the query?

The current behaviour is the expected one, so I don't think we can change the query. This test case inside the react-native-testing-library showcases that only native components are supposed to be returned - which corresponds to my findings where RCTScrollView is returned and FlashList not.

Is it possible to suggest an alternative instead?

I am actually not sure if react-native-testing-library is the right tool for the job here. It's supposed to be used for e2e tests and mimic user behaviour - so of course, you should be able to only interact with native components. If Shop used for those kinds of tests @quilted/react-testing, they could leverage the find method which finds a React element.

I don't like the solution proposed in this PR - although it fixes the issue, we should not have to pass all the props of FlashList to the native view. Instead, it seems to me the test is not written correctly.

@linddominic, what do you think about this?

Hm. I don't know if the statement that react-native-testing-library is supposed to be used for e2e and that switching is the full solution.

I could dig into if there is another way to write the same test too see if that particular test in Shop is doing something iffy.

On a higher-level discussion if we want FlashList to be a drop-in replacement to FlashList I feel like this PR should get merged.

@fortmarek
Copy link
Contributor Author

On a higher-level discussion if we want FlashList to be a drop-in replacement to FlashList I feel like this PR should get merged.

This is a good point. We should probably get this merged as FlatList passes all of its props to ScrollView (see here). I will update the PR to also pass other relevant props to ScrollView that are currently excluded.

@naqvitalha
Copy link
Collaborator

This is a good point. We should probably get this merged as FlatList passes all of its props to ScrollView (see here). I will update the PR to also pass other relevant props to ScrollView that are currently excluded.

Is that really a good idea? If you pass all the props things will break for e.g, stickyHeaderIndices. Most of them are deliberate omissions and the remaining ones are already forwarded to ScrollView.

I don't have an issue with just passing onViewableItemsChanged forward but I think this test should be changed anyway. It's okay to make assumptions on implementation details or use private APIs but then such tests need to be updated when things change :)

FlatList can stop forwarding this prop too and I wouldn't call it a breaking change even if some people need to fix their tests. It's going to be extremely difficult to maintain FlashList if we can't change internals when we want to. Honestly, I'd only want to worry about keeping public APIs same.

@fortmarek
Copy link
Contributor Author

fortmarek commented Apr 29, 2022

I don't have an issue with just passing onViewableItemsChanged forward but I think this test should be changed anyway. It's okay to make assumptions on implementation details or use private APIs but then such tests need to be updated when things change :)

I generally agree but at least initially, people will expect FlashList to mostly behave as FlatList does and if we can make a small change to achieve that without any real drawbacks, I think we should go for it. But let's keep the scope limited and just add onViewableItemsChanged which had no good reason to be excluded 👍

FlatList can stop forwarding this prop too and I wouldn't call it a breaking change even if some people need to fix their tests. It's going to be extremely difficult to maintain FlashList if we can't change internals when we want to. Honestly, I'd only want to worry about keeping public APIs same.

I think nobody is advocating for matching the internals completely or treating internals as a public API. We both agree that the way the test is written relies on the internal implementation which you never should do. But as mentioned, in this case we can accommodate Shop team as it should not really impact us in a negative way.

@fortmarek fortmarek marked this pull request as ready for review April 29, 2022 07:53
Copy link
Contributor

@ElviraBurchik ElviraBurchik left a comment

Choose a reason for hiding this comment

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

Based on the discussion regarding this change, I agree that it makes sense to merge it

Copy link
Collaborator

@naqvitalha naqvitalha left a comment

Choose a reason for hiding this comment

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

Looks good, including this has no side effects.

@fortmarek fortmarek merged commit 66d1fd9 into main Apr 29, 2022
@fortmarek fortmarek deleted the fix/test-id branch April 29, 2022 14:24
@fortmarek
Copy link
Contributor Author

@linddominic this has now been released in the 0.5.0 version 🚀

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.

None yet

4 participants