waitForElementToBeRemoved function Async
Waits for the removal of the single element returned from the callback
to be removed from the DOM.
Similar to testing-library.com/docs/dom-testing-library/api-async/#waitforelementtoberemoved,
but designed to work with Dart Futures instead of JS Promise
s.
To wait for multiple elements to be removed, use waitForElementsToBeRemoved.
Options
container
An ancestor DOM node of the element 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> waitForElementToBeRemoved(
Node Function() callback, {
Node? container,
Duration? timeout,
Duration interval = const Duration(milliseconds: 50),
QueryTimeoutFn? onTimeout,
MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions,
}) async {
final config = getConfig();
container ??= document.body!;
timeout ??= Duration(milliseconds: config.asyncUtilTimeout);
final el = callback();
// Keep null check to maintain backwards compatibility for consumers that are not opted in to null safety.
ArgumentError.checkNotNull(el);
if (!container.contains(el)) {
throw TestingLibraryElementError(
'The element returned from the callback was not present in the container at the time waitForElementToBeRemoved() was called:\n\n'
'${prettyDOM(container)}');
}
await waitFor(
() => expect(container!.contains(el), isFalse),
container: container,
timeout: timeout,
interval: interval,
onTimeout: onTimeout ??
(error) {
return TimeoutException(
'The element returned from the callback was still present in the container after ${timeout!.inMilliseconds}ms:\n\n'
'${prettyDOM(container)}');
},
mutationObserverOptions: mutationObserverOptions,
);
}