getAllByTestId function

List getAllByTestId(
  1. dynamic root,
  2. String value, {
  3. String key = defaultTestIdKey,
})

Returns all descendants of root with a key test ID prop value set to a space-delimited string containing value.

This is similar to getByTestId, which returns only the first matching descendant.

Note: when using with components that forward props (like test IDs), this will return both the Dart components and the components they renders, since they will both have the same test ID.

If you want to get only Dart components, use getAllComponentsByTestId.

This method works for:

  • ReactComponent render trees (output of render)
  • ReactElement trees (output of renderShallow/Component.render)

Example:

var renderedInstance = render(Dom.div()(
  // Div1
  (Dom.div()
    ..addTestId('first')
    ..addTestId('shared-id')
  )(),

  Dom.div()(
    // Div2
    (Dom.div()
      ..addTestId('second')
      ..addTestId('other-id')
      ..addTestId('shared-id')
    )(),
  ),
));

var allFirsts  = getAllByTestId(renderedInstance, 'first');    // Returns `[Div1]`
var allSeconds = getAllByTestId(renderedInstance, 'second');   // Returns `[Div2]`
var allOthers  = getAllByTestId(renderedInstance, 'other-id'); // Returns `[Div2]`
var allShared  = getAllByTestId(renderedInstance, 'other-id'); // Returns `[Div1, Div2]`
var allNonexistents = getAllByTestId(renderedInstance, 'nonexistent'); // Returns `null`

It is recommended that, instead of setting this key prop manually, you should use the UiProps.addTestId method so the prop is only set in a test environment.

Implementation

List /* < [1] > */ getAllByTestId(dynamic root, String value, {String key = defaultTestIdKey}) {
  if (root is react.Component) root = root.jsThis;

  if (isValidElement(root)) {
    return _getAllByTestIdShallow(root, value, key: key);
  }

  return react_test_utils.findAllInRenderedTree(root, allowInterop((descendant) {
    Map? props;
    if (react_test_utils.isDOMComponent(descendant)) {
      props = findDomNode(descendant)!.attributes;
    } else if (react_test_utils.isCompositeComponent(descendant)) {
      props = getProps(descendant);
    }
    return props != null && _hasTestId(props, key, value);
  }));
}