waitForElementsToBeRemoved function Null safety Async

Future<void> waitForElementsToBeRemoved(
  1. List<Node> callback(
      ),
    1. {Node? container,
    2. Duration? timeout,
    3. Duration interval = const Duration(milliseconds: 50),
    4. QueryTimeoutFn? onTimeout,
    5. MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}
    )

    Waits for the removal of all the elements returned from the callback to be removed from the DOM.

    To wait for a single element to be removed, use waitForElementToBeRemoved.

    Options

    container

    An ancestor DOM node of the elements you return from callback. This node will have a MutationObserver attached to it.

    Defaults to document.body.

    timeout

    How long to wait for the node to appear in the DOM before throwing a TestFailure, defaulting to 1000ms.

    interval

    How often the callback is called, defaulting to 50ms.

    onTimeout

    Is called if the timeout duration passes before the node is found in the DOM, and can be used to customize a TestFailure message.

    mutationObserverOptions

    The default values are:

    {subtree: true, childList: true, attributes: true, characterData: true}
    

    which will detect additions and removals of child elements (including text nodes) in the container and any of its descendants. It will also detect attribute changes. When any of those changes occur, it will re-run the callback.

    Implementation

    Future<void> waitForElementsToBeRemoved(
      List<Node> Function() callback, {
      Node? container,
      Duration? timeout,
      Duration interval = const Duration(milliseconds: 50),
      QueryTimeoutFn? onTimeout,
      MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions,
    }) async {
      container ??= document.body!;
      final els = callback();
    
      // Keep null check to maintain backwards compatibility for consumers that are not opted in to null safety.
      ArgumentError.checkNotNull(els);
    
      if (els.isEmpty) {
        throw TestingLibraryElementError('The callback must return one or more non-null Elements.');
      }
    
      for (final el in els) {
        if (!container.contains(el)) {
          throw TestingLibraryElementError(
              'One of the elements returned from the callback was not present in the container at the time waitForElementsToBeRemoved() was called:\n\n'
              '${prettyDOM(container)}');
        }
      }
    
      await Future.wait(els.map((el) => waitForElementToBeRemoved(() => el,
          container: container,
          timeout: timeout,
          interval: interval,
          onTimeout: onTimeout,
          mutationObserverOptions: mutationObserverOptions)));
    }