Skip to content

Feature: improved findAll method #1181

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

Closed
mdjastrzebski opened this issue Oct 17, 2022 · 4 comments · Fixed by #1222
Closed

Feature: improved findAll method #1181

mdjastrzebski opened this issue Oct 17, 2022 · 4 comments · Fixed by #1222
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@mdjastrzebski
Copy link
Member

mdjastrzebski commented Oct 17, 2022

Describe the Feature

We currently use ReactTestInstance.findAll method in order to traverse element tree. This is a basic tree walking algorithm that starts at the root and traverses recursively to the children.

There is one major issue with the current method: for query predicates that inspect all descendants (e.g. concat all string content) there is no easy way to find only the deepest element matching given predicate.

<Text testID="outer">
  <Text testID="inner">
    Hello.
  </Text>
</Text>

When using a predict that will concat all nested string contents, then both outer and inner text will be matched.

Possible Implementations

This should be a function, with similar signature:

function findAll(element: ReactTestInstance, predicate: (element: ReactTestInstance) => boolean, options: FindAllOptions)
  • it should work by traversing tree top-down from root (as current algorithm)
  • but it should be inspecting the elements on its way back, so that the deepest element is matched first
  • having information about deeper element having been matched inside given branch it would prevent it's ancestor elements also getting match
  • this should be an optional behavior controlled by option passed inside options argument. Proposed initial option name: deepestOnly (default false)
  • it might be worth considering adding some additional argument to predicate callback to facilitate this

Applications

  • This would be an internal API used by queries.
  • It could allow us to improve some existing non-trivial queries, e.g. *ByText or *ByRole with name option
@mdjastrzebski mdjastrzebski changed the title Improved findAll method Feature: improved findAll method Oct 17, 2022
@mdjastrzebski mdjastrzebski added help wanted Extra attention is needed good first issue Good for newcomers and removed feature request labels Oct 17, 2022
@rparthas
Copy link

I am interested to work on it.

@mdjastrzebski
Copy link
Member Author

Go ahead @rparthas

@mdjastrzebski
Copy link
Member Author

@rparthas I wanted to check if you still plan to contribute this feature?

@rparthas
Copy link

rparthas commented Nov 2, 2022

Yes I intend to. Got bit sidetracked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants