renderAndGetComponent function

Component? renderAndGetComponent(
  1. dynamic component, {
  2. bool autoTearDown = true,
  3. Callback? autoTearDownCallback,
})

Renders a React component or builder into a detached node and returns the associated Dart component.

If component is a function component, calling renderAndGetComponent will throw a StateError.

See TestJacket.getInstance for more information about this limitation.

Implementation

react.Component? renderAndGetComponent(dynamic component,
        {bool autoTearDown = true, Callback? autoTearDownCallback}) {
  final renderedInstance = render(component, autoTearDown: autoTearDown, autoTearDownCallback: autoTearDownCallback);

  // [1] Adding an additional check for dom components here because the current behavior when `renderedInstance` is
  //     a DOM component (Element) - is to return `null`. While that will most likely cause null exceptions once the
  //     consumer attempts to make a call on the "Dart instance" they have requested - we don't want this change
  //     to cause new exceptions in a scenario where the consumer was storing a null value and then simply
  //     not using it in their test.
  if (!react_test_utils.isCompositeComponent(renderedInstance) &&
      /*[1]*/!react_test_utils.isDOMComponent(renderedInstance)) {
    throw StateError(
      'renderAndGetComponent() is only supported when the rendered object is a composite (class based) component.');
  }

  return getDartComponent(renderedInstance);
}