pumpNyWidgetAndWaitForInit method
Pump a widget and wait for loading to complete.
This is useful for testing NyPage and NyState widgets that have
async init methods. It pumps frames until hasInitComplete is true
or the timeout is reached.
Example:
await tester.pumpNyWidgetAndWaitForInit(
HomePage(),
timeout: Duration(seconds: 5),
);
// Now the init() has completed
expect(find.text('Loaded Data'), findsOneWidget);
Implementation
Future<void> pumpNyWidgetAndWaitForInit(
Widget widget, {
Duration timeout = const Duration(seconds: 10),
ThemeData? theme,
bool useSimpleTheme = false,
}) async {
await pumpNyWidget(widget, theme: theme, useSimpleTheme: useSimpleTheme);
// Pump frames until init completes or timeout
final stopwatch = Stopwatch()..start();
while (stopwatch.elapsed < timeout) {
await pump(const Duration(milliseconds: 100));
// Check if any loading indicators are gone
final loaderFinder = find.byType(CircularProgressIndicator);
if (loaderFinder.evaluate().isEmpty) {
// Also check for Skeletonizer
final skeletonizerFinder = find.byType(skel.Skeletonizer);
final skeletonizers = skeletonizerFinder.evaluate();
bool hasActiveSkeletonizer = false;
for (final element in skeletonizers) {
final widget = element.widget as skel.Skeletonizer;
if (widget.enabled) {
hasActiveSkeletonizer = true;
break;
}
}
if (!hasActiveSkeletonizer) {
break;
}
}
}
await pump(const Duration(milliseconds: 100));
}