Skip to content

Feature: improved findAll method #1181

@mdjastrzebski

Description

@mdjastrzebski

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions